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