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 From 72aeff07de251f66c579405f0aecb0b9c4d4cfac Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 14:47:07 +0300 Subject: Added scattered mode for the ghosts --- src/ghost.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'src/ghost.py') diff --git a/src/ghost.py b/src/ghost.py index 301645b..43c0c4a 100644 --- a/src/ghost.py +++ b/src/ghost.py @@ -2,6 +2,7 @@ import pygame import math from util import get_sprites from settings import settings +from mode import MODE import map as Map dx = [1, 0, -1, 0] # right, down, left, up @@ -19,6 +20,7 @@ class Ghost(): self.color = color self.last_move = 3 # this represents the direction based on the dx, dy arrays self.speed = 3 + self.mode = MODE.SCATTERED def in_bounds(self, pos): (x, y) = pos @@ -37,7 +39,12 @@ class Ghost(): 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 get_default_tile(self): + return (75, 75) + + # 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] @@ -54,6 +61,8 @@ class Ghost(): def get_next_move(self, pacman, maze, screen, blinky): + default_tile = self.get_default_tile() + ret = len(dx) * [math.inf] forbidden = inv_dir[self.last_move] @@ -63,7 +72,11 @@ class Ghost(): 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 self.mode == MODE.SCATTERED: + ret[i] = self.heuristic( + (nx, ny), default_tile[0], default_tile[1]) + else: + 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) @@ -73,6 +86,9 @@ class Ghost(): # Favour going up when there is a conflict if min_h == ret[3] and min_h != math.inf: return 3 + # Favour going down than sideways when there is a conflict + if min_h == ret[1] and min_h != math.inf: + return 1 min_idx = ret.index(min_h) return min_idx -- cgit v1.2.3 From 9621f880a03337a8a252cf5cd3993d5a1a29969c Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 17:59:55 +0300 Subject: Added frightened mode --- src/ghost.py | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) (limited to 'src/ghost.py') diff --git a/src/ghost.py b/src/ghost.py index 43c0c4a..cfd3068 100644 --- a/src/ghost.py +++ b/src/ghost.py @@ -1,8 +1,10 @@ import pygame +import random import math from util import get_sprites from settings import settings from mode import MODE +from direction import DIRECTION import map as Map dx = [1, 0, -1, 0] # right, down, left, up @@ -16,6 +18,8 @@ class Ghost(): def __init__(self, sprite_sheet, color, x, y): self.x = x self.y = y + self.sprite_sheet = sprite_sheet + self.name = "blinky" self.sprite = get_sprites(sprite_sheet) self.color = color self.last_move = 3 # this represents the direction based on the dx, dy arrays @@ -67,6 +71,14 @@ class Ghost(): forbidden = inv_dir[self.last_move] + rand_pos = (0, 0) + + if pacman.powerup: + self.mode = MODE.FRIGHETENED + pacman.sprite = get_sprites(pygame.image.load( + '../assets/blinky.png').convert_alpha()) + rand_pos = random.randint(0, 900), random.randint(0, 990) + for i in range(len(dx)): nx = self.x + dx[i] * self.speed ny = self.y + dy[i] * self.speed @@ -75,8 +87,11 @@ class Ghost(): if self.mode == MODE.SCATTERED: ret[i] = self.heuristic( (nx, ny), default_tile[0], default_tile[1]) - else: + elif self.mode == MODE.CHASING: ret[i] = self.heuristic((nx, ny), pacman.x, pacman.y) + elif self.mode == MODE.FRIGHETENED: + ret[i] = self.heuristic( + (nx, ny), rand_pos[0], rand_pos[1]) if settings.debug: pygame.draw.line(screen, self.color, (pacman.x, pacman.y), (self.x, self.y), 1) @@ -103,9 +118,23 @@ class Ghost(): self.x %= 900 # The logic of the portal self.last_move = min_idx - def draw(self, screen): + def draw(self, screen, powerup, counter): radius = 30 // 2 pos = (self.x - radius, self.y - radius) - image = pygame.transform.scale( - self.sprite[sprite_sheet[self.last_move]], (40, 40)) - screen.blit(image, pos) + if powerup: + self.sprite = get_sprites(pygame.image.load( + f'../assets/pacman_{self.color}.png').convert_alpha()) + image = pygame.transform.scale(self.sprite[counter // 5], (35, 35)) + if self.last_move == DIRECTION.UP.value: + screen.blit(pygame.transform.rotate(image, 270), pos) + elif self.last_move == DIRECTION.DOWN.value: + screen.blit(pygame.transform.rotate(image, 90), pos) + elif self.last_move == DIRECTION.RIGHT.value: + screen.blit(pygame.transform.flip(image, True, False), pos) + elif self.last_move == DIRECTION.LEFT.value: + screen.blit(image, pos) + else: + self.sprite = get_sprites(self.sprite_sheet) + image = pygame.transform.scale( + self.sprite[sprite_sheet[self.last_move]], (40, 40)) + screen.blit(image, pos) -- cgit v1.2.3 From 1584574267bae0ec4b0096ace7a7cbbe08787c05 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 18:32:04 +0300 Subject: Fixed a bug in inky's algorithms and also now the ghost reset to chase mode after pacman finishes his powerup --- src/ghost.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/ghost.py') diff --git a/src/ghost.py b/src/ghost.py index cfd3068..e785afc 100644 --- a/src/ghost.py +++ b/src/ghost.py @@ -75,10 +75,11 @@ class Ghost(): if pacman.powerup: self.mode = MODE.FRIGHETENED - pacman.sprite = get_sprites(pygame.image.load( - '../assets/blinky.png').convert_alpha()) rand_pos = random.randint(0, 900), random.randint(0, 990) + if pacman.powerup is False and self.mode == MODE.FRIGHETENED: + self.mode = MODE.CHASING + for i in range(len(dx)): nx = self.x + dx[i] * self.speed ny = self.y + dy[i] * self.speed @@ -119,6 +120,7 @@ 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: -- cgit v1.2.3 From d610718c10e310c75126593624ecaaaa2233b371 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 20:08:41 +0300 Subject: Added a simple GameOver Screen --- src/ghost.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/ghost.py') 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: -- cgit v1.2.3 From 241e41892a10d3913c63935a8f9e14306e8a73cd Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 21:40:21 +0300 Subject: Made a Singeltion class GameState containg the current state of the game --- src/ghost.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/ghost.py') diff --git a/src/ghost.py b/src/ghost.py index e4b4cf1..fc3adfe 100644 --- a/src/ghost.py +++ b/src/ghost.py @@ -63,7 +63,7 @@ class Ghost(): return True - def get_next_move(self, pacman, maze, screen, blinky): + def get_next_move(self, game_state, screen): default_tile = self.get_default_tile() @@ -73,28 +73,29 @@ class Ghost(): rand_pos = (0, 0) - if pacman.powerup: + if game_state.pacman.powerup: self.mode = MODE.FRIGHETENED rand_pos = random.randint(0, 900), random.randint(0, 990) - if pacman.powerup is False and self.mode == MODE.FRIGHETENED: + if game_state.pacman.powerup is False and self.mode == MODE.FRIGHETENED: self.mode = MODE.CHASING 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 self.check_collision(nx, ny, 30, 30, game_state.map.maze): if i != forbidden: if self.mode == MODE.SCATTERED: ret[i] = self.heuristic( (nx, ny), default_tile[0], default_tile[1]) elif self.mode == MODE.CHASING: - ret[i] = self.heuristic((nx, ny), pacman.x, pacman.y) + ret[i] = self.heuristic( + (nx, ny), game_state.pacman.x, game_state.pacman.y) elif self.mode == MODE.FRIGHETENED: ret[i] = self.heuristic( (nx, ny), rand_pos[0], rand_pos[1]) if settings.debug: - pygame.draw.line(screen, self.color, (pacman.x, pacman.y), + pygame.draw.line(screen, self.color, (game_state.pacman.x, game_state.pacman.y), (self.x, self.y), 1) min_h = min(ret) @@ -108,10 +109,10 @@ class Ghost(): min_idx = ret.index(min_h) return min_idx - def move(self, maze, pacman, screen, is_pacman_alive, blinky): - if abs(pacman.x - self.x) <= 15 and abs(pacman.y - self.y) <= 15: - is_pacman_alive[0] = False - min_idx = self.get_next_move(pacman, maze, screen, blinky) + 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 + min_idx = self.get_next_move(game_state, screen) new_dx = dx[min_idx] * self.speed new_dy = dy[min_idx] * self.speed self.x += new_dx -- cgit v1.2.3 From aaf0194f9b5d93bd6612bc0b419c4b8f89b4aa21 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 23:02:03 +0300 Subject: Added a simple Wining screen when the user collects all the food --- src/ghost.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/ghost.py') 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 -- cgit v1.2.3 From 5b2e6b7e660865b6db9bfb61e1b1d0fecc536858 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 23:18:26 +0300 Subject: Added EatenMode --- src/ghost.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/ghost.py') diff --git a/src/ghost.py b/src/ghost.py index c05eb1b..8568ff4 100644 --- a/src/ghost.py +++ b/src/ghost.py @@ -3,6 +3,7 @@ import time import random import math from util import get_sprites +from timer import Timer from settings import settings from mode import MODE from direction import DIRECTION @@ -25,6 +26,7 @@ class Ghost(): self.color = color self.last_move = 3 # this represents the direction based on the dx, dy arrays self.speed = 3 + self.timer = None self.mode = MODE.SCATTERED def in_bounds(self, pos): @@ -69,6 +71,8 @@ class Ghost(): 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() + self.mode = MODE.EATEN + self.timer = Timer(2 * 1000) time.sleep(1) game_state.score += 200 self.x = initial_position[0] @@ -87,7 +91,7 @@ class Ghost(): rand_pos = (0, 0) - if game_state.pacman.powerup: + if game_state.pacman.powerup and self.mode != MODE.EATEN: self.mode = MODE.FRIGHETENED rand_pos = random.randint(0, 900), random.randint(0, 990) @@ -108,6 +112,10 @@ class Ghost(): elif self.mode == MODE.FRIGHETENED: ret[i] = self.heuristic( (nx, ny), rand_pos[0], rand_pos[1]) + elif self.mode == MODE.EATEN: + pos = self.get_intial_tile() + self.x = pos[0] + self.y = pos[1] if settings.debug: pygame.draw.line(screen, self.color, (game_state.pacman.x, game_state.pacman.y), (self.x, self.y), 1) @@ -134,6 +142,11 @@ class Ghost(): self.last_move = min_idx def draw(self, screen, powerup, counter): + if self.timer is not None: + elapsed_time = pygame.time.get_ticks() - self.timer.start + if elapsed_time > self.timer.duration: + self.mode = MODE.CHASING + radius = 30 // 2 pos = (self.x - radius, self.y - radius) if powerup: -- cgit v1.2.3