aboutsummaryrefslogtreecommitdiff
path: root/src/Ghost.py
diff options
context:
space:
mode:
authorOmar Magdy <99906646+omagdy7@users.noreply.github.com>2023-04-16 17:50:36 +0200
committerGitHub <noreply@github.com>2023-04-16 17:50:36 +0200
commitb673be7fc9e94a5a8245797be6a62547c4efa579 (patch)
tree5983f58f8512ca7386932a1549302d3e9fd7f3ba /src/Ghost.py
parentaa2162d1c2c873161b08efa3affff8ac73e27824 (diff)
parent3634fcc4f9f62d40ea03ba890a3290e05ed20ed8 (diff)
downloadMacpan-b673be7fc9e94a5a8245797be6a62547c4efa579.tar.xz
Macpan-b673be7fc9e94a5a8245797be6a62547c4efa579.zip
Merge pull request #4 from omagdy7/Ghost
Added Ghosts with A* implementation
Diffstat (limited to 'src/Ghost.py')
-rw-r--r--src/Ghost.py95
1 files changed, 91 insertions, 4 deletions
diff --git a/src/Ghost.py b/src/Ghost.py
index 33086a1..d4bc683 100644
--- a/src/Ghost.py
+++ b/src/Ghost.py
@@ -1,7 +1,94 @@
+import pygame
+import math
+from Direction import DIRECTION
+import map as Map
+
class Ghost():
- def __init__(self):
- self.x = None
- self.y = None
+ def __init__(self, color, x, y):
+ self.x = x
+ self.y = y
+ self.color = color
+ self.last_move = 3
+ self.speed = 3
+
+ def heuristic(self, pacman_pos, next_pos):
+ return abs(next_pos[0] - pacman_pos[0]) + abs(next_pos[1] - pacman_pos[1])
+
+
+
+
+ # checks if the current position of pacman is either a dot, big dot or free
+ def is_valid(self, maze, x, y):
+ is_dot = maze[y][x] == Map.D
+ is_big_dot = maze[y][x] == Map.BD
+ is_free = maze[y][x] == 0
+ return (is_dot or is_free or is_big_dot)
+
+
+ # checks collision with pacman and obstacles returns false if there is a collision and true otherwise
+ def check_collision(self, nx, ny, gx, gy, maze):
+ direct_x = [1, 0, -1, 0, 1, 1, -1, -1]
+ direct_y = [0, 1, 0, -1, -1, 1, -1, 1]
+
+
+ for i in range(len(direct_x)):
+ px = nx + direct_x[i] * 14
+ py = ny + direct_y[i] * 14
+ x = px // gx
+ y = py // gy
+ if not self.is_valid(maze, x, y):
+ return False
+
+ return True
+
+
+ def get_next_move(self, pacman_pos, maze):
+ dx = [1, 0, -1, 0]
+ dy = [0, 1, 0, -1]
+
+ ret = len(dx) * [math.inf]
+
+ forbidden = 0
+
+ if self.last_move == 0:
+ forbidden = 2
+ if self.last_move == 1:
+ forbidden = 3
+ if self.last_move == 2:
+ forbidden = 0
+ if self.last_move == 3:
+ forbidden = 1
+
+
+ for i in range(len(dx)):
+ if i != forbidden:
+ nx = self.x + dx[i] * self.speed
+ ny = self.y + dy[i] * self.speed
+ if self.check_collision(nx, ny, 30, 30, maze):
+ ret[i] = self.heuristic((nx, ny), pacman_pos)
+
+ min_idx = ret.index(min(ret))
+ return min_idx
+
+
+
+
+
+
+
+ def move(self, maze, player_pos):
+ min_idx = self.get_next_move(player_pos, maze)
+ dx = [1, 0, -1, 0]
+ dy = [0, 1, 0, -1]
+ new_dx = dx[min_idx] * self.speed
+ new_dy = dy[min_idx] * self.speed
+ self.x += new_dx
+ self.y += new_dy
+ self.last_move = min_idx
+
+ def draw(self, screen):
+ radius = 30 // 2
+ pos = (self.x , self.y)
+ pygame.draw.circle(screen, self.color, pos, radius)
- # def heuristic(self):