Created
September 21, 2022 13:43
-
-
Save thisisignitedoreo/8be607c4f7241e7c0c1833beb712549a to your computer and use it in GitHub Desktop.
PyGame boilerplate code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pygame as pg | |
| class Program: | |
| def __init__(self, res=None, fps=60, title=None): | |
| pg.init() | |
| self.res = self.width, self.height = res if not res is None else [800, 600] # If resolution is not set then 800x600 | |
| self.center = self.center_width, self.center_height = (res[0] // 2, res[1] // 2) if not res is None else [400, 300] # Same, but divided by 2 | |
| self.fps = fps | |
| self.screen = pg.display.set_mode(self.res) | |
| self.clock = pg.time.Clock() | |
| self.title = title | |
| def draw(self): # Draw things here | |
| self.screen.fill(pg.color.Color("darkslategray")) | |
| def update(self): # Calculate things here | |
| pass | |
| def run(self): | |
| while True: | |
| self.update() | |
| self.draw() | |
| [exit() for ev in pg.event.get() if ev.type == pg.QUIT] | |
| pg.display.set_caption(str(self.clock.get_fps()) if self.title is None else self.title) # Set caption to FPS if caption is not set | |
| pg.display.flip() | |
| self.clock.tick(self.fps) | |
| if __name__ == "__main__": | |
| pgm = Program() | |
| pgm.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment