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/clyde.py | 47 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'src/clyde.py') diff --git a/src/clyde.py b/src/clyde.py index 117f71a..95de2cb 100644 --- a/src/clyde.py +++ b/src/clyde.py @@ -1,9 +1,54 @@ from ghost import Ghost +import pygame +from settings import settings +from typing_extensions import override +import math + class Clyde(Ghost): def __init__(self, sprite_sheet, x, y): - super().__init__(sprite_sheet, "orange", x, y) + super().__init__(sprite_sheet, "cyan", x, y) + + def is_eight_tiles_away(self, pacman): + tile_width = 30 + dx = self.x - pacman.x + dy = self.y - pacman.y + return math.sqrt(dx * dx + dy * dy) <= tile_width * 8 + + @override + def get_next_move(self, pacman, maze, screen, blinky): + dx = [1, 0, -1, 0] # right, down, left, up + dy = [0, 1, 0, -1] + + inv_dir = [2, 3, 0, 1] + + ret = len(dx) * [math.inf] + bottom_left_corner = (2.5 * 30, (len(maze) - 1 - 1 - 0.5) * 30) + 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: + if self.is_eight_tiles_away(pacman): + ret[i] = self.heuristic( + (nx, ny), bottom_left_corner[0], bottom_left_corner[1]) + if settings.debug: + pygame.draw.line(screen, self.color, (bottom_left_corner), + (self.x, self.y), 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) + 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 -- 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/clyde.py | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) (limited to 'src/clyde.py') diff --git a/src/clyde.py b/src/clyde.py index 95de2cb..d17a583 100644 --- a/src/clyde.py +++ b/src/clyde.py @@ -1,6 +1,7 @@ from ghost import Ghost import pygame from settings import settings +from mode import MODE from typing_extensions import override import math @@ -15,8 +16,14 @@ class Clyde(Ghost): dy = self.y - pacman.y return math.sqrt(dx * dx + dy * dy) <= tile_width * 8 + @override + def get_default_tile(self): + return (27 * 30 + 15, 2 * 30 + 15) + @override def get_next_move(self, pacman, maze, screen, blinky): + default_tile = self.get_default_tile() + dx = [1, 0, -1, 0] # right, down, left, up dy = [0, 1, 0, -1] @@ -32,23 +39,30 @@ class Clyde(Ghost): ny = self.y + dy[i] * self.speed if self.check_collision(nx, ny, 30, 30, maze): if i != forbidden: - if self.is_eight_tiles_away(pacman): + if self.mode == MODE.SCATTERED: ret[i] = self.heuristic( - (nx, ny), bottom_left_corner[0], bottom_left_corner[1]) - if settings.debug: - pygame.draw.line(screen, self.color, (bottom_left_corner), - (self.x, self.y), 1) + (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) + if self.is_eight_tiles_away(pacman): + ret[i] = self.heuristic( + (nx, ny), bottom_left_corner[0], bottom_left_corner[1]) + if settings.debug: + pygame.draw.line(screen, self.color, (bottom_left_corner), + (self.x, self.y), 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) min_h = min(ret) # 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/clyde.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/clyde.py') diff --git a/src/clyde.py b/src/clyde.py index d17a583..140ce16 100644 --- a/src/clyde.py +++ b/src/clyde.py @@ -1,4 +1,5 @@ from ghost import Ghost +import random import pygame from settings import settings from mode import MODE @@ -34,6 +35,12 @@ class Clyde(Ghost): forbidden = inv_dir[self.last_move] + rand_pos = (0, 0) + + if pacman.powerup: + self.mode = MODE.FRIGHETENED + 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 @@ -42,7 +49,10 @@ class Clyde(Ghost): if self.mode == MODE.SCATTERED: ret[i] = self.heuristic( (nx, ny), default_tile[0], default_tile[1]) - else: + elif self.mode == MODE.FRIGHETENED: + ret[i] = self.heuristic( + (nx, ny), rand_pos[0], rand_pos[1]) + elif self.mode == MODE.CHASING: if self.is_eight_tiles_away(pacman): ret[i] = self.heuristic( (nx, ny), bottom_left_corner[0], bottom_left_corner[1]) -- 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/clyde.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/clyde.py') diff --git a/src/clyde.py b/src/clyde.py index 140ce16..7dcf54e 100644 --- a/src/clyde.py +++ b/src/clyde.py @@ -41,6 +41,9 @@ class Clyde(Ghost): self.mode = MODE.FRIGHETENED 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 -- 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/clyde.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/clyde.py') diff --git a/src/clyde.py b/src/clyde.py index 7dcf54e..e48a439 100644 --- a/src/clyde.py +++ b/src/clyde.py @@ -22,7 +22,7 @@ class Clyde(Ghost): return (27 * 30 + 15, 2 * 30 + 15) @override - def get_next_move(self, pacman, maze, screen, blinky): + def get_next_move(self, game_state, screen): default_tile = self.get_default_tile() dx = [1, 0, -1, 0] # right, down, left, up @@ -31,23 +31,24 @@ class Clyde(Ghost): inv_dir = [2, 3, 0, 1] ret = len(dx) * [math.inf] - bottom_left_corner = (2.5 * 30, (len(maze) - 1 - 1 - 0.5) * 30) + bottom_left_corner = ( + 2.5 * 30, (len(game_state.map.maze) - 1 - 1 - 0.5) * 30) forbidden = inv_dir[self.last_move] 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( @@ -56,7 +57,7 @@ class Clyde(Ghost): ret[i] = self.heuristic( (nx, ny), rand_pos[0], rand_pos[1]) elif self.mode == MODE.CHASING: - if self.is_eight_tiles_away(pacman): + if self.is_eight_tiles_away(game_state.pacman): ret[i] = self.heuristic( (nx, ny), bottom_left_corner[0], bottom_left_corner[1]) if settings.debug: @@ -64,9 +65,9 @@ class Clyde(Ghost): (self.x, self.y), 1) else: ret[i] = self.heuristic( - (nx, ny), pacman.x, pacman.y) + (nx, ny), game_state.pacman.x, game_state.pacman.y) 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) -- 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/clyde.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'src/clyde.py') diff --git a/src/clyde.py b/src/clyde.py index e48a439..755eafd 100644 --- a/src/clyde.py +++ b/src/clyde.py @@ -19,7 +19,11 @@ class Clyde(Ghost): @override def get_default_tile(self): - return (27 * 30 + 15, 2 * 30 + 15) + return (2 * 30 + 15, 30 * 30 + 15) + + @override + def get_intial_tile(self): + return (14 * 30 + 15, 12 * 30 + 15) @override def get_next_move(self, game_state, screen): @@ -45,6 +49,10 @@ class Clyde(Ghost): if game_state.pacman.powerup is False and self.mode == MODE.FRIGHETENED: self.mode = MODE.CHASING + if settings.debug: + pygame.draw.line(screen, self.color, (game_state.pacman.x, game_state.pacman.y), + (self.x, self.y), 1) + for i in range(len(dx)): nx = self.x + dx[i] * self.speed ny = self.y + dy[i] * 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/clyde.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/clyde.py') diff --git a/src/clyde.py b/src/clyde.py index 755eafd..2fd4aee 100644 --- a/src/clyde.py +++ b/src/clyde.py @@ -42,7 +42,7 @@ class Clyde(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) @@ -77,7 +77,10 @@ class Clyde(Ghost): if settings.debug: pygame.draw.line(screen, self.color, (game_state.pacman.x, game_state.pacman.y), (self.x, self.y), 1) - + elif self.mode == MODE.EATEN: + pos = self.get_intial_tile() + self.x = pos[0] + self.y = pos[1] min_h = min(ret) # Favour going up when there is a conflict -- cgit v1.2.3