User Tools

Site Tools


p5bg:pygame_framework

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
p5bg:pygame_framework [2024/09/11 04:58] appledogp5bg: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. 
- 
-== PressStart2p.ttf 
-* {{:pressstart2p.ttf.zip|}} 
- 
-== main.py 
-<Code:Python> 
-from Window import * 
-from Game import * 
- 
-def main(): 
-    window = Window() 
-    window.setSize(800,600) 
-    window.setCaption("PyGame Framework Example") 
-    window.setFont("clacon2.ttf", 32) 
- 
-    game = Game(window) 
-    game.start() 
- 
-if __name__ == "__main__": 
-    main() 
-</Code> 
- 
-== Window.py 
-Minor changes. 
-drawText was moved to here, since it operates on the window itself and can sometimes be needed by things outside of Game. 
- 
-<Code:Python> 
-import pygame 
- 
-class Window: 
-    def __init__(self, width, height): 
-        pygame.init() 
-        self.setSize(width, height) 
- 
-    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 
- 
-    def drawText(self, at_x, at_y, text, color): 
-        text_surface = self.font.render(text, False, color) 
-        x = self.fontwidth * at_x 
-        y = self.fontheight * at_y 
-        self.screen.blit(text_surface, (x + 2, y))</Code> 
- 
-== Screen.py 
-New addition. One of the things we discovered is the need for a "class Constants" or "Globals" sometimes, and screen related things like FPS are kept here. This is required sometimes by sprites to calculate their updates. 
- 
-<Code:Python> 
- 
-class Screen: 
- 
-    WIDTH = 800 
-    HEIGHT = 600 
-    FPS = 60 
- 
-    MARGIN = 96 
-    GAMETOP = MARGIN 
-    GAMEBOT = HEIGHT - MARGIN 
-</Code> 
- 
-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 p 
- 
-<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 (see notes) 
-        self.running = True 
- 
-    def start(self): 
- 
-        # Clock for controlling the frame rate 
-        clock = pygame.time.Clock() 
- 
-        # Main game loop 
-        frame_counter = 0 
-        time_start = time.time() * 1000 
- 
-        while self.running == True: 
-            # Manage time for timed events like animations 
-            time_clock = (time.time() * 1000) - time_start 
-            frame_counter += 1 
-            if frame_counter > 60: 
-                frame_counter = 1 
- 
-            # All event handling 
-            self.checkEvents() 
- 
-            # Frame generation 
-            self.screen.fill((0, 0, 0))  # Clear the screen. 
-            self.drawGame() 
-            pygame.display.update()  # update the display. Can also use flip() 
- 
-            # Control the frame rate 
-            clock.tick(60) 
- 
-    def drawGame(self): 
-        self.window.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_q: 
-                    self.quit_game() 
-                if event.key == pygame.K_ESCAPE: 
-                    self.quit_game() 
- 
-                if event.key == pygame.K_LEFT: 
-                    print("left") 
- 
-                elif event.key == pygame.K_RIGHT: 
-                    print("right") 
- 
-                elif event.key == pygame.K_UP: 
-                    print("up") 
- 
-                elif event.key == pygame.K_DOWN: 
-                    print("down") 
- 
-                else: 
-                    pass 
- 
-    def quit_game(self): 
-        print("Exiting game...") 
-        pygame.quit() 
-        quit() 
-        exit() 
-</Code> 
  
p5bg/pygame_framework.1726030730.txt.gz · Last modified: 2024/09/11 04:58 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki