diff --git a/scenes/game_objects/ghost.py b/scenes/game_objects/ghost.py index 0258a5c..5b96f7b 100644 --- a/scenes/game_objects/ghost.py +++ b/scenes/game_objects/ghost.py @@ -3,6 +3,7 @@ from random import random, randint from pygame import Rect, image from engine.entity import Entity +from engine.game import Game from engine.scene_manager import SceneManager from engine.resources import res from engine.components.collide_rect import CollideRect @@ -55,44 +56,51 @@ class Ghost(Entity): return def script(self): - # graph = SceneManager().scene.entities['level'].graph + graph = SceneManager().scene.entities['level'].graph - # cur_node = graph[self.y//S][self.x//S] - # # print(self.y//S, self.x//S) - # pacman = SceneManager().scene.entities['pacman'] - # x_dist = pacman.x - self.x - # y_dist = pacman.y - self.y - # s = 1 - # p = self.find_shortest_path(graph[pacman.y//S][pacman.x//S]) - # # print([x[1] for x in p]) - # cur_dir = p[0][1] - # if cur_dir == Direction.UP: - # self.phys.vx = 0 - # self.phys.vy = -1 - # elif cur_dir == Direction.DOWN: - # self.phys.vx = 0 - # self.phys.vy = 1 - # elif cur_dir == Direction.LEFT: - # self.phys.vx = -1 - # self.phys.vy = 0 - # else: - # self.phys.vx = 1 - # self.phys.vy = 0 + if self.x % S != 0 or self.y % S != 0: + return - if self.direction == Direction.UP: - self.phys.vy = -Ghost.SPEED + cur_node = graph[self.y//S][self.x//S] + # print(self.y//S, self.x//S) + pacman = SceneManager().scene.entities['pacman'] + x_dist = pacman.x - self.x + y_dist = pacman.y - self.y + s = 1 + # print(pacman.y//S, pacman.x//S) + p = self.find_shortest_path(graph[pacman.y//S][pacman.x//S]) + # print([x[1] for x in p]) + cur_dir = p[0][1] + if cur_dir == Direction.UP: self.phys.vx = 0 - elif self.direction == Direction.DOWN: - self.phys.vy = Ghost.SPEED + self.phys.vy = -1 + elif cur_dir == Direction.DOWN: self.phys.vx = 0 - elif self.direction == Direction.LEFT: - self.phys.vx = -Ghost.SPEED + self.phys.vy = 1 + elif cur_dir == Direction.LEFT: + self.phys.vx = -1 self.phys.vy = 0 - elif self.direction == Direction.RIGHT: - self.phys.vx = Ghost.SPEED + else: + self.phys.vx = 1 self.phys.vy = 0 + # if self.direction == Direction.UP: + # self.phys.vy = -Ghost.SPEED + # self.phys.vx = 0 + # elif self.direction == Direction.DOWN: + # self.phys.vy = Ghost.SPEED + # self.phys.vx = 0 + # elif self.direction == Direction.LEFT: + # self.phys.vx = -Ghost.SPEED + # self.phys.vy = 0 + # elif self.direction == Direction.RIGHT: + # self.phys.vx = Ghost.SPEED + # self.phys.vy = 0 + def find_shortest_path(self, final_node): + if final_node is None: + print('oups') + exit(1) graph = SceneManager().scene.entities['level'].graph cur_node = graph[self.y//S][self.x//S] children = cur_node.children diff --git a/scenes/game_objects/level.py b/scenes/game_objects/level.py index 2704006..9b20fcc 100644 --- a/scenes/game_objects/level.py +++ b/scenes/game_objects/level.py @@ -46,6 +46,10 @@ class Level(Entity): self.graph = [[None for x in range(w)] for y in range(h)] for x, y in product(range(w), range(h)): col = desc.get_at((x, y)) + + if col in ((0, 0, 0), (255, 0, 0), (139, 139, 139), + (193, 193, 193)): + self.graph[y][x] = Node() if col == (0, 0, 255): self.add(CollideRect(Rect(x * S, y * S, S, S))) self.surf.blit(wall, (x * S, y * S)) @@ -55,9 +59,6 @@ class Level(Entity): pc.y = y * S self.scene.add(pc) PacDot.tot += 1 - self.graph[y][x] = Node() - elif col == (0, 0, 0) or col == (255, 0, 0): - self.graph[y][x] = Node() elif col == (193, 193, 193): pc = EnsmallmentDot() pc.x = x * S diff --git a/scenes/lvl0.py b/scenes/lvl0.py index 59375a8..de8aa9d 100644 --- a/scenes/lvl0.py +++ b/scenes/lvl0.py @@ -11,9 +11,9 @@ class Lvl0(Scene): self.add(Level('lvl0.png')) self.add(PacMan(S, S)) self.add(Ghost(S*6, S*2)) - # self.add(Ghost(S*6, S*13)) - # self.add(Ghost(S*18, S*13)) - # self.add(Ghost(S*8, S*23)) + self.add(Ghost(S*6, S*13)) + self.add(Ghost(S*18, S*13)) + self.add(Ghost(S*8, S*23)) super().load()