aboutsummaryrefslogtreecommitdiff
path: root/src/ghost.py
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-05-08 23:02:03 +0300
committeromagdy7 <omar.professional8777@gmail.com>2023-05-08 23:02:03 +0300
commitaaf0194f9b5d93bd6612bc0b419c4b8f89b4aa21 (patch)
tree5c8950e0cb7adb4782e35bb26a0aa8242ea71398 /src/ghost.py
parent241e41892a10d3913c63935a8f9e14306e8a73cd (diff)
downloadMacpan-aaf0194f9b5d93bd6612bc0b419c4b8f89b4aa21.tar.xz
Macpan-aaf0194f9b5d93bd6612bc0b419c4b8f89b4aa21.zip
Added a simple Wining screen when the user collects all the food
Diffstat (limited to 'src/ghost.py')
-rw-r--r--src/ghost.py19
1 files changed, 16 insertions, 3 deletions
diff --git a/src/ghost.py b/src/ghost.py
index fc3adfe..c05eb1b 100644
--- a/src/ghost.py
+++ b/src/ghost.py
@@ -1,4 +1,5 @@
import pygame
+import time
import random
import math
from util import get_sprites
@@ -34,7 +35,6 @@ class Ghost():
return abs(next_pos[0] - tx) + abs(next_pos[1] - ty)
# checks if the current position of pacman is either a dot, big dot or free
-
def is_valid(self, maze, x, y):
if x >= 0 and x < 30: # Necessary to make portals work
is_dot = maze[y][x] == Map.D
@@ -46,6 +46,9 @@ class Ghost():
def get_default_tile(self):
return (75, 75)
+ def get_intial_tile(self):
+ return (12 * 30 + 15, 12 * 30 + 15)
+
# checks collision with pacman and obstacles returns false if there is
# a collision and true otherwise
@@ -63,6 +66,17 @@ class Ghost():
return True
+ def check_pacman_collision(self, game_state):
+ if game_state.pacman.powerup and abs(game_state.pacman.x - self.x) <= 30 and abs(game_state.pacman.y - self.y) <= 30:
+ initial_position = self.get_intial_tile()
+ time.sleep(1)
+ game_state.score += 200
+ self.x = initial_position[0]
+ self.y = initial_position[1]
+ elif not game_state.pacman.powerup and abs(game_state.pacman.x - self.x) <= 30 and abs(game_state.pacman.y - self.y) <= 30:
+ if abs(game_state.pacman.x - self.x) <= 30 and abs(game_state.pacman.y - self.y) <= 30:
+ game_state.is_pacman_alive = False
+
def get_next_move(self, game_state, screen):
default_tile = self.get_default_tile()
@@ -110,8 +124,7 @@ class Ghost():
return min_idx
def move(self, game_state, screen):
- if abs(game_state.pacman.x - self.x) <= 15 and abs(game_state.pacman.y - self.y) <= 15:
- game_state.is_pacman_alive = False
+ self.check_pacman_collision(game_state)
min_idx = self.get_next_move(game_state, screen)
new_dx = dx[min_idx] * self.speed
new_dy = dy[min_idx] * self.speed