From aaf0194f9b5d93bd6612bc0b419c4b8f89b4aa21 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 23:02:03 +0300 Subject: Added a simple Wining screen when the user collects all the food --- src/game.py | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 9 deletions(-) (limited to 'src/game.py') diff --git a/src/game.py b/src/game.py index 4787c00..a7efb67 100644 --- a/src/game.py +++ b/src/game.py @@ -11,16 +11,66 @@ class Game(): self.settings = settings def show_gameover_screen(self, screen, game_state, sprites): - font = pygame.font.SysFont(None, 48) + font = pygame.font.SysFont(None, 64) # Render the "Game Over" text to a surface - game_over_text = font.render( - "Game Over. Press R to try again.", True, (255, 255, 255)) + game_over_text_1 = font.render( + "Game Over", True, (255, 255, 255)) + game_over_text_2 = font.render( + "Press R to try again or Q to quit.", True, (255, 255, 255)) # Blit the "Game Over" text onto the screen - text_rect = game_over_text.get_rect( + text_rect_1 = game_over_text_1.get_rect( + center=(WIDTH/2, HEIGHT/2 - 75)) + text_rect_2 = game_over_text_2.get_rect( center=(WIDTH/2, HEIGHT/2)) - screen.blit(game_over_text, text_rect) + + screen.blit(game_over_text_1, text_rect_1) + screen.blit(game_over_text_2, text_rect_2) + + # Update the display + pygame.display.flip() + + quit_game = False # Initialize the flag variable + while not quit_game: + for event in pygame.event.get(): + if event.type == pygame.KEYDOWN and event.key == pygame.K_r: + # Reset the game and start again + # Add your own code here to reset the game state + self.reset_game(game_state, sprites) + game_state.game_over = False + quit_game = True # Set the flag to True to break out of both loops + break + elif event.type == pygame.KEYDOWN and event.key == pygame.K_q: + game_state.game_over = True + quit_game = True + break + elif event.type == pygame.QUIT: + game_state.game_over = True + quit_game = True # Set the flag to True to break out of both loops + break + + def show_wining_screen(self, screen, game_state, sprites): + font = pygame.font.SysFont(None, 64) + + # Render the "Game Over" text to a surface + wining_text_1 = font.render( + "Congratulation You Won!!", True, (255, 255, 255)) + wining_text_2 = font.render( + "Press R to play again or Q to quit", True, (255, 255, 255)) + + text_rect_1 = wining_text_1.get_rect( + center=(WIDTH/2, HEIGHT/2 - 75)) + text_rect_2 = wining_text_2.get_rect( + center=(WIDTH/2, HEIGHT/2)) + + # Blit the "Game Over" text onto the screen + text_rect_1 = wining_text_1.get_rect( + center=(WIDTH/2, HEIGHT/2)) + text_rect_2 = wining_text_2.get_rect( + center=(WIDTH/2, HEIGHT/2 + 100)) + screen.blit(wining_text_1, text_rect_1) + screen.blit(wining_text_2, text_rect_2) # Update the display pygame.display.flip() @@ -35,6 +85,10 @@ class Game(): game_state.game_over = False quit_game = True # Set the flag to True to break out of both loops break + elif event.type == pygame.KEYDOWN and event.key == pygame.K_q: + game_state.game_over = True + quit_game = True + break elif event.type == pygame.QUIT: game_state.game_over = True quit_game = True # Set the flag to True to break out of both loops @@ -62,7 +116,7 @@ class Game(): inky_sprite = pygame.image.load('../assets/inky.png').convert_alpha() sprites = [sprite_sheet, blinky_sprite, - pinky_sprite, clyde_sprite, inky_sprite] + pinky_sprite, inky_sprite, clyde_sprite] # Set the timer to trigger after 10,000 milliseconds (10 seconds) timer_event = pygame.USEREVENT + 1 @@ -155,7 +209,7 @@ class Game(): # if tx and ty doesn't lead to colliding change the current # dx and dy to them and other wise # let pacman move in his previous direction - if game_state.pacman.check_collision(game_state.map, tx, ty, TILE_WIDTH, TILE_HEIGHT): + if game_state.pacman.check_collision(game_state, tx, ty, TILE_WIDTH, TILE_HEIGHT): dx = tx dy = ty @@ -168,10 +222,10 @@ class Game(): elif dy > 0: game_state.pacman.direction = DIRECTION.DOWN - if game_state.pacman.check_collision(game_state.map, dx, dy, TILE_WIDTH, TILE_HEIGHT): + if game_state.pacman.check_collision(game_state, dx, dy, TILE_WIDTH, TILE_HEIGHT): game_state.pacman.x += dx game_state.pacman.y += dy - game_state.pacman.x %= 900 + game_state.pacman.x %= 900 # logic for portal # Move ghosts game_state.blinky.move(game_state, screen) @@ -189,6 +243,10 @@ class Game(): game_state.inky.draw(screen, game_state.pacman.powerup, counter) game_state.clyde.draw(screen, game_state.pacman.powerup, counter) + if game_state.food == 246: + self.show_wining_screen(screen, game_state, sprites) + + if not game_state.is_pacman_alive: self.show_gameover_screen( screen, game_state, sprites) @@ -198,4 +256,5 @@ class Game(): pygame.display.flip() # Quit Pygame + print(game_state.score) pygame.quit() -- cgit v1.2.3