User Tools

Site Tools


p5bg:bullet_game

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:bullet_game [2024/08/31 02:19] appledogp5bg:bullet_game [2024/09/11 06:42] (current) – removed appledog
Line 1: Line 1:
-= Bullet Game 
-Bullet Game is our near-end stage pygame 2d game framework example. We've already done at least 6 or 7 other games using pygame. So this will document the patterns we have learned. 
  
-We began by "just writing" a game, then the second part was refactoring the game. I wanted to make refactoring it's own project. What are the goals of this refactoring? 
- 
-* Refactoring 
-** Encapsulation 
-** Light Commentary (code commentary) ex. IN and OUT for functions. 
- 
-The primary goal is encapsulation. So, we're going to break up the old main into several new classes. 
- 
-== main.py 
-It is possible to completely minimize main.py into a game = Game(), but that just passes the buck. Main should be the first place someone looks, so let's keep some 'headline information' there-- the name of the program, general window information, and so on. We'll pass this information to Game, to keep Game's //init// as simple as possible. 
- 
-<Code> 
-from Window import Window 
-from Game import Game 
- 
-def main(): 
-    window = Window() 
-    window.setLogo("logo32x32.png") 
-    window.setCaption("Bullet Game") 
-    window.setSize(Screen.WIDTH, Screen.HEIGHT) 
-    window.setFont("assets/ps2p.ttf", 32) 
-     
-    game = Game(window) 
-    game.start() 
- 
-if __name__ == "__main__": 
-    main() 
-</Code> 
- 
-== Screen and Window 
-The first thing we always do is have a Screen class and a Window class. The Screen class defines things like WIDTH and HEIGHT, and the Window class holds the pygame screen and other pygame initialization. 
- 
-Initially, this code would appear at the start of a main.py. Later we moved it into class Game's __init__. However, to keep things clean we made it into it's own class. 
- 
-=== class Screen: 
-This contains things like the width and height that will be needed by many different classes. This could also be put into window. 
- 
-<Code:Python> 
- 
-class Screen: 
-    WIDTH = 800 
-    HEIGHT = 600 
- 
-    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? 
- 
-=== class Window: 
-| def __init__ | This calls pygame.init(). See [[main]] | 
-| setLogo | this sets the little icon in the corner of the screen. It's the icon for the desktop, usually. Normally we don't care about this. | 
-| setCaption | This will set the title of the window. Usually the name of the program or game. | 
-| setFont | This will probably be moved to it's own Font class later. That way we can have multiple fonts if we want and it keeps the code a bit cleaner. | 
- 
-==== full code 
-<Code:Python> 
-import pygame 
-from Screen import * 
- 
-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 
- 
-        return self.font</Code> 
p5bg/bullet_game.1725070751.txt.gz · Last modified: 2024/08/31 02:19 by appledog

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki