diff options
| author | omagdy7 <omar.professional8777@gmail.com> | 2023-04-19 05:20:26 +0200 |
|---|---|---|
| committer | omagdy7 <omar.professional8777@gmail.com> | 2023-04-19 05:20:26 +0200 |
| commit | 4fe339692233290a5d074533b4f4ebea668f8832 (patch) | |
| tree | 75e56e2e4a4fc767d17d0aa9ac651c9f69a60030 /src/Ghost.py | |
| parent | 7e4ef8be6fdce53ad01cacec3a9a9ed157d161b7 (diff) | |
| download | Macpan-4fe339692233290a5d074533b4f4ebea668f8832.tar.xz Macpan-4fe339692233290a5d074533b4f4ebea668f8832.zip | |
Fixed a bug in pacman position not updating proberly in the heueristic function
Diffstat (limited to 'src/Ghost.py')
| -rw-r--r-- | src/Ghost.py | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/Ghost.py b/src/Ghost.py index a26de62..668c943 100644 --- a/src/Ghost.py +++ b/src/Ghost.py @@ -1,7 +1,6 @@ -import pygame import math from util import get_sprites -from Direction import DIRECTION +from settings import settings import map as Map class Ghost(): @@ -13,10 +12,12 @@ class Ghost(): self.last_move = 3 self.speed = 3 - def heuristic(self, pacman_pos, next_pos): - return abs(next_pos[0] - pacman_pos[0]) + abs(next_pos[1] - pacman_pos[1]) - + def in_bounds(self, pos): + (x, y) = pos + return (x >= 0) and (y >= 0) and (x < settings.width - 30) and (y < settings.height) + 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 @@ -38,7 +39,7 @@ class Ghost(): py = ny + direct_y[i] * 14 x = px // gx y = py // gy - if not self.is_valid(maze, x, y): + if not self.in_bounds((px, py)) or not self.is_valid(maze, x, y): return False return True @@ -66,7 +67,7 @@ class 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), (pacman.x, pacman.y)) + ret[i] = self.heuristic((nx, ny), pacman.x, pacman.y) min_idx = ret.index(min(ret)) return min_idx @@ -85,4 +86,11 @@ class Ghost(): def draw(self, screen): radius = 30 // 2 pos = (self.x - radius , self.y - radius) - screen.blit(self.sprite[0], pos) + if self.last_move == 0: + screen.blit(self.sprite[2], pos) + elif self.last_move == 1: + screen.blit(self.sprite[0], pos) + elif self.last_move == 2: + screen.blit(self.sprite[3], pos) + elif self.last_move == 3: + screen.blit(self.sprite[1], pos) |
