def menu_page(self): bg = pg.image.load('./assets/bg.jpg') bg = pg.transform.scale(bg, (self.win_width, self.win_height)) new_game_button = Button('NEW GAME', (100, 150, 250, 50), self.game_page) new_game_button.config(hover=(25, 25, 25)) play_online_button = Button('PLAY ONLINE', (100, 225, 250, 50), lambda: self.game_page(online=True)) play_online_button.config(hover=(25, 25, 25)) quit_game_button = Button('QUIT', (100, 300, 250, 50), self.quit) quit_game_button.config(hover=(25, 25, 25)) while True: self.win.blit(bg, (0, 0)) new_game_button.draw(self.win) play_online_button.draw(self.win) quit_game_button.draw(self.win) for event in pg.event.get(): if event.type == pg.QUIT: self.quit() if event.type == pg.MOUSEBUTTONDOWN: new_game_button.collide(event.pos) play_online_button.collide(event.pos) quit_game_button.collide(event.pos) mouse = pg.mouse.get_pos() new_game_button.mouseover(self.win, mouse) play_online_button.mouseover(self.win, mouse) quit_game_button.mouseover(self.win, mouse) pg.display.update()
class NotePlatformerScene(Scene): def __init__(self, state): level = getattr(world1, f"Level_{state['level'] + 1}") self.state = state self.player = Player(position=Vector(*level.player)) self.platforms = Group() self.gems = Group() level_width = PS.PPU * ( max(map(lambda p: p[0] + p[2], level.platforms)) + PS.BLOB_SIZE) self.blobs = HorizontalScrollingGroup( self.player, (GS.SCREEN_WIDTH, GS.SCREEN_HEIGHT), (level_width, GS.SCREEN_HEIGHT), PS.SCROLL_MARGIN * PS.PPU) for x, y, width in level.platforms: platform = Platform(width=width, position=Vector(x, y)) self.blobs.add(platform) self.platforms.add(platform) for x, y, note, winner in level.gems: gem = Gem(note, winner=winner, position=Vector(x, y)) self.blobs.add(gem) self.gems.add(gem) if winner: goal_path = os.path.join("sounds", "long", f"{note}.wav") self.goal_sound = pygame.mixer.Sound(goal_path) self.channels = {} self.greeting = TextBox( "Your task is to find the gem which emits a certain sound.\nYou " "can listen to the sound by clicking on this box. You can also " "listen to it during the game by clicking on the \"Goal\" button " "in the corner.\nClick outside the box to start.", bgcolor=PS.TEXT_BGCOLOR, max_size=(GS.SCREEN_WIDTH * 0.6, GS.SCREEN_HEIGHT * 0.6), style=PS.TEXT_STYLE) self.started = False self.sound_btn = Button("Target sound", Vector(50, 50), Vector(10, 5)) def update(self): if not self.started: return self.blobs.update() # Handle player-platform collisions. for platform in pygame.sprite.spritecollide(self.player, self.platforms, False): self.player.collide(platform) if self.player.rect.bottom > GS.SCREEN_HEIGHT: self.quit(win=False) # Play sound near gems. for gem in self.gems: self.player.listen_to(gem) # Handle collisions with gems. for gem in pygame.sprite.spritecollide(self.player, self.gems, False): # End condition. self.quit(win=gem.winner) def handle_events(self, events): for event in events: # Handle interaction with greeting box. if not self.started: if event.type == pygame.MOUSEBUTTONUP: pos = pygame.mouse.get_pos() if self.greeting.rect.collidepoint(pos): self.goal_sound.play() else: self.started = True continue # Handle navigation key presses. if event.type == pygame.KEYDOWN: if event.key in PS.LEFT_KEYS: self.player.go_left() elif event.key in PS.RIGHT_KEYS: self.player.go_right() elif event.key in PS.JUMP_KEYS: if self.player.can_jump(self.platforms): self.player.jump() elif event.type == pygame.KEYUP: if event.key in PS.LEFT_KEYS: self.player.stop_left() elif event.key in PS.RIGHT_KEYS: self.player.stop_right() elif event.key in PS.JUMP_KEYS: self.player.stop_jump() # Handle clicking sound button. elif event.type == pygame.MOUSEBUTTONUP: if self.sound_btn.rect.collidepoint(pygame.mouse.get_pos()): self.goal_sound.play() def render(self, screen): screen.fill(PS.BG_COLOR) self.blobs.draw(screen) self.sound_btn.draw(screen) if not self.started: overlay = Surface(screen.get_size(), pygame.SRCALPHA) overlay.fill(PS.OVERLAY_COLOR) screen.blit(overlay, (0, 0)) self.greeting.draw(screen) def quit(self, win): for gem in self.gems: gem.sound.stop() if win: self.state["level_progress"][self.state["world"]] = ( self.state["level"] + 1) next_scene = menus.WinScene if win else menus.LoseScene self.manager.go_to(next_scene(self.state))
class Menu: def __init__(self, players, window, playerChar=Char.EMPTY): self.__buttons = [[[], [], []], [[], []]] self.__buttons[0][0] = Button("Person", gameConfig['width'] * 0.06, gameConfig['height'] * 0.4, (255, 128, 0), 30, gameConfig['width'] * 0.25, 60) self.__buttons[0][1] = Button("Random Bot", gameConfig['width'] * 0.37, gameConfig['height'] * 0.4, (128, 0, 128), 30, gameConfig['width'] * 0.25, 60) self.__buttons[0][2] = Button("Smart Bot", gameConfig['width'] * 0.68, gameConfig['height'] * 0.4, (128, 128, 128), 30, gameConfig['width'] * 0.25, 60) self.__buttons[1][0] = Button("X", gameConfig['width'] * 0.2, gameConfig['height'] * 0.6, fontsConfig['xColor'], 50, 150, 100) self.__buttons[1][1] = Button("O", gameConfig['width'] * 0.6, gameConfig['height'] * 0.6, fontsConfig['oColor'], 50, 150, 100) self.__finishButton = Button("Continue", gameConfig['width'] * 0.7, gameConfig['height'] * 0.9, (0, 100, 0), 20, gameConfig['width'] * 0.25, 40) self.__nameRect = pygame.Rect(gameConfig['width'] * 0.25, gameConfig['height'] * 0.25, gameConfig['width'] * 0.5, 32) self.__id = 1 self.__playerName = "" self.__playerChar = playerChar self.__players = players self.__running = True self.__window = window def update(self): for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_BACKSPACE: self.__playerName = self.__playerName[0:-1] else: if len(self.__playerName ) <= 10 and event.key != pygame.K_RETURN: self.__playerName += event.unicode if event.type == pygame.MOUSEBUTTONDOWN and pygame.mouse.get_pressed( )[0]: pos = pygame.mouse.get_pos() if self.__id == 1: for i in range(0, 2): if self.__buttons[1][i].click(pos): self.__buttons[1][(i + 1) % 2].setIsClicked(False) self.__buttons[1][i].setIsClicked(True) for i in range(0, 3): if self.__buttons[0][i].click(pos): for button in self.__buttons[0]: button.setIsClicked(False) self.__buttons[0][i].setIsClicked(True) if self.__finishButton.click(pos): self.createPlayer() def draw(self): self.__window.fill(gameConfig['screenColor']) #Draw title welcomeText = fontsConfig['bigFont'].render("Welcome to Tic Tac Toe!", True, fontsConfig['titleColor']) playerText = fontsConfig['mediumFont'].render( "Build Player " + str(self.__id) + ":", True, fontsConfig['textColor']) self.__window.blit(welcomeText, (gameConfig['width'] * 0.16, 0)) self.__window.blit( playerText, (gameConfig['width'] * 0.1, gameConfig['height'] * 0.1)) # Draw name nameText = fontsConfig['mediumFont'].render("Name:", 1, fontsConfig['titleColor']) self.__window.blit( nameText, (gameConfig['width'] * 0.2, gameConfig['height'] * 0.17)) texto = fontsConfig['smallFont'].render(self.__playerName, 1, (4, 15, 133)) pygame.draw.rect(self.__window, (255, 255, 255), self.__nameRect) self.__window.blit(texto, (self.__nameRect.x + 5, self.__nameRect.y + 5)) #DrawButtons if self.__id == 1: for i in range(0, 2): self.__buttons[1][i].draw(self.__window) for i in range(0, 3): self.__buttons[0][i].draw(self.__window) self.__finishButton.draw(self.__window) pygame.display.update() def createPlayer(self): if self.__id == 1: if self.__buttons[1][0].getIsClicked(): self.__playerChar = Char.X elif self.__buttons[1][1].getIsClicked(): self.__playerChar = Char.O else: return if self.__buttons[0][0].getIsClicked(): self.__players.append(Player(self.__playerName, self.__playerChar)) elif self.__buttons[0][1].getIsClicked(): self.__players.append( TicTacToeBot(self.__playerName, self.__playerChar, BotType.RANDOM)) elif self.__buttons[0][2].getIsClicked(): self.__players.append( TicTacToeBot(self.__playerName, self.__playerChar, BotType.MINIMAX)) else: return if self.__id == 1: self.moveToPlayer2() else: self.__running = False def moveToPlayer2(self): self.__id = 2 for i in range(0, 3): self.__buttons[0][i].setIsClicked(False) self.__finishButton = Button("Start Game", gameConfig['width'] * 0.7, gameConfig['height'] * 0.9, (0, 100, 0), 20, gameConfig['width'] * 0.25, 40) self.__playerName = "" if self.__playerChar == Char.X: self.__playerChar = Char.O else: self.__playerChar = Char.X def isWindowRunning(self): return self.__running def getPlayers(self): return self.__players