def main_loop(games): keep_running = True show_splash = True ticks = 0 banana = Banana() splash = Splash() while show_splash: delta = timer.tick(fps) splash.draw(delta) screen.blit(splash.surface, (0, 0)) splash.new_generation() splash.iterate() pygame.display.flip() for e in pygame.event.get(): if e.type is pygame.KEYDOWN and e.key == pygame.K_SPACE: show_splash = False if e.type is pygame.KEYDOWN and e.key == pygame.K_ESCAPE: keep_running = False show_splash = False while keep_running: timer.tick(fps) screen.fill(black) for e in pygame.event.get(): if e.type is pygame.KEYDOWN and (e.key == pygame.K_ESCAPE or e.key == pygame.K_SPACE): keep_running = False for i, game in enumerate(games): game.playSound(ticks) game.change_column_color(ticks) game.draw() screen.blit(game.surface, ((i%2)*game_screen_width,(i/2)*game_screen_height)) banana.draw(ticks) screen.blit(banana.surface, (game_screen_width, game_screen_height)) pygame.draw.line(screen, white, (0, game_screen_height), (2*game_screen_width, game_screen_height)) pygame.draw.line(screen, white, (game_screen_width, 0), (game_screen_width, 2*game_screen_height)) pygame.display.flip() ticks += 1 if ticks == 16: ticks = 0 for game in games: game.new_generation() game.iterate()
def main_loop(self): ''' Pętla główna gry. ''' self.__timeElapsed = 0.0 # czas jaki upłynął od początku gry splash = Splash(self, self.__camera, config.SPLASH_DISPLAY_TIME) while not self.__window.has_exit: self.__window.dispatch_events() # update świata i HUDa dt = clock.tick() * config.DTIME_MULTIPLY self.__timeElapsed += dt if config.PRINT_FPS and dt>0.001: print "FPS:", 1.0/dt # ustaw kamerę self.__camera.set3d() # narysuj splash screen albo grę if self.__timeElapsed < config.SPLASH_DISPLAY_TIME: splash.update(dt) splash.draw() else: self.__world.update( dt ) # self.__hud.update( dt ) # narysuj świat self.__camera.set3d() self.__world.draw() # # narysuj HUD # self.__camera.set2d() # self.__hud.draw() self.__window.flip()
def main_loop(self): """ Pętla główna gry. """ self.__timeElapsed = 0.0 # czas jaki upłynął od początku gry splash = Splash(self, self.__camera, config.SPLASH_DISPLAY_TIME) while not self.__window.has_exit: self.__window.dispatch_events() # update świata i HUDa dt = clock.tick() * config.DTIME_MULTIPLY self.__timeElapsed += dt if config.PRINT_FPS and dt > 0.001: print "FPS:", 1.0 / dt # ustaw kamerę self.__camera.set3d() # narysuj splash screen albo grę if self.__timeElapsed < config.SPLASH_DISPLAY_TIME: splash.update(dt) splash.draw() else: self.__world.update(dt) # self.__hud.update( dt ) # narysuj świat self.__camera.set3d() self.__world.draw() # # narysuj HUD # self.__camera.set2d() # self.__hud.draw() self.__window.flip()
class Game(): def __init__(self): pygame.init() pygame.font.init() self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) self.end_game = False self.clock = pygame.time.Clock() pygame.display.set_caption('Dreamer') background_img = pygame.image.load( './assets/backgrounds/background.png') self.background = pygame.transform.scale(background_img, (SCREEN_WIDTH, SCREEN_HEIGHT)) self.screen.blit(self.background, (0, 0)) self.splash_screen = Splash(self.screen, 'start') self.main_game = GameLevel(self.screen) self.tick = 0 self.final_score = 0 self.game_screen = 1 return def splash(self): self.splash_screen.draw() keys = pygame.key.get_pressed() if keys[pygame.K_RETURN]: self.game_screen += 1 return def run(self): while not self.end_game: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() self.clock.tick(LOOP_FREQUENCY) if self.game_screen == 1: self.splash() elif self.game_screen == 2: self.final_score = self.main_game.final_score self.splash_screen = Splash(self.screen, 'playing', str(self.final_score)) self.splash_screen.draw() if self.main_game.update_state(self.tick): # print("Final score: {}pts.".format(self.final_score)) # sys.exit() self.game_screen = 3 elif self.game_screen == 3: self.splash_screen = Splash(self.screen, 'end', str(self.final_score)) self.splash() elif self.game_screen == 4: sys.exit() pygame.display.update() if self.tick == TARGET_UPDATE_DELAY: self.tick = -1 self.tick += 1 self.screen.blit(self.background, (0, 0)) return