aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-05-08 20:08:41 +0300
committeromagdy7 <omar.professional8777@gmail.com>2023-05-08 20:08:41 +0300
commitd610718c10e310c75126593624ecaaaa2233b371 (patch)
tree5e093b81f92d25e0f2df9e3a334e3f4521e3e3d3
parent1584574267bae0ec4b0096ace7a7cbbe08787c05 (diff)
downloadMacpan-d610718c10e310c75126593624ecaaaa2233b371.tar.xz
Macpan-d610718c10e310c75126593624ecaaaa2233b371.zip
Added a simple GameOver Screen
-rw-r--r--src/game.py51
-rw-r--r--src/ghost.py5
-rw-r--r--src/settings.py2
3 files changed, 46 insertions, 12 deletions
diff --git a/src/game.py b/src/game.py
index 8e4ce37..a904fad 100644
--- a/src/game.py
+++ b/src/game.py
@@ -17,6 +17,35 @@ class Game():
def __init__(self):
self.settings = settings
+ def show_gameover_screen(self, screen, game_over):
+ font = pygame.font.SysFont(None, 48)
+
+ # Render the "Game Over" text to a surface
+ game_over_text = font.render(
+ "Game Over. Press R to try again.", True, (255, 255, 255))
+
+ # Blit the "Game Over" text onto the screen
+ text_rect = game_over_text.get_rect(
+ center=(WIDTH/2, HEIGHT/2))
+ screen.blit(game_over_text, text_rect)
+
+ # 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
+ game_over[0] = False
+ quit_game = True # Set the flag to True to break out of both loops
+ break
+ elif event.type == pygame.QUIT:
+ game_over[0] = True
+ quit_game = True # Set the flag to True to break out of both loops
+ break
+
def run(self):
# Initialize Pygame
pygame.init()
@@ -68,7 +97,6 @@ class Game():
pygame.mixer.music.load('../assets/sfx/game_start.wav')
siren_sound = pygame.mixer.Sound('../assets/sfx/siren_1.wav')
- munch_sound = pygame.mixer.Sound('../assets/sfx/munch_1.wav')
if settings.sound:
pygame.mixer.music.play()
@@ -92,10 +120,12 @@ class Game():
tx = dx
ty = dy
+ is_pacman_alive = [True]
+
# Handling events
for event in pygame.event.get():
if event.type == pygame.QUIT:
- is_game_over = False
+ is_game_over[0] = True
elif event.type == pygame.KEYDOWN:
# Move the circle based on the pressed key
if event.key == pygame.K_w:
@@ -163,10 +193,10 @@ class Game():
player.x %= 900
# Move ghosts
- blinky.move(maze.maze, player, screen, is_game_over, blinky)
- pinky.move(maze.maze, player, screen, is_game_over, blinky)
- inky.move(maze.maze, player, screen, is_game_over, blinky)
- clyde.move(maze.maze, player, screen, is_game_over, blinky)
+ blinky.move(maze.maze, player, screen, is_pacman_alive, blinky)
+ pinky.move(maze.maze, player, screen, is_pacman_alive, blinky)
+ inky.move(maze.maze, player, screen, is_pacman_alive, blinky)
+ clyde.move(maze.maze, player, screen, is_pacman_alive, blinky)
# Draw the map on each frame
maze.draw_map(screen)
@@ -178,8 +208,13 @@ class Game():
inky.draw(screen, player.powerup, counter)
clyde.draw(screen, player.powerup, counter)
- # Update the screen
- pygame.display.flip()
+ if not is_pacman_alive[0]:
+ self.show_gameover_screen(
+ screen, is_game_over)
+ is_pacman_alive[0] = True
+ else:
+ # Update the screen
+ pygame.display.flip()
# Quit Pygame
pygame.quit()
diff --git a/src/ghost.py b/src/ghost.py
index e785afc..e4b4cf1 100644
--- a/src/ghost.py
+++ b/src/ghost.py
@@ -108,9 +108,9 @@ class Ghost():
min_idx = ret.index(min_h)
return min_idx
- def move(self, maze, pacman, screen, game_over, blinky):
+ def move(self, maze, pacman, screen, is_pacman_alive, blinky):
if abs(pacman.x - self.x) <= 15 and abs(pacman.y - self.y) <= 15:
- game_over[0] = True
+ is_pacman_alive[0] = False
min_idx = self.get_next_move(pacman, maze, screen, blinky)
new_dx = dx[min_idx] * self.speed
new_dy = dy[min_idx] * self.speed
@@ -120,7 +120,6 @@ class Ghost():
self.last_move = min_idx
def draw(self, screen, powerup, counter):
- print(f"{self.color} -> mode: {self.mode}")
radius = 30 // 2
pos = (self.x - radius, self.y - radius)
if powerup:
diff --git a/src/settings.py b/src/settings.py
index d1cc93c..a8dfa24 100644
--- a/src/settings.py
+++ b/src/settings.py
@@ -3,7 +3,7 @@ class Settings():
self.width = 900
self.height = 990
self.fps = 60
- self.debug = True
+ self.debug = False
self.sound = False