class GameLoop(DirectorLoop): def __init__(self): super().__init__() self.clock = GameClock() try: import IPython.core shell = IPython.core.getipython.get_ipython() self._kernel = shell.kernel except ImportError: self._kernel = None def terminal_init(self): super().terminal_init() terminal.set(""" window.resizeable=true; """) def get_initial_scene(self): return MainMenuScene() def terminal_update(self): self.clock.tick() if self._kernel: self._kernel.do_one_iteration() return super().terminal_update()
class GalaxyView: def __init__(self): self.galaxy = Galaxy() self.screen = pygame.display.set_mode((500, 500)) self.clock = GameClock(FPS, update_callback=self.update) def draw_planets(self): for _planet in self.galaxy.planets: if isinstance(_planet, planet.Planet): self.screen.set_at(_planet.position, (255, 255, 255)) def draw(self): self.screen.fill((0, 0, 0)) self.draw_planets() pygame.display.flip() def main_loop(self): while True: self.clock.tick() def update(self, dt): self.galaxy.apply_dt(dt) self.draw() for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit()
def run(self): def callback(time): self.evManager.post(TickEvent()) return clock = GameClock() clock.schedule_interval(callback, 0.042) #clock.schedule_interval(callback, 0.25) while self.keepGoing: clock.tick()
def main(): global clock, pygame_clock, screen, screen_rect, sprite_group, eraser_image global USE_PYGAME_CLOCK, DO_KILL, USE_PREDICTION, PYGAME_FPS screen = pygame.display.set_mode((800, 600)) screen.fill(BGCOLOR) screen_rect = screen.get_rect() eraser_image = screen.copy() which_settings = 0 pygame_clock = pygame.time.Clock() clock = GameClock(*SETTINGS[which_settings], update_callback=update_gameclock, frame_callback=display_gameclock) clock.schedule_interval(set_caption_gameclock, 1.0) clock.use_wait = False sprite_group = pygame.sprite.Group([Ball() for i in range(INIT_BALLS)]) # game_is_running = True while game_is_running: if USE_PYGAME_CLOCK: pygame_clock.tick(PYGAME_FPS) update_pygame() display_pygame() else: clock.tick() # for e in pygame.event.get(): if e.type == QUIT: quit() elif e.type == KEYDOWN: if e.key == K_ESCAPE: quit() elif e.key == K_TAB: which_settings += 1 if which_settings >= len(SETTINGS): which_settings = 0 (clock.max_ups, clock.max_fps) = SETTINGS[which_settings] elif e.key == K_1: sprite_group.add(Ball()) elif e.key == K_b: sprite_group.add([Ball() for i in range(25)]) elif e.key == K_k: DO_KILL = not DO_KILL elif e.key == K_l: USE_PYGAME_CLOCK = not USE_PYGAME_CLOCK elif e.key == K_m: if PYGAME_FPS == 0: PYGAME_FPS = 30 else: PYGAME_FPS = 0 elif e.key == K_p: USE_PREDICTION = not USE_PREDICTION elif e.key == K_w: clock.use_wait = not clock.use_wait
class InteractiveApp(App): def __init__(self, kernel): self.kernel = kernel self.clock = GameClock() self.clock.frame_callback = self.my_update def init_root(self, width=80, height=26, show_credits=False): self.root = tdl.init(width, height) self.scratch = tdl.Console(width, height) self.temp_console = tdl.Console(width, height) self.width, self.height = width, height tdl.set_fps(30) if show_credits: tcod.console_credits() # tcod.sys_set_fps(self.max_fps) def draw(self): self.root.drawStr(1, 2, "No draw method defined, using default method.") self.root.drawStr(1, 3, "Time elapsed = {0}".format(tcod.sys_elapsed_milli())) self.root.drawStr(1, 4, "FPS = {0}".format(tcod.sys_get_fps())) def my_update(self, dt): self.root.clear() self.draw() tdl.flush() def runOnce(self): """Pump events to this App instance and then return. This works in the way described in L{App.run} except it immediately returns after the first L{update} call. Having multiple L{App} instances and selectively calling runOnce on them is a decent way to create a state machine. """ self.kernel.do_one_iteration() self.clock.tick() for event in get(): if event.type: # exclude custom events with a blank type variable # call the ev_* methods method = 'ev_%s' % event.type # ev_TYPE getattr(self, method)(event) if event.type == 'KEYDOWN': # call the key_* methods method = 'key_%s' % event.key # key_KEYNAME if hasattr(self, method): # silently exclude undefined methods getattr(self, method)(event)
class InteractiveApp(App): def __init__(self, kernel): self.kernel = kernel self.clock = GameClock() self.clock.frame_callback = self.frame_update def init_root(self, width=80, height=26, show_credits=False): self.root = tdl.init(width, height) self.canvas = tdl.Console(width, height) self.temp_console = tdl.Console(width, height) self.width, self.height = width, height tdl.set_fps(30) if show_credits: tcod.console_credits() # tcod.sys_set_fps(self.max_fps) def draw(self): self.root.blit(self.canvas) def frame_update(self, dt): self.root.clear() self.draw() tdl.flush() def runOnce(self): """Pump events to this App instance and then return. This works in the way described in L{App.run} except it immediately returns after the first L{update} call. Having multiple L{App} instances and selectively calling runOnce on them is a decent way to create a state machine. """ if self.kernel: self.kernel.do_one_iteration() self.clock.tick() for event in get(): if event.type: # exclude custom events with a blank type variable # call the ev_* methods method = 'ev_%s' % event.type # ev_TYPE getattr(self, method)(event) if event.type == 'KEYDOWN': # call the key_* methods method = 'key_%s' % event.key # key_KEYNAME if hasattr(self, method): # silently exclude undefined methods getattr(self, method)(event)
class GalaxyView: def __init__(self): self.galaxy = Galaxy() self.screen = pygame.display.set_mode((500, 500)) self.clock = GameClock(update_callback=self.update, max_ups=30) def update(self, dt): self.handle_events() self.screen.fill((30, 0, 0)) for i in range(10): self.galaxy.apply_dt(dt/(2000*(i+1))) self.galaxy.load_from_gpu() for _planet in self.galaxy.planets_np: temp = _planet.astype(np.int) _planet_temp = planet.Planet(r=[temp[0], temp[1]]) self.screen.set_at(_planet_temp.r, (255, 255, 255)) pygame.display.flip() def handle_events(self): for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: self.galaxy.clicked = True return elif event.type == pygame.MOUSEBUTTONUP: self.galaxy.clicked = False return elif event.type == pygame.MOUSEMOTION: if self.galaxy.clicked: x, y = event.pos y = 500-y print(x, y) self.galaxy.click = np.array([x, y], dtype=np.float32) def main_loop(self): while True: self.clock.tick()
# noinspection PyUnusedLocal def _update(_): """ Pump events to the current GameState and tell its objects to update :param _: unused dt provided by GameClock :return: """ events = get() CONTEXT["gamestate"].handle_input(events) CONTEXT["gamestate"].update() # gamestate.handle_input(events) # gamestate.update() # noinspection PyUnusedLocal def _draw(_): """ Ask the current GameState to redraw itself :param _: unused interp provided by GameClock :return: """ # gamestate.draw() CONTEXT["gamestate"].draw() flip() CLOCK = GameClock(max_ups=60, max_fps=60, update_callback=_update, frame_callback=_draw) while True: CLOCK.tick()