class Game: """pygame wrapper class""" def __init__(self, shape, name="", use_opengl=False): """ Parameters ---------- shape : tuple of ints Width and height of screen name : str (optional) name of game (will be shown above window) """ self.use_opengl = use_opengl if use_opengl: self.camera = Camera() self._obs = Observer(self) self.waiting = False self.name = name self.shape = np.array(shape) self.time = time() self.ms_per_frame = 30 def _open(self, set_mouse=False): # initialize pygame and window pygame.init() if self.use_opengl: pygame.display.set_mode(self.shape, DOUBLEBUF | OPENGL) self.camera.setup(self.shape) else: self.surf = pygame.display.set_mode(self.shape) pygame.display.set_caption(self.name) if set_mouse: pygame.mouse.set_pos(self.shape / 2) def _draw(self, routines=[]): """ Draws everything """ if self.use_opengl: GL.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT) else: self.surf.fill((0, 0, 0)) # draw stuff if not hasattr(routines, "__getitem__"): routines = [routines] if routines != None else [] for routine in routines: routine() if self.use_opengl: pygame.display.flip() else: pygame.display.update() def _balance(self, PRINT_SHIFT=False): """ Balances game between frames to make it run more smoothly Parameters ---------- PRINT_SHIFT : bool prints time delay to console """ shift = round((time() - self.time) * 1000) if PRINT_SHIFT: print(shift) if shift < self.ms_per_frame: pygame.time.delay(int(self.ms_per_frame - shift)) self.time = time() def _terminate(self, print_traceback=False): """ Closes pygame and stops game from running Parameters ---------- exception : AttributeError (optional) prints args if passed """ if print_traceback: print(traceback.format_exc()) pygame.quit() self.running = False print("pygame was terminated...") def run(self, routines=[]): """ Loops all game processes while running """ self._open(True) self.running = True while self.running: try: self._obs.handle_events() if not self.waiting: self._draw(routines) self.camera.run() self._balance() except Exception as e: self._terminate(e) pygame.quit()