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/inky.py | 55 ++++++++++++++----------------------------------------- 1 file changed, 14 insertions(+), 41 deletions(-) (limited to 'src/inky.py') diff --git a/src/inky.py b/src/inky.py index 2f886a6..e1c44b0 100644 --- a/src/inky.py +++ b/src/inky.py @@ -1,44 +1,17 @@ import math +from typing_extensions import override from direction import DIRECTION +from settings import settings +import pygame from ghost import Ghost + class Inky(Ghost): def __init__(self, sprite_sheet, x, y): - super().__init__(sprite_sheet, "cyan", x, y) - + super().__init__(sprite_sheet, "orange", x, y) - # def get_intermediate_tile(self, pacman): - # if pacman.direction == DIRECTION.UP: - # new_target = (pacman.x - 30 * 2, pacman.y - 30 * 4) - # if self.in_bounds(new_target): - # return new_target - # else: - # return (pacman.x, pacman.y) - # elif pacman.direction == DIRECTION.DOWN: - # new_target = (pacman.x, pacman.y + 30 * 2) - # if self.in_bounds(new_target): - # return new_target - # else: - # return (pacman.x, pacman.y) - # elif pacman.direction == DIRECTION.RIGHT: - # new_target = (pacman.x + 30 * 2, pacman.y) - # if self.in_bounds(new_target): - # return new_target - # else: - # return (pacman.x, pacman.y) - # elif pacman.direction == DIRECTION.LEFT: - # new_target = (pacman.x - 30 * 2, pacman.y) - # if self.in_bounds(new_target): - # return new_target - # else: - # return (pacman.x, pacman.y) - # - # def get_vector_blinky_it(self, blinky, pacman): - # it = self.get_intermediate_tile(pacman) - # return (it[0] - blinky.x, it[1], blinky.y) - # # @override - # def get_next_move(self, target, maze, screen): + # def get_next_move(self, target, maze, screen, blinky): # dx = [1, 0, -1, 0] # dy = [0, 1, 0, -1] # @@ -55,19 +28,19 @@ class Inky(Ghost): # if self.last_move == 3: # forbidden = 1 # - # new_target = self.get_intermediate_tile(target) - # pygame.draw.circle(screen, self.color, (new_target[0], new_target[1]), 15) - # + # new_target = self.get_target(target, blinky) + # + # if settings.debug: + # pygame.draw.line(screen, self.color, (new_target), + # (blinky.x, blinky.y), 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), new_target[0], new_target[1]) + # ret[i] = self.heuristic( + # (nx, ny), new_target[0], new_target[1]) # # min_idx = ret.index(min(ret)) # return min_idx - # - # - # - # -- cgit v1.2.3 From c7c473177086399a8fb97936b4c3c2b67a43fce0 Mon Sep 17 00:00:00 2001 From: omagdy7 Date: Mon, 8 May 2023 13:07:48 +0300 Subject: Finished inky's algorithm and also added an option in settings to disable audio --- src/inky.py | 102 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 68 insertions(+), 34 deletions(-) (limited to 'src/inky.py') diff --git a/src/inky.py b/src/inky.py index e1c44b0..86ac156 100644 --- a/src/inky.py +++ b/src/inky.py @@ -10,37 +10,71 @@ class Inky(Ghost): def __init__(self, sprite_sheet, x, y): super().__init__(sprite_sheet, "orange", x, y) - # @override - # def get_next_move(self, target, maze, screen, blinky): - # 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 - # - # new_target = self.get_target(target, blinky) - # - # if settings.debug: - # pygame.draw.line(screen, self.color, (new_target), - # (blinky.x, blinky.y), 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), new_target[0], new_target[1]) - # - # min_idx = ret.index(min(ret)) - # return min_idx + def get_intermediate_tile(self, pacman): + if pacman.direction == DIRECTION.UP: + new_target = (pacman.x - 30 * 2, pacman.y - 30 * 2) + if self.in_bounds(new_target): + return new_target + else: + return (pacman.x, pacman.y) + elif pacman.direction == DIRECTION.DOWN: + new_target = (pacman.x, pacman.y + 30 * 2) + if self.in_bounds(new_target): + return new_target + else: + return (pacman.x, pacman.y) + elif pacman.direction == DIRECTION.RIGHT: + new_target = (pacman.x + 30 * 2, pacman.y) + if self.in_bounds(new_target): + return new_target + else: + return (pacman.x, pacman.y) + elif pacman.direction == DIRECTION.LEFT: + new_target = (pacman.x - 30 * 2, pacman.y) + if self.in_bounds(new_target): + return new_target + else: + return (pacman.x, pacman.y) + + def get_target(self, inter_tile, blinky): + target = (inter_tile[0] - (blinky.x - inter_tile[0]), + inter_tile[1] - (blinky.y - inter_tile[1])) + return target + + @override + def get_next_move(self, target, maze, screen, blinky): + 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 + + inter_tile = self.get_intermediate_tile(target) + target = self.get_target(inter_tile, blinky) + + # y = mx + c + + if settings.debug: + pygame.draw.line(screen, self.color, (target), + (self.x, self.y), 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), target[0], target[1]) + + min_idx = ret.index(min(ret)) + 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/inky.py | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src/inky.py') diff --git a/src/inky.py b/src/inky.py index 86ac156..070faa0 100644 --- a/src/inky.py +++ b/src/inky.py @@ -1,6 +1,7 @@ import math from typing_extensions import override from direction import DIRECTION +from mode import MODE from settings import settings import pygame from ghost import Ghost @@ -36,6 +37,10 @@ class Inky(Ghost): else: return (pacman.x, pacman.y) + @override + def get_default_tile(self): + return (2 * 30 + 15, 30 * 30 + 15) + def get_target(self, inter_tile, blinky): target = (inter_tile[0] - (blinky.x - inter_tile[0]), inter_tile[1] - (blinky.y - inter_tile[1])) @@ -43,6 +48,8 @@ class Inky(Ghost): @override def get_next_move(self, target, maze, screen, blinky): + default_tile = self.get_default_tile() + dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] @@ -73,8 +80,20 @@ class Inky(Ghost): 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), target[0], target[1]) + if self.mode == MODE.SCATTERED: + ret[i] = self.heuristic( + (nx, ny), default_tile[0], default_tile[1]) + else: + ret[i] = self.heuristic( + (nx, ny), target[0], target[1]) + + min_h = min(ret) - min_idx = ret.index(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/inky.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/inky.py') diff --git a/src/inky.py b/src/inky.py index 070faa0..1989048 100644 --- a/src/inky.py +++ b/src/inky.py @@ -1,4 +1,5 @@ import math +import random from typing_extensions import override from direction import DIRECTION from mode import MODE @@ -47,7 +48,7 @@ class Inky(Ghost): return target @override - def get_next_move(self, target, maze, screen, blinky): + def get_next_move(self, pacman, maze, screen, blinky): default_tile = self.get_default_tile() dx = [1, 0, -1, 0] @@ -66,9 +67,15 @@ class Inky(Ghost): if self.last_move == 3: forbidden = 1 - inter_tile = self.get_intermediate_tile(target) + inter_tile = self.get_intermediate_tile(pacman) target = self.get_target(inter_tile, blinky) + rand_pos = (0, 0) + + if pacman.powerup: + self.mode = MODE.FRIGHETENED + rand_pos = random.randint(0, 900), random.randint(0, 990) + # y = mx + c if settings.debug: @@ -83,7 +90,10 @@ class Inky(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: ret[i] = self.heuristic( (nx, ny), target[0], target[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/inky.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/inky.py') diff --git a/src/inky.py b/src/inky.py index 1989048..1f5bb8b 100644 --- a/src/inky.py +++ b/src/inky.py @@ -43,8 +43,8 @@ class Inky(Ghost): return (2 * 30 + 15, 30 * 30 + 15) def get_target(self, inter_tile, blinky): - target = (inter_tile[0] - (blinky.x - inter_tile[0]), - inter_tile[1] - (blinky.y - inter_tile[1])) + target = (max(inter_tile[0] - (blinky.x - inter_tile[0]) % 900, 0), + max(inter_tile[1] - (blinky.y - inter_tile[1]) % 990, 0)) return target @override @@ -76,7 +76,8 @@ class Inky(Ghost): self.mode = MODE.FRIGHETENED rand_pos = random.randint(0, 900), random.randint(0, 990) - # y = mx + c + if pacman.powerup is False and self.mode == MODE.FRIGHETENED: + self.mode = MODE.CHASING if settings.debug: pygame.draw.line(screen, self.color, (target), -- 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/inky.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/inky.py') diff --git a/src/inky.py b/src/inky.py index 1f5bb8b..840b60b 100644 --- a/src/inky.py +++ b/src/inky.py @@ -48,7 +48,7 @@ class Inky(Ghost): return target @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] @@ -67,16 +67,16 @@ class Inky(Ghost): if self.last_move == 3: forbidden = 1 - inter_tile = self.get_intermediate_tile(pacman) - target = self.get_target(inter_tile, blinky) + inter_tile = self.get_intermediate_tile(game_state.pacman) + target = self.get_target(inter_tile, game_state.blinky) 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 if settings.debug: @@ -87,7 +87,7 @@ class Inky(Ghost): 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): + if self.check_collision(nx, ny, 30, 30, game_state.map.maze): if self.mode == MODE.SCATTERED: ret[i] = self.heuristic( (nx, ny), default_tile[0], default_tile[1]) -- 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/inky.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/inky.py') diff --git a/src/inky.py b/src/inky.py index 840b60b..2b48ede 100644 --- a/src/inky.py +++ b/src/inky.py @@ -40,7 +40,11 @@ class Inky(Ghost): @override def get_default_tile(self): - return (2 * 30 + 15, 30 * 30 + 15) + return (27 * 30 + 15, 2 * 30 + 15) + + @override + def get_intial_tile(self): + return (13 * 30 + 15, 12 * 30 + 15) def get_target(self, inter_tile, blinky): target = (max(inter_tile[0] - (blinky.x - inter_tile[0]) % 900, 0), -- 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/inky.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/inky.py') diff --git a/src/inky.py b/src/inky.py index 2b48ede..be2e62c 100644 --- a/src/inky.py +++ b/src/inky.py @@ -76,7 +76,7 @@ class Inky(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) @@ -101,6 +101,10 @@ class Inky(Ghost): elif self.mode == MODE.CHASING: ret[i] = self.heuristic( (nx, ny), target[0], target[1]) + elif self.mode == MODE.EATEN: + pos = self.get_intial_tile() + self.x = pos[0] + self.y = pos[1] min_h = min(ret) -- cgit v1.2.3