78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
from enum import Enum
|
||
from random import random, randint
|
||
|
||
from pygame import Surface, draw, Rect, image
|
||
|
||
from engine.entity import Entity
|
||
from engine.resources import res
|
||
from engine.components.collide_rect import CollideRect
|
||
from engine.components.sprite import Sprite
|
||
|
||
from .common import S
|
||
from .pacdot import PacDot
|
||
|
||
|
||
class Direction(Enum):
|
||
UP = 1
|
||
DOWN = 2
|
||
LEFT = 3
|
||
RIGHT = 4
|
||
|
||
|
||
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,
|
||
cb=self.on_col)
|
||
self.add(self.phys)
|
||
self.script = Ghost.script
|
||
self.x = x
|
||
self.y = y
|
||
self.direction = Direction(randint(1, 4))
|
||
|
||
def on_col(self, c):
|
||
if not c.solid:
|
||
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):
|
||
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
|