pygame_terminal
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
pygame_terminal [2023/09/20 01:53] – appledog | pygame_terminal [2023/11/06 00:23] (current) – appledog | ||
---|---|---|---|
Line 14: | Line 14: | ||
Additionally a small logo can be used, but if you comment out the logo line it still works. | Additionally a small logo can be used, but if you comment out the logo line it still works. | ||
+ | |||
+ | == Simpifying the code | ||
+ | The code can be simplified by removing all references to logos and captions, and removing the boilerplate key code for up and down arrows, etc. | ||
+ | |||
+ | == Code Commentary | ||
+ | The most overlooked part of such a system is the font metrics. Often it is required to determine only only the width and height but also determine if there is a baseline or an offset for the characters. It is important to know that there is not always an easy way to do this on a font by font basis and sometimes you must use a manual adjustment. In the base code shown here there is a -2 adjustment to font height in setFont(). This may be adjusted to taste by the programmer based on the font and the situation. | ||
+ | |||
+ | Pro tip, you may also wish to blit the text 1 to 5 pixels to the right and down so that it does not touch the window borders. | ||
== Character Set and Logo | == Character Set and Logo | ||
* I use a few different ones depending on the situation. I find that the ones included here are some of the best and/or most useful. | * I use a few different ones depending on the situation. I find that the ones included here are some of the best and/or most useful. | ||
* Example Logo: {{: | * Example Logo: {{: | ||
+ | |||
+ | The best source for old character sets is [[https:// | ||
+ | |||
+ | The clacon2 font is also very good: http:// | ||
+ | |||
+ | == main.py | ||
+ | < | ||
+ | from Window import Window | ||
+ | from Game import Game | ||
+ | |||
+ | def main(): | ||
+ | window = Window() | ||
+ | window.setLogo(" | ||
+ | window.setCaption(" | ||
+ | window.setSize(960, | ||
+ | window.setFont(" | ||
+ | |||
+ | game = Game(window) | ||
+ | game.start() | ||
+ | |||
+ | if __name__ == " | ||
+ | main() | ||
+ | </ | ||
+ | |||
+ | == game.py | ||
+ | < | ||
+ | import pygame | ||
+ | import time | ||
+ | |||
+ | class Game: | ||
+ | def __init__(self, | ||
+ | self.window = window | ||
+ | self.screen = window.screen | ||
+ | self.logo = window.logo | ||
+ | self.font = window.font | ||
+ | |||
+ | # Clear the screen. | ||
+ | self.screen.fill((0, | ||
+ | |||
+ | # Set up game variables | ||
+ | self.running = True | ||
+ | |||
+ | def start(self): | ||
+ | # Main loop | ||
+ | while self.running == True: | ||
+ | self.checkEvents() | ||
+ | |||
+ | self.screen.fill((0, | ||
+ | |||
+ | self.drawGame() | ||
+ | |||
+ | pygame.display.flip() | ||
+ | time.sleep(1 / 60) # Sleep for 1/FPS, or about 60 fps. | ||
+ | |||
+ | def drawGame(self): | ||
+ | self.drawText(0, | ||
+ | |||
+ | |||
+ | def checkEvents(self): | ||
+ | for event in pygame.event.get(): | ||
+ | if event.type == pygame.QUIT: | ||
+ | self.running = False | ||
+ | return | ||
+ | |||
+ | if event.type == pygame.KEYDOWN: | ||
+ | # key down event, process keys. | ||
+ | |||
+ | if event.key == pygame.K_LEFT: | ||
+ | pass | ||
+ | | ||
+ | elif event.key == pygame.K_RIGHT: | ||
+ | pass | ||
+ | |||
+ | elif event.key == pygame.K_UP: | ||
+ | pass | ||
+ | |||
+ | elif event.key == pygame.K_DOWN: | ||
+ | pass | ||
+ | |||
+ | else: | ||
+ | pass | ||
+ | |||
+ | |||
+ | def drawText(self, | ||
+ | text_surface = self.font.render(text, | ||
+ | x = self.window.fontwidth * at_x | ||
+ | y = self.window.fontheight * at_y | ||
+ | self.screen.blit(text_surface, | ||
+ | </ | ||
+ | |||
+ | == window.py | ||
+ | < | ||
+ | import pygame | ||
+ | |||
+ | class Window: | ||
+ | def __init__(self): | ||
+ | pygame.init() | ||
+ | |||
+ | def setLogo(self, | ||
+ | self.logo = pygame.image.load(filename) | ||
+ | pygame.display.set_icon(self.logo) | ||
+ | return self.logo | ||
+ | |||
+ | def setCaption(self, | ||
+ | pygame.display.set_caption(cap) | ||
+ | |||
+ | def setSize(self, | ||
+ | self.width = width | ||
+ | self.height = height | ||
+ | self.size = (width, height) | ||
+ | self.screen = pygame.display.set_mode(self.size) | ||
+ | return self.screen | ||
+ | |||
+ | def setFont(self, | ||
+ | pygame.font.init() | ||
+ | self.font = pygame.font.Font(filename, | ||
+ | |||
+ | font_width, font_height = self.font.size(" | ||
+ | self.fontwidth = font_width | ||
+ | self.fontheight = size - 2 | ||
+ | |||
+ | return self.font | ||
+ | </ | ||
+ | |||
+ | == Next | ||
+ | You may wish to continue on to [[Pygame Terminal II]] for a more advanced version of this. |
pygame_terminal.1695174797.txt.gz · Last modified: 2023/09/20 01:53 by appledog