2019-12-09 12:36:42 +01:00
|
|
|
|
from random import random, randint
|
|
|
|
|
|
2019-12-09 15:34:37 +01:00
|
|
|
|
from pygame import Rect, image
|
2019-12-09 12:36:42 +01:00
|
|
|
|
|
|
|
|
|
from engine.entity import Entity
|
2019-12-09 15:29:35 +01:00
|
|
|
|
from engine.scene_manager import SceneManager
|
2019-12-09 12:36:42 +01:00
|
|
|
|
from engine.resources import res
|
|
|
|
|
from engine.components.collide_rect import CollideRect
|
|
|
|
|
from engine.components.sprite import Sprite
|
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
from .common import S, Direction
|
2019-12-09 12:36:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Ghost(Entity):
|
|
|
|
|
SPEED = 1
|
|
|
|
|
|
|
|
|
|
def __init__(self, x, y):
|
|
|
|
|
super().__init__(self.__repr__())
|
|
|
|
|
sprite = Sprite(image.load(res('ghost.png')).convert(), 2)
|
|
|
|
|
self.add(sprite)
|
|
|
|
|
self.phys = CollideRect(Rect(self.x, self.y,
|
|
|
|
|
sprite.width, sprite.width),
|
|
|
|
|
static=False,
|
|
|
|
|
solid=False,
|
2019-12-09 15:29:35 +01:00
|
|
|
|
cb=self.cb)
|
2019-12-09 12:36:42 +01:00
|
|
|
|
self.add(self.phys)
|
|
|
|
|
self.script = Ghost.script
|
|
|
|
|
self.x = x
|
|
|
|
|
self.y = y
|
2019-12-09 15:29:35 +01:00
|
|
|
|
self.direction = Direction(randint(0, 3))
|
2019-12-09 12:36:42 +01:00
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
def cb(self, c):
|
2019-12-09 13:21:33 +01:00
|
|
|
|
if not c.solid:
|
2019-12-09 12:36:42 +01:00
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if c.parent.name == 'pacman':
|
|
|
|
|
print('Perdu !')
|
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
|
|
if self.direction == Direction.UP:
|
|
|
|
|
self.direction = Direction.RIGHT if random() < .5 \
|
|
|
|
|
else Direction.LEFT
|
|
|
|
|
return
|
|
|
|
|
if self.direction == Direction.RIGHT:
|
|
|
|
|
self.direction = Direction.DOWN if random() < .5 \
|
|
|
|
|
else Direction.UP
|
|
|
|
|
return
|
|
|
|
|
if self.direction == Direction.DOWN:
|
|
|
|
|
self.direction = Direction.LEFT if random() < .5 \
|
|
|
|
|
else Direction.RIGHT
|
|
|
|
|
return
|
|
|
|
|
if self.direction == Direction.LEFT:
|
|
|
|
|
self.direction = Direction.UP if random() < .5 \
|
|
|
|
|
else Direction.DOWN
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
def script(self):
|
2019-12-09 16:11:51 +01:00
|
|
|
|
graph = SceneManager().scene.entities['level'].graph
|
2019-12-09 15:29:35 +01:00
|
|
|
|
|
2019-12-09 16:11:51 +01:00
|
|
|
|
if self.x % S != 0 or self.y % S != 0:
|
|
|
|
|
return
|
2019-12-09 15:29:35 +01:00
|
|
|
|
|
2019-12-09 18:15:05 +01:00
|
|
|
|
# cur_node = graph[self.y//S][self.x//S]
|
2019-12-09 16:11:51 +01:00
|
|
|
|
# print(self.y//S, self.x//S)
|
|
|
|
|
pacman = SceneManager().scene.entities['pacman']
|
2019-12-09 18:15:05 +01:00
|
|
|
|
# x_dist = pacman.x - self.x
|
|
|
|
|
# y_dist = pacman.y - self.y
|
|
|
|
|
# s = 1
|
2019-12-09 16:11:51 +01:00
|
|
|
|
# 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:
|
2019-12-09 12:36:42 +01:00
|
|
|
|
self.phys.vx = 0
|
2019-12-09 16:11:51 +01:00
|
|
|
|
self.phys.vy = -1
|
|
|
|
|
elif cur_dir == Direction.DOWN:
|
2019-12-09 12:36:42 +01:00
|
|
|
|
self.phys.vx = 0
|
2019-12-09 16:11:51 +01:00
|
|
|
|
self.phys.vy = 1
|
|
|
|
|
elif cur_dir == Direction.LEFT:
|
|
|
|
|
self.phys.vx = -1
|
2019-12-09 12:36:42 +01:00
|
|
|
|
self.phys.vy = 0
|
2019-12-09 16:11:51 +01:00
|
|
|
|
else:
|
|
|
|
|
self.phys.vx = 1
|
2019-12-09 12:36:42 +01:00
|
|
|
|
self.phys.vy = 0
|
2019-12-09 15:29:35 +01:00
|
|
|
|
|
2019-12-09 16:11:51 +01:00
|
|
|
|
# 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
|
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
def find_shortest_path(self, final_node):
|
2019-12-09 16:11:51 +01:00
|
|
|
|
if final_node is None:
|
|
|
|
|
print('oups')
|
|
|
|
|
exit(1)
|
2019-12-09 15:29:35 +01:00
|
|
|
|
graph = SceneManager().scene.entities['level'].graph
|
|
|
|
|
cur_node = graph[self.y//S][self.x//S]
|
|
|
|
|
children = cur_node.children
|
2019-12-09 16:26:54 +01:00
|
|
|
|
child_ok = list(filter(lambda c: c[0] == final_node, children))
|
|
|
|
|
if child_ok:
|
|
|
|
|
return [child_ok[0]]
|
2019-12-09 15:29:35 +01:00
|
|
|
|
trace = [cur_node] + [x[0] for x in children]
|
|
|
|
|
paths = [[x] for x in children]
|
|
|
|
|
while 1:
|
|
|
|
|
cur_len = len(paths)
|
|
|
|
|
for path in paths[:cur_len]:
|
|
|
|
|
for child in path[-1][0].children:
|
|
|
|
|
if child[0] is not None and child[0] not in trace:
|
|
|
|
|
trace.append(child[0])
|
|
|
|
|
paths.append(path + [child])
|
|
|
|
|
if child[0] == final_node:
|
|
|
|
|
return path
|
|
|
|
|
paths = paths[cur_len:]
|