p5bg:pygame_framework
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
p5bg:pygame_framework [2024/09/11 05:12] – appledog | p5bg:pygame_framework [2024/09/11 06:43] (current) – removed appledog | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | = PyGame Framework | ||
- | This is a lighter version of [[:pygame terminal]], without the terminal stuff, mainly used for quickly making a 2d arcade style game. We will quickly go over the main points and then discuss changes and patterns discovered over the past several projects. | ||
- | |||
- | == Fonts | ||
- | * Press Start 2P (an arcade font) {{: | ||
- | * CLACON2 (Classic Console 2) {{: | ||
- | * IBM VGA 8x16 (9x16 is better?) {{: | ||
- | |||
- | |||
- | == main.py | ||
- | < | ||
- | from Window import * | ||
- | from Game import * | ||
- | |||
- | def main(): | ||
- | window = Window() | ||
- | window.setSize(800, | ||
- | window.setCaption(" | ||
- | window.setFont(" | ||
- | |||
- | game = Game(window) | ||
- | game.start() | ||
- | |||
- | if __name__ == " | ||
- | main() | ||
- | </ | ||
- | |||
- | == Window.py | ||
- | Note that the window will not usually appear until the events loop ("for event in pygame.event.get()" | ||
- | |||
- | drawText is included here, since it is a kind of convenience function. If you don't need this you don't need the setFont() in main() or the TTF file either. | ||
- | |||
- | < | ||
- | 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 | ||
- | |||
- | def drawText(self, | ||
- | text_surface = self.font.render(text, | ||
- | x = self.fontwidth * at_x | ||
- | y = self.fontheight * at_y | ||
- | self.screen.blit(text_surface, | ||
- | |||
- | == Screen.py | ||
- | New addition. One of the things we discovered is the need for a "class Constants" | ||
- | |||
- | < | ||
- | |||
- | class Screen: | ||
- | |||
- | WIDTH = 800 | ||
- | HEIGHT = 600 | ||
- | FPS = 60 | ||
- | |||
- | MARGIN = 96 | ||
- | GAMETOP = MARGIN | ||
- | GAMEBOT = HEIGHT - MARGIN | ||
- | </ | ||
- | |||
- | Here, margin and gametop are used as display boundaries for things like sprites so text can be drawn at the top of the screen and things stay within the edges of the screen. The most important parts are WIDTH and HEIGHT. A big question is, should Screen and Window be merged? | ||
- | |||
- | == Game.py | ||
- | A number of changes. One, instead of display.flip() we use display.update(). Second, instead of time.sleep(1 / 60) to get 60 FPS we use clock.tick(60). | ||
- | |||
- | Some extra framework code was added for convenience, | ||
- | |||
- | < | ||
- | import pygame | ||
- | import time | ||
- | |||
- | class Game: | ||
- | def __init__(self, | ||
- | self.window = window | ||
- | self.screen = window.screen | ||
- | self.font = window.font | ||
- | |||
- | # Set up game variables (see notes) | ||
- | self.running = True | ||
- | |||
- | def start(self): | ||
- | |||
- | # Clock for controlling the frame rate | ||
- | clock = pygame.time.Clock() | ||
- | |||
- | # Variables to keep track of time, if needed | ||
- | frame_counter = 0 | ||
- | time_start = time.time() * 1000 | ||
- | |||
- | # Main Loop | ||
- | while self.running == True: | ||
- | # Manage time for timed events like animations | ||
- | time_clock = (time.time() * 1000) - time_start | ||
- | frame_counter += 1 | ||
- | if frame_counter > Screen.FPS: | ||
- | frame_counter = 1 | ||
- | |||
- | # All event handling | ||
- | self.checkEvents() | ||
- | |||
- | # Frame generation | ||
- | self.screen.fill((0, | ||
- | self.drawGame() | ||
- | pygame.display.update() | ||
- | |||
- | # Control the frame rate | ||
- | clock.tick(Screen.FPS) | ||
- | |||
- | def drawGame(self): | ||
- | self.window.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_q: | ||
- | self.quit_game() | ||
- | if event.key == pygame.K_ESCAPE: | ||
- | self.quit_game() | ||
- | |||
- | if event.key == pygame.K_LEFT: | ||
- | print(" | ||
- | |||
- | elif event.key == pygame.K_RIGHT: | ||
- | print(" | ||
- | |||
- | elif event.key == pygame.K_UP: | ||
- | print(" | ||
- | |||
- | elif event.key == pygame.K_DOWN: | ||
- | print(" | ||
- | |||
- | else: | ||
- | pass | ||
- | |||
- | def quit_game(self): | ||
- | print(" | ||
- | pygame.quit() | ||
- | quit() | ||
- | exit() | ||
- | </ | ||
- | |||
- | == Color.py | ||
- | This is an important file because it allows us to set and use standard colors. | ||
- | |||
- | < | ||
- | class Color: | ||
- | WHITE = (255, 255, 255) | ||
- | BLACK = (0, 0, 0) | ||
- | GRAY = (127, 127, 127) | ||
- | |||
- | RED = (255, 0, 0) | ||
- | GREEN = (0, 255, 0) | ||
- | BLUE = (0, 0, 255) | ||
- | |||
- | # VGA Colors (in hexadecimal) | ||
- | VGA_BLACK = 0x000000 | ||
- | VGA_BLUE = 0x0000AA | ||
- | VGA_GREEN = 0x00AA00 | ||
- | VGA_CYAN = 0x00AAAA | ||
- | VGA_RED = 0xAA0000 | ||
- | VGA_MAGENTA = 0xAA00AA | ||
- | VGA_BROWN = 0xAA5500 | ||
- | VGA_LIGHT_GRAY = 0xAAAAAA | ||
- | VGA_DARK_GRAY = 0x555555 | ||
- | VGA_LIGHT_BLUE = 0x5555FF | ||
- | VGA_LIGHT_GREEN = 0x55FF55 | ||
- | VGA_LIGHT_CYAN = 0x55FFFF | ||
- | VGA_LIGHT_RED = 0xFF5555 | ||
- | VGA_LIGHT_MAGENTA = 0xFF55FF | ||
- | VGA_YELLOW = 0xFFFF55 | ||
- | VGA_WHITE = 0xFFFFFF | ||
- | |||
- | # Array of VGA colors for easy access by index | ||
- | vga_color = [ | ||
- | VGA_BLACK, | ||
- | VGA_BLUE, | ||
- | VGA_GREEN, | ||
- | VGA_CYAN, | ||
- | VGA_RED, | ||
- | VGA_MAGENTA, | ||
- | VGA_BROWN, | ||
- | VGA_LIGHT_GRAY, | ||
- | VGA_DARK_GRAY, | ||
- | VGA_LIGHT_BLUE, | ||
- | VGA_LIGHT_GREEN, | ||
- | VGA_LIGHT_CYAN, | ||
- | VGA_LIGHT_RED, | ||
- | VGA_LIGHT_MAGENTA, | ||
- | VGA_YELLOW, | ||
- | VGA_WHITE | ||
- | ] | ||
- | </ |
p5bg/pygame_framework.1726031561.txt.gz · Last modified: 2024/09/11 05:12 by appledog