User Tools

Site Tools


pygame_terminal

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
pygame_terminal [2023/09/20 01:53] appledogpygame_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: {{:logo32x32.png?direct&32|}} * Example Logo: {{:logo32x32.png?direct&32|}}
 +
 +The best source for old character sets is [[https://int10h.org/oldschool-pc-fonts/|The Ultimate Oldschool PC Font Pack]]. I usually use an IBM_PC_VGA_8x16_PLUS TTF font. They're easy to work with in Python, Java, and C++. If you can't use TTF on the web there are woff and other versions available too. The //plus// means you can use unicode and/or ANSI codes which is often the only way to print IBM PC special characters. Perfect for those writing a ZZT clone!
 +
 +The clacon2 font is also very good: http://webdraft.hu/fonts/classic-console/
 +
 +== main.py
 +<Code:Python>
 +from Window import Window
 +from Game import Game
 +
 +def main():
 +    window = Window()
 +    window.setLogo("logo32x32.png")
 +    window.setCaption("Basic Console Starter Program")
 +    window.setSize(960, 640)
 +    window.setFont("PxPlus_IBM_VGA8.ttf", 32) # 32 point font
 +
 +    game = Game(window)
 +    game.start()
 +
 +if __name__ == "__main__":
 +    main()
 +</Code>
 +
 +== game.py
 +<Code:Python>
 +import pygame
 +import time
 +
 +class Game:
 +    def __init__(self, window):
 +        self.window = window
 +        self.screen = window.screen
 +        self.logo = window.logo
 +        self.font = window.font
 +
 +        # Clear the screen.
 +        self.screen.fill((0, 0, 0))
 +
 +        # Set up game variables
 +        self.running = True
 +
 +    def start(self):
 +        # Main loop
 +        while self.running == True:
 +            self.checkEvents()
 +
 +            self.screen.fill((0, 0, 0))  # Clear the screen.
 +
 +            self.drawGame()
 +
 +            pygame.display.flip()  # update the display.
 +            time.sleep(1 / 60)  # Sleep for 1/FPS, or about 60 fps.
 +
 +    def drawGame(self):
 +        self.drawText(0, 2, "it works!", "gray")
 +
 +
 +    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, at_x, at_y, text, color):
 +        text_surface = self.font.render(text, False, color)
 +        x = self.window.fontwidth * at_x
 +        y = self.window.fontheight * at_y
 +        self.screen.blit(text_surface, (x + 2, y))
 +</Code>
 +
 +== window.py
 +<Code:Python>
 +import pygame
 +
 +class Window:
 +    def __init__(self):
 +        pygame.init()
 +
 +    def setLogo(self, filename):
 +        self.logo = pygame.image.load(filename)
 +        pygame.display.set_icon(self.logo)
 +        return self.logo
 +
 +    def setCaption(self, cap):
 +        pygame.display.set_caption(cap)
 +
 +    def setSize(self, width, height):
 +        self.width = width
 +        self.height = height
 +        self.size = (width, height)
 +        self.screen = pygame.display.set_mode(self.size)
 +        return self.screen
 +
 +    def setFont(self, filename, size):
 +        pygame.font.init()
 +        self.font = pygame.font.Font(filename, size)
 +
 +        font_width, font_height = self.font.size("@")
 +        self.fontwidth = font_width
 +        self.fontheight = size - 2
 +
 +        return self.font
 +</Code>
 +
 +== 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

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki