aboutsummaryrefslogtreecommitdiff
path: root/src/ghost.py
diff options
context:
space:
mode:
authoromagdy7 <omar.professional8777@gmail.com>2023-05-08 14:47:07 +0300
committeromagdy7 <omar.professional8777@gmail.com>2023-05-08 14:47:07 +0300
commit72aeff07de251f66c579405f0aecb0b9c4d4cfac (patch)
treef8cd713cb9bd7df73297e110eaf8baad2236b48b /src/ghost.py
parentc7c473177086399a8fb97936b4c3c2b67a43fce0 (diff)
downloadMacpan-72aeff07de251f66c579405f0aecb0b9c4d4cfac.tar.xz
Macpan-72aeff07de251f66c579405f0aecb0b9c4d4cfac.zip
Added scattered mode for the ghosts
Diffstat (limited to 'src/ghost.py')
-rw-r--r--src/ghost.py20
1 files changed, 18 insertions, 2 deletions
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