def draw(self, screen: Surface, clock: pg.time.Clock) -> Optional[ScreenType]: player.handle_keys() light_mask = Surface((screen.get_width(), screen.get_height()), flags=SRCALPHA) light_mask.fill(Color(0, 0, 0)) # Draw stuff and fill the light mask with light sources map.draw(screen, light_mask) player.draw(screen, light_mask) screen.blit(light_mask, (0, 0)) monsters.draw_all(screen) clock.tick(60) try: logic.tick() except GameOverEx: return DefeatScreen except WonEx: return EndScreen fps = clock.get_fps() if fps > 0: pg.display.set_caption(f"FPS: {clock.get_fps()}") return None
def update_thread(fps: float, clock: pygame.time.Clock): ''' This loop runs on a seperate thread, and controls the internal game logic Currently, this should run at around 500 fps ''' fps_controller = FPSController(fps) global done while not done: s_time = time.time() b.drive(pygame.key.get_pressed()) b.update_loc() e_time = time.time() work_time = e_time - s_time fps_controller.sleep(work_time, clock.get_fps()) clock.tick(1000)
def draw_thread(fps: float, clock: pygame.time.Clock): ''' This loop controls the graphics in the game, and runs on the main thread Should run at 60 FPS ''' fps_controller = FPSController(fps) global done while not done: for event in pygame.event.get(): if event.type == pygame.QUIT: done = True s_time = time.time() pygame.draw.rect(screen, (255, 255, 255), (0, 0, w, h)) b.draw(screen) pygame.display.flip() e_time = time.time() work_time = e_time - s_time fps_controller.sleep(work_time, clock.get_fps()) clock.tick(1000)