From 7f501d21772d96756a851421690db08387df3c26 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Sun, 7 May 2023 11:32:10 +0300 Subject: Finished clyde algorithm and added a debug mode to run the program --- src/ghost.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'src/ghost.py') diff --git a/src/ghost.py b/src/ghost.py index e810bfe..301645b 100644 --- a/src/ghost.py +++ b/src/ghost.py @@ -4,19 +4,20 @@ from util import get_sprites from settings import settings import map as Map -dx = [1, 0, -1, 0] # right, down, left, up +dx = [1, 0, -1, 0] # right, down, left, up 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): + 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 # this represents the direction based on the dx, dy arrays + self.last_move = 3 # this represents the direction based on the dx, dy arrays self.speed = 3 def in_bounds(self, pos): @@ -26,67 +27,69 @@ class Ghost(): 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): - if x >= 0 and x < 30: + if x >= 0 and x < 30: # Necessary to make portals work 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) return True - # 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): + if not self.is_valid(maze, x, y): return False return True - - def get_next_move(self, pacman, maze, screen): + def get_next_move(self, pacman, maze, screen, blinky): ret = len(dx) * [math.inf] forbidden = inv_dir[self.last_move] - + for i in range(len(dx)): nx = self.x + dx[i] * self.speed ny = self.y + dy[i] * self.speed if self.check_collision(nx, ny, 30, 30, maze): if i != forbidden: ret[i] = self.heuristic((nx, ny), pacman.x, pacman.y) + if settings.debug: + pygame.draw.line(screen, self.color, (pacman.x, pacman.y), + (self.x, self.y), 1) min_h = min(ret) + + # Favour going up when there is a conflict if min_h == ret[3] and min_h != math.inf: return 3 min_idx = ret.index(min_h) return min_idx - - def move(self, maze, pacman, screen, game_over): + def move(self, maze, pacman, screen, game_over, blinky): if abs(pacman.x - self.x) <= 15 and abs(pacman.y - self.y) <= 15: game_over[0] = True - min_idx = self.get_next_move(pacman, maze, screen) + min_idx = self.get_next_move(pacman, maze, screen, blinky) new_dx = dx[min_idx] * self.speed new_dy = dy[min_idx] * self.speed self.x += new_dx self.y += new_dy - self.x %= 900 + self.x %= 900 # The logic of the portal self.last_move = min_idx def draw(self, screen): radius = 30 // 2 - pos = (self.x - radius , self.y - radius) - image = pygame.transform.scale(self.sprite[sprite_sheet[self.last_move]], (40, 40)) + pos = (self.x - radius, self.y - radius) + image = pygame.transform.scale( + self.sprite[sprite_sheet[self.last_move]], (40, 40)) screen.blit(image, pos) -- cgit v1.2.3