diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2023-04-19 06:07:55 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2023-04-19 06:07:55 +0200 |
| commit | 68f17e149ad49e63b05bb53ced64c74f4a372c4f (patch) | |
| tree | 7978ef1e000aeb1d066715199b900ba2b62a3a05 /src/ghost.py | |
| parent | 4fe339692233290a5d074533b4f4ebea668f8832 (diff) | |
| download | Macpan-68f17e149ad49e63b05bb53ced64c74f4a372c4f.tar.xz Macpan-68f17e149ad49e63b05bb53ced64c74f4a372c4f.zip | |
Renamed some files and did a bit of refactoring
Diffstat (limited to 'src/ghost.py')
| -rw-r--r-- | src/ghost.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/ghost.py b/src/ghost.py new file mode 100644 index 0000000..98b7deb --- /dev/null +++ b/src/ghost.py @@ -0,0 +1,82 @@ +import math +from util import get_sprites +from settings import settings +import map as Map + +dx = [1, 0, -1, 0] +dy = [0, 1, 0, -1] + +inv_dir = [2, 3, 0, 1] +sprite_sheet = [2, 0, 3, 1] + +class Ghost(): + def __init__(self,sprite_sheet, color, x, y): + self.x = x + self.y = y + self.sprite = get_sprites(sprite_sheet) + self.color = color + self.last_move = 3 + self.speed = 3 + + def in_bounds(self, pos): + (x, y) = pos + return (x >= 0) and (y >= 0) and (x < settings.width - 30) and (y < settings.height) + + def heuristic(self, next_pos, tx, ty): + 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): + 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.in_bounds((px, py)) or not self.is_valid(maze, x, y): + return False + + return True + + + def get_next_move(self, pacman, maze): + + ret = len(dx) * [math.inf] + + forbidden = inv_dir[self.last_move] + + 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.x, pacman.y) + + min_idx = ret.index(min(ret)) + return min_idx + + + def move(self, maze, pacman): + min_idx = self.get_next_move(pacman, maze) + 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 - radius , self.y - radius) + screen.blit(self.sprite[sprite_sheet[self.last_move]], pos) |
