aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-04-16 19:24:24 +0200
committeromagdy7 <omar.professional8777@gmail.com>2023-04-16 19:24:24 +0200
commit16e402a3a4bd82c0eefe50e31d6613f5f7a99a02 (patch)
tree736e22432bee393dab1ea5c99e83e92ff68e642a
parentb673be7fc9e94a5a8245797be6a62547c4efa579 (diff)
downloadMacpan-16e402a3a4bd82c0eefe50e31d6613f5f7a99a02.tar.xz
Macpan-16e402a3a4bd82c0eefe50e31d6613f5f7a99a02.zip
Added code for blinky and pinky
-rw-r--r--src/Game.py18
-rw-r--r--src/Ghost.py14
-rw-r--r--src/blinky.py8
-rw-r--r--src/pinky.py78
4 files changed, 100 insertions, 18 deletions
diff --git a/src/Game.py b/src/Game.py
index e4784c9..0ba8c79 100644
--- a/src/Game.py
+++ b/src/Game.py
@@ -1,6 +1,8 @@
from pygame.mouse import get_pressed
import Player
import Ghost
+from pinky import Pinky
+from blinky import Blinky
from Direction import DIRECTION
import settings as Settings
import map as Map
@@ -25,8 +27,8 @@ class Game():
'../assets/pacman_left_sprite.png').convert_alpha()
player = Player.Player(sprite_sheet)
- blinky = Ghost.Ghost("red", 75, 75)
- pinky = Ghost.Ghost("pink", 27 * 30, 30 * 30 + 15)
+ blinky = Blinky(75, 75)
+ pinky = Pinky(27 * 30, 30 * 30 + 15)
inky = Ghost.Ghost("orange", 75, 30 * 30 + 15)
clyde = Ghost.Ghost("cyan", 27 * 30 + 15, 75)
@@ -152,16 +154,16 @@ class Game():
player.y += dy
- blinky.move(maze.maze, (player.x, player.y))
- pinky.move(maze.maze, (player.x, player.y))
- inky.move(maze.maze, (player.x, player.y))
- clyde.move(maze.maze, (player.x, player.y))
+ blinky.move(maze.maze, player)
+ pinky.move(maze.maze, player)
+ # inky.move(maze.maze, player)
+ # clyde.move(maze.maze, player)
maze.draw_map(screen)
player.draw(screen, counter)
blinky.draw(screen)
pinky.draw(screen)
- inky.draw(screen)
- clyde.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 d4bc683..17f5ed0 100644
--- a/src/Ghost.py
+++ b/src/Ghost.py
@@ -42,7 +42,7 @@ class Ghost():
return True
- def get_next_move(self, pacman_pos, maze):
+ def get_next_move(self, pacman, maze):
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
@@ -58,26 +58,20 @@ class Ghost():
forbidden = 0
if self.last_move == 3:
forbidden = 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), pacman_pos)
+ ret[i] = self.heuristic((nx, ny), (pacman.x, pacman.y))
min_idx = ret.index(min(ret))
return min_idx
-
-
-
-
-
- def move(self, maze, player_pos):
- min_idx = self.get_next_move(player_pos, maze)
+ def move(self, maze, pacman):
+ min_idx = self.get_next_move(pacman, maze)
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
new_dx = dx[min_idx] * self.speed
diff --git a/src/blinky.py b/src/blinky.py
new file mode 100644
index 0000000..d30bc52
--- /dev/null
+++ b/src/blinky.py
@@ -0,0 +1,8 @@
+from Ghost import Ghost
+
+class Blinky(Ghost):
+ def __init__(self, x, y):
+ super().__init__("red", x, y)
+
+
+
diff --git a/src/pinky.py b/src/pinky.py
new file mode 100644
index 0000000..b512bb9
--- /dev/null
+++ b/src/pinky.py
@@ -0,0 +1,78 @@
+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, x, y):
+ super().__init__("pink", x, y)
+
+
+
+ def in_bounds(self, pos):
+ return pos[0] >= 0 and pos[1] >= 0 and pos[0] < settings.width and pos[1] < settings.height
+
+
+ def get_four_tiles_ahead_of_pacman(self, pacman):
+ if pacman.direction == DIRECTION.UP:
+ new_target = (pacman.x - 30 * 4, 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 * 4)
+ 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 * 4, 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 * 4, pacman.y)
+ if self.in_bounds(new_target):
+ return new_target
+ else:
+ return (pacman.x, pacman.y)
+
+ @override
+ def get_next_move(self, target, maze):
+ 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_four_tiles_ahead_of_pacman(target)
+ print((target.x, target.y), new_target)
+
+ 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)
+
+ min_idx = ret.index(min(ret))
+ return min_idx
+
+
+
+
+
+