def __init__(self): super().__init__("main_menu") self.gui = GUI_group() splash_text = Text_widget( (Config.SCREENWIDTH // 2, Config.SCREENHEIGHT // 4 + 60), random.choice(Main_menu.SPLASH_TEXTS), color_normal=pygame.Color(180, 180, 180), text_justify="center", vertical_align="center") start_button = Button_widget( (Config.SCREENWIDTH // 2 - 125, Config.SCREENHEIGHT // 2), "Spiel starten") start_button.on_click.append(self.start_button_click) settings_button = Button_widget( (Config.SCREENWIDTH // 2 - 125, Config.SCREENHEIGHT // 2 + 60), "Einstellungen") settings_button.on_click.append(self.settings_button_click) exit_button = Button_widget( (Config.SCREENWIDTH // 2 - 125, Config.SCREENHEIGHT // 2 + 120), "Beenden") exit_button.on_click.append(self.exit_button_click) self.gui.add(splash_text, start_button, settings_button, exit_button) self.next_gamestate = None self.logo = pygame.image.load("logo.png") Sound_manager.stop_music()
def update(self, delta): # handle racket control (ai or keyboard or mouse) if self.countdown is None: for racket_control in self.racket_controls: racket_control.update(delta) # update rackets for racket in self.rackets: racket.update(delta) # update ball if self.countdown is None: self.ball.update(delta) # update countdown if self.countdown is not None: count_before = int(self.countdown + 1) self.countdown -= delta / 1000 count_after = int(self.countdown + 1) if count_before != count_after: if count_after >= 1: Sound_manager.play("tick") else: Sound_manager.play("start") self.countdown_text.text = f"Spielstart in {int(self.countdown + 1)} s..." if self.countdown <= 0: self.countdown = None else: self.countdown_text.text = "" # update gui self.gui.update(delta) return self.next_gamestate
screen = pygame.display.set_mode((Config.SCREENWIDTH, Config.SCREENHEIGHT)) clock = pygame.time.Clock() # initialize gamestate manager and load main menu gamestate_manager = Gamestate_manager() gamestate_manager.change_gamestate('main_menu') # game loop running = True while running: # handle events for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == Globals.SONG_END: Sound_manager.change_song() else: gamestate_manager.handle_event(event) # update game logic and check if game should continue running if clock.get_time() <= 100: # pause game while window is getting dragged running = running and gamestate_manager.update(clock.get_time()) # draw screen.fill(pygame.Color("black")) gamestate_manager.draw(screen) pygame.display.flip() # show fps in title bar and limit fps pygame.display.set_caption(f"RETRO-PONG - {round(clock.get_fps())} fps") clock.tick(Config.MAX_FPS)
def __init__(self): super().__init__("game") self.next_gamestate = None self.rackets = [ Racket((Config.MARGIN, Config.SCREENHEIGHT // 2 - Config.RACKET_HEIGHT // 2), (Config.RACKET_WIDTH, Config.RACKET_HEIGHT)), Racket((Config.SCREENWIDTH - Config.MARGIN - Config.RACKET_WIDTH, Config.SCREENHEIGHT // 2 - Config.RACKET_HEIGHT // 2), (Config.RACKET_WIDTH, Config.RACKET_HEIGHT)) ] self.ball = Ball(self.rackets, (Config.SCREENWIDTH // 2 - Config.BALL_SIZE // 2, Config.SCREENHEIGHT // 2 - Config.BALL_SIZE // 2)) self.ball.on_boundary_hit.append( self.score_point) # subscribe to event to update the score self.ball.on_boundary_hit.append( self.play_boundary_sound) # play boundary sound self.ball.on_racket_hit.append( self.increase_speed ) # subscribe to event to increase speed on every racket hit self.ball.on_racket_hit.append( self.play_racket_sound) # play racket bounce sound self.racket_controls = [] for i, control in enumerate(Config.controls): if control == "ai": self.racket_controls.append( Control_ai(self.rackets[i], self.ball)) # nerf ai players - otherwise they are too strong self.rackets[i].max_speed = Config.AI_PLAYER_SPEED elif control == "mouse": self.racket_controls.append(Control_mouse(self.rackets[i])) self.rackets[ i].max_speed = Config.MOUSE_SPEED # higher speed for mouse input elif control == "keyboard": self.racket_controls.append(Control_keyboard(self.rackets[i])) self.info_text = Text_widget( (Config.SCREENWIDTH // 2, Config.SCREENHEIGHT - Config.MARGIN), "ESC zum Beenden", color_normal=pygame.Color(100, 100, 100), text_justify="center", vertical_align="top") self.score_widgets = [ Text_widget( (Config.MARGIN + Config.RACKET_WIDTH * 2, Config.MARGIN), "0"), Text_widget( (Config.SCREENWIDTH - Config.MARGIN - Config.RACKET_WIDTH * 2, Config.MARGIN), "0", text_justify="right") ] self.countdown = Config.START_COUNTDOWN self.countdown_text = Text_widget( (Config.SCREENWIDTH // 2, Config.SCREENHEIGHT // 2), "", text_justify="center", vertical_align="center") self.gui = GUI_group() self.gui.add(self.score_widgets[0], self.score_widgets[1], self.info_text, self.countdown_text) Sound_manager.change_song()
def play_racket_sound(self, sender, racket): Sound_manager.play("racket")
def play_boundary_sound(self, sender, boundary): if boundary == "top" or boundary == "bottom": Sound_manager.play("click") else: if self.get_winner() is None: Sound_manager.play("fail")
def _button_get_focus(self, sender): self.text_widget.color = self.color_focussed Sound_manager.play("menu_move")
def click(self): if str(type(self)) == "<class 'button_widget.Button_widget'>": Sound_manager.play("menu_select") self._message_event_listeners("on_click")