aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-05-07 11:32:10 +0300
committeromagdy7 <omar.professional8777@gmail.com>2023-05-07 11:32:10 +0300
commit7f501d21772d96756a851421690db08387df3c26 (patch)
tree39880a4410ab5defc74f0f27a8a6a9e1e5caa5fd /src
parent7194ca65ae23d96960fe7edb619efb100db0a49c (diff)
downloadMacpan-7f501d21772d96756a851421690db08387df3c26.tar.xz
Macpan-7f501d21772d96756a851421690db08387df3c26.zip
Finished clyde algorithm and added a debug mode to run the program
Diffstat (limited to 'src')
-rw-r--r--src/blinky.py4
-rw-r--r--src/clyde.py47
-rw-r--r--src/game.py28
-rw-r--r--src/ghost.py37
-rw-r--r--src/inky.py55
-rw-r--r--src/map.py124
-rw-r--r--src/pinky.py25
-rw-r--r--src/settings.py2
8 files changed, 185 insertions, 137 deletions
diff --git a/src/blinky.py b/src/blinky.py
index eee44a8..4c45644 100644
--- a/src/blinky.py
+++ b/src/blinky.py
@@ -1,8 +1,6 @@
from ghost import Ghost
+
class Blinky(Ghost):
def __init__(self, sprite_sheet, x, y):
super().__init__(sprite_sheet, "red", x, y)
-
-
-
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
diff --git a/src/game.py b/src/game.py
index 5b85c39..5a41f88 100644
--- a/src/game.py
+++ b/src/game.py
@@ -24,13 +24,15 @@ class Game():
screen = pygame.display.set_mode((WIDTH, HEIGHT))
# Sprite sheet for pacman
- sprite_sheet = pygame.image.load( '../assets/pacman_left_sprite.png').convert_alpha()
+ sprite_sheet = pygame.image.load(
+ '../assets/pacman_left_sprite.png').convert_alpha()
# Sprite sheets for the ghosts
- blinky_sprite = pygame.image.load('../assets/blinky.png').convert_alpha()
- pinky_sprite = pygame.image.load( '../assets/pinky.png').convert_alpha()
- clyde_sprite = pygame.image.load( '../assets/clyde.png').convert_alpha()
- inky_sprite = pygame.image.load( '../assets/inky.png').convert_alpha()
+ blinky_sprite = pygame.image.load(
+ '../assets/blinky.png').convert_alpha()
+ pinky_sprite = pygame.image.load('../assets/pinky.png').convert_alpha()
+ clyde_sprite = pygame.image.load('../assets/clyde.png').convert_alpha()
+ inky_sprite = pygame.image.load('../assets/inky.png').convert_alpha()
# our beautiful maze
maze = Map.Map()
@@ -41,7 +43,7 @@ class Game():
# Initialize the player and the ghosts
player = Player(sprite_sheet)
- blinky = Blinky(blinky_sprite,75, 75)
+ blinky = Blinky(blinky_sprite, 75, 75)
pinky = Pinky(pinky_sprite, 27 * 30, 30 * 30 + 15)
inky = Inky(inky_sprite, 75, 30 * 30 + 15)
clyde = Clyde(clyde_sprite, 27 * 30 + 15, 75)
@@ -63,7 +65,6 @@ class Game():
siren_sound.play(-1)
is_game_over = [False]
-
# Main game loop
while not is_game_over[0]:
# setting game fps
@@ -124,7 +125,6 @@ class Game():
tx = player.speed
ty = 0
-
# if tx and ty doesn't lead to colliding change the current dx and dy to them and other wise
# let pacman move in his previous direction
if player.check_collision(maze, tx, ty, TILE_WIDTH, TILE_HEIGHT):
@@ -145,12 +145,11 @@ class Game():
player.y += dy
player.x %= 900
-
# Move ghosts
- blinky.move(maze.maze, player, screen, is_game_over)
- pinky.move(maze.maze, player, screen, is_game_over)
- inky.move(maze.maze, player, screen, is_game_over)
- clyde.move(maze.maze, player, screen, is_game_over)
+ blinky.move(maze.maze, player, screen, is_game_over, blinky)
+ pinky.move(maze.maze, player, screen, is_game_over, blinky)
+ # inky.move(maze.maze, player, screen, is_game_over, blinky)
+ clyde.move(maze.maze, player, screen, is_game_over, blinky)
# Draw the map on each frame
maze.draw_map(screen)
@@ -159,10 +158,9 @@ class Game():
player.draw(screen, counter)
blinky.draw(screen)
pinky.draw(screen)
- inky.draw(screen)
+ # inky.draw(screen)
clyde.draw(screen)
-
# Update the screen
pygame.display.flip()
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)
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
- #
- #
- #
- #
diff --git a/src/map.py b/src/map.py
index 2ccfa1f..01c1258 100644
--- a/src/map.py
+++ b/src/map.py
@@ -3,17 +3,15 @@ import math
import settings as Settings
-H = 1
-V = 2
-D = 4
-BD = 8
-TR = 16
-TL = 32
-BL = 64
-BR = 128
-G = 256
-LP = 512
-RP = 1024
+H = 1 # Horitoznal wall
+V = 2 # Vertical wall
+D = 4 # Dot
+BD = 8 # Big Dot
+TR = 16 # TopRight wall
+TL = 32 # TopLeft wall
+BL = 64 # BottomLeft wall
+BR = 128 # BottomrRight wall
+G = 256 # Ghost
PI = math.pi
@@ -21,40 +19,73 @@ PI = math.pi
class Map():
def __init__(self):
self.maze = [
- [TL, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H,H, H, H, H, H, H, H, H, H, H, H, H, H, TR],
- [V, TL, H, H, H, H, H, H, H, H, H, H, H, H, TR, TL, H, H, H, H, H, H, H, H, H, H, H, H, TR, V],
- [V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V],
- [V, V, D, TL, H, H, TR, D, TL, H, H, H, TR, D, V, V, D, TL, H, H, H, TR, D, TL, H, H, TR, D, V, V],
- [V, V, BD, V, 0, 0, V, D, V, 0, 0, 0, V, D, V, V, D, V, 0, 0, 0, V, D, V, 0, 0, V, BD, V, V],
- [V, V, D, BL, H, H, BR, D, BL, H, H, H, BR, D, BL, BR, D, BL, H, H, H, BR, D, BL, H, H, BR, D, V, V],
- [V, V, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, V, V],
- [V, V, D, TL, H, H, TR, D, TL, TR, D, TL, H, H, H, H, H, H, TR, D, TL, TR, D, TL, H, H, TR, D, V, V],
- [V, V, D, BL, H, H, BR, D, V, V, D, BL, H, H, TR, TL, H, H, BR, D, V, V, D, BL, H, H, BR, D, V, V],
- [V, V, D, D, D, D, D, D, V, V, D, D, D, D, V, V, D, D, D, D, V, V, D, D, D, D, D, D, V, V],
- [V, BL, H, H, H, H, TR, D, V, BL, H, H, TR, 0, V, V, 0, TL, H, H, BR, V, D, TL, H, H, H, H, BR, V],
- [V, 0, 0, 0, 0, 0, V, D, V, TL, H, H, BR, 0, BL, BR, 0, BL, H, H, TR, V, D, V, 0, 0, 0, 0, 0, V],
- [V, 0, 0, 0, 0, 0, V, D, V, V, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, V, V, D, V, 0, 0, 0, 0, 0, V],
- [BR, 0, 0, 0, 0, 0, V, D, V, V, 0, TL, H, H, G, G, H, H, TR, 0, V, V, D, V, 0, 0, 0, 0, 0, BL],
- [H, H, H, H, H, H, BR, D, BL, BR, 0, V, 0, 0, 0, 0, 0, 0, V, 0, BL, BR, D, BL, H, H, H, H, H, H],
- [0, 0, 0, 0, 0, 0, 0, D, 0, 0, 0, V, 0, 0, 0, 0, 0, 0, V, 0, 0, 0, D, 0, 0, 0, 0, 0, 0, 0],
- [H, H, H, H, H, H, TR, D, TL, TR, 0, V, 0, 0, 0, 0, 0, 0, V, 0, TL, TR, D, TL, H, H, H, H, H, H],
- [TR, 0, 0, 0, 0, 0, V, D, V, V, 0, BL, H, H, H, H, H, H, BR, 0, V, V, D, V, 0, 0, 0, 0, 0, TL],
- [V, 0, 0, 0, 0, 0, V, D, V, V, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, V, V, D, V, 0, 0, 0, 0, 0, V],
- [V, 0, 0, 0, 0, 0, V, D, V, V, 0, TL, H, H, H, H, H, H, TR, 0, V, V, D, V, 0, 0, 0, 0, 0, V],
- [V, TL, H, H, H, H, BR, D, BL, BR, 0, BL, H, H, TR, TL, H, H, BR, 0, BL, BR, D, BL, H, H, H, H, TR, V],
- [V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V],
- [V, V, D, TL, H, H, TR, D, TL, H, H, H, TR, D, V, V, D, TL, H, H, H, TR, D, TL, H, H, TR, D, V, V],
- [V, V, D, BL, H, TR, V, D, BL, H, H, H, BR, D, BL, BR, D, BL, H, H, H, BR, D, V, TL, H, BR, D, V, V],
- [V, V, BD, D, D, V, V, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, V, V, D, D, BD, V, V],
- [V, BL, H, TR, D, V, V, D, TL, TR, D, TL, H, H, H, H, H, H, TR, D, TL, TR, D, V, V, D, TL, H, BR, V],
- [V, TL, H, BR, D, BL, BR, D, V, V, D, BL, H, H, TR, TL, H, H, BR, D, V, V, D, BL, BR, D, BL, H, TR, V],
- [V, V, D, D, D, D, D, D, V, V, D, D, D, D, V, V, D, D, D, D, V, V, D, D, D, D, D, D, V, V],
- [V, V, D, TL, H, H, H, H, BR, BL, H, H, TR, D, V, V, D, TL, H, H, BR, BL, H, H, H, H, TR, D, V, V],
- [V, V, D, BL, H, H, H, H, H, H, H, H, BR, D, BL, BR, D, BL, H, H, H, H, H, H, H, H, BR, D, V, V],
- [V, V, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, D, V, V],
- [V, BL, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, BR, V],
- [BL, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H, BR]
- ]
+ [TL, H, H, H, H, H, H, H, H, H, H, H, H, H, H, H,
+ H, H, H, H, H, H, H, H, H, H, H, H, H, TR],
+ [V, TL, H, H, H, H, H, H, H, H, H, H, H, H, TR,
+ TL, H, H, H, H, H, H, H, H, H, H, H, H, TR, V],
+ [V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V,
+ D, D, D, D, D, D, D, D, D, D, D, D, V, V],
+ [V, V, D, TL, H, H, TR, D, TL, H, H, H, TR, D, V, V,
+ D, TL, H, H, H, TR, D, TL, H, H, TR, D, V, V],
+ [V, V, BD, V, 0, 0, V, D, V, 0, 0, 0, V, D, V, V,
+ D, V, 0, 0, 0, V, D, V, 0, 0, V, BD, V, V],
+ [V, V, D, BL, H, H, BR, D, BL, H, H, H, BR, D, BL,
+ BR, D, BL, H, H, H, BR, D, BL, H, H, BR, D, V, V],
+ [V, V, D, D, D, D, D, D, D, D, D, D, D, D, D, D,
+ D, D, D, D, D, D, D, D, D, D, D, D, V, V],
+ [V, V, D, TL, H, H, TR, D, TL, TR, D, TL, H, H, H, H,
+ H, H, TR, D, TL, TR, D, TL, H, H, TR, D, V, V],
+ [V, V, D, BL, H, H, BR, D, V, V, D, BL, H, H, TR,
+ TL, H, H, BR, D, V, V, D, BL, H, H, BR, D, V, V],
+ [V, V, D, D, D, D, D, D, V, V, D, D, D, D, V, V,
+ D, D, D, D, V, V, D, D, D, D, D, D, V, V],
+ [V, BL, H, H, H, H, TR, D, V, BL, H, H, TR, 0, V, V,
+ 0, TL, H, H, BR, V, D, TL, H, H, H, H, BR, V],
+ [V, 0, 0, 0, 0, 0, V, D, V, TL, H, H, BR, 0, BL,
+ BR, 0, BL, H, H, TR, V, D, V, 0, 0, 0, 0, 0, V],
+ [V, 0, 0, 0, 0, 0, V, D, V, V, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, V, V, D, V, 0, 0, 0, 0, 0, V],
+ [BR, 0, 0, 0, 0, 0, V, D, V, V, 0, TL, H, H, G, G,
+ H, H, TR, 0, V, V, D, V, 0, 0, 0, 0, 0, BL],
+ [H, H, H, H, H, H, BR, D, BL, BR, 0, V, 0, 0, 0, 0,
+ 0, 0, V, 0, BL, BR, D, BL, H, H, H, H, H, H],
+ [0, 0, 0, 0, 0, 0, 0, D, 0, 0, 0, V, 0, 0, 0, 0,
+ 0, 0, V, 0, 0, 0, D, 0, 0, 0, 0, 0, 0, 0],
+ [H, H, H, H, H, H, TR, D, TL, TR, 0, V, 0, 0, 0, 0,
+ 0, 0, V, 0, TL, TR, D, TL, H, H, H, H, H, H],
+ [TR, 0, 0, 0, 0, 0, V, D, V, V, 0, BL, H, H, H, H,
+ H, H, BR, 0, V, V, D, V, 0, 0, 0, 0, 0, TL],
+ [V, 0, 0, 0, 0, 0, V, D, V, V, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, V, V, D, V, 0, 0, 0, 0, 0, V],
+ [V, 0, 0, 0, 0, 0, V, D, V, V, 0, TL, H, H, H, H,
+ H, H, TR, 0, V, V, D, V, 0, 0, 0, 0, 0, V],
+ [V, TL, H, H, H, H, BR, D, BL, BR, 0, BL, H, H, TR,
+ TL, H, H, BR, 0, BL, BR, D, BL, H, H, H, H, TR, V],
+ [V, V, D, D, D, D, D, D, D, D, D, D, D, D, V, V,
+ D, D, D, D, D, D, D, D, D, D, D, D, V, V],
+ [V, V, D, TL, H, H, TR, D, TL, H, H, H, TR, D, V, V,
+ D, TL, H, H, H, TR, D, TL, H, H, TR, D, V, V],
+ [V, V, D, BL, H, TR, V, D, BL, H, H, H, BR, D, BL,
+ BR, D, BL, H, H, H, BR, D, V, TL, H, BR, D, V, V],
+ [V, V, BD, D, D, V, V, D, D, D, D, D, D, D, D, D,
+ D, D, D, D, D, D, D, V, V, D, D, BD, V, V],
+ [V, BL, H, TR, D, V, V, D, TL, TR, D, TL, H, H, H, H,
+ H, H, TR, D, TL, TR, D, V, V, D, TL, H, BR, V],
+ [V, TL, H, BR, D, BL, BR, D, V, V, D, BL, H, H, TR,
+ TL, H, H, BR, D, V, V, D, BL, BR, D, BL, H, TR, V],
+ [V, V, D, D, D, D, D, D, V, V, D, D, D, D, V, V,
+ D, D, D, D, V, V, D, D, D, D, D, D, V, V],
+ [V, V, D, TL, H, H, H, H, BR, BL, H, H, TR, D, V, V,
+ D, TL, H, H, BR, BL, H, H, H, H, TR, D, V, V],
+ [V, V, D, BL, H, H, H, H, H, H, H, H, BR, D, BL,
+ BR, D, BL, H, H, H, H, H, H, H, H, BR, D, V, V],
+ [V, V, D, D, D, D, D, D, D, D, D, D, D, D, D, D,
+ D, D, D, D, D, D, D, D, D, D, D, D, V, V],
+ [V, BL, H, H, H, H, H, H, H, H, H, H, H, H, H, H,
+ H, H, H, H, H, H, H, H, H, H, H, H, BR, V],
+ [BL, H, H, H, H, H, H, H, H, H, H, H, H, H, H,
+ H, H, H, H, H, H, H, H, H, H, H, H, H, H, BR]
+ ]
self.dot_color = (255, 255, 255) # white
self.small_dot_radius = 4
self.big_dot_radius = 8
@@ -63,9 +94,6 @@ class Map():
self.line_horizontal = Settings.settings.width // len(self.maze[0])
self.line_stroke = 1
- def consturct_map(self):
- pass
-
def draw_wall(self, screen, flag, pos):
if flag & V:
pos1 = (pos[0] + self.line_vertical * 0.5, pos[1])
diff --git a/src/pinky.py b/src/pinky.py
index 42b5d91..191ff61 100644
--- a/src/pinky.py
+++ b/src/pinky.py
@@ -1,13 +1,14 @@
import pygame
from typing_extensions import override
from direction import DIRECTION
+from settings import settings
import math
from ghost import Ghost
+
class Pinky(Ghost):
def __init__(self, sprite_sheet, x, y):
- super().__init__(sprite_sheet,"pink", x, y)
-
+ super().__init__(sprite_sheet, "pink", x, y)
def get_four_tiles_ahead_of_pacman(self, pacman):
if pacman.direction == DIRECTION.UP:
@@ -36,7 +37,7 @@ class Pinky(Ghost):
return (pacman.x, pacman.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]
@@ -54,20 +55,20 @@ class Pinky(Ghost):
forbidden = 1
new_target = self.get_four_tiles_ahead_of_pacman(target)
- pygame.draw.circle(screen, self.color, (new_target[0], new_target[1]), 15)
-
+ if settings.debug:
+ pygame.draw.circle(screen, self.color,
+ (new_target[0], new_target[1]), 15)
+
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])
+ if settings.debug:
+ pygame.draw.line(screen, self.color, (new_target),
+ (self.x, self.y), 1)
min_idx = ret.index(min(ret))
return min_idx
-
-
-
-
-
-
diff --git a/src/settings.py b/src/settings.py
index 9f0f118..94eabd8 100644
--- a/src/settings.py
+++ b/src/settings.py
@@ -3,5 +3,7 @@ class Settings():
self.width = 900
self.height = 990
self.fps = 60
+ self.debug = True
+
settings = Settings()