2019-12-09 12:36:42 +01:00
|
|
|
|
from pygame import Surface, draw, Rect, key, image, transform
|
|
|
|
|
from pygame.locals import K_UP, K_DOWN, K_LEFT, K_RIGHT
|
|
|
|
|
|
|
|
|
|
from engine.entity import Entity
|
|
|
|
|
from engine.game import Game
|
|
|
|
|
from engine.resources import res
|
|
|
|
|
from engine.components.collide_rect import CollideRect
|
|
|
|
|
from engine.components.sprite import Sprite
|
|
|
|
|
|
|
|
|
|
from .common import S
|
2019-12-09 13:21:33 +01:00
|
|
|
|
from .pacdot import EnsmallmentDot
|
2019-12-09 12:36:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PacMan(Entity):
|
|
|
|
|
SPEED = 2
|
|
|
|
|
|
|
|
|
|
def __init__(self, x, y):
|
|
|
|
|
super().__init__('pacman')
|
2019-12-09 15:29:35 +01:00
|
|
|
|
self.x = x
|
|
|
|
|
self.y = y
|
2019-12-09 12:36:42 +01:00
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# Surface quand on est petit.
|
2019-12-09 13:21:33 +01:00
|
|
|
|
self.smol_surf = Surface((S//2, S//2)).convert()
|
|
|
|
|
self.smol_surf.fill((0, 0, 0))
|
|
|
|
|
draw.circle(self.smol_surf,
|
|
|
|
|
(255, 255, 0),
|
|
|
|
|
(S//4, S//4),
|
|
|
|
|
S//4)
|
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# Surfaces pour chaque direction, pour chaque état de
|
|
|
|
|
# l’animation.
|
|
|
|
|
surf_1 = image.load(res('pacman_1.png')).convert()
|
|
|
|
|
surf_2 = image.load(res('pacman_2.png')).convert()
|
2019-12-09 12:36:42 +01:00
|
|
|
|
self.surf = [[transform.rotate(surf_1, 90),
|
|
|
|
|
transform.rotate(surf_1, -90),
|
|
|
|
|
transform.flip(surf_1, True, False),
|
|
|
|
|
surf_1],
|
|
|
|
|
[transform.rotate(surf_2, 90),
|
|
|
|
|
transform.rotate(surf_2, -90),
|
|
|
|
|
transform.flip(surf_2, True, False),
|
|
|
|
|
surf_2]]
|
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# État d’animation courant.
|
2019-12-09 12:36:42 +01:00
|
|
|
|
self.cur_anim = 0
|
|
|
|
|
|
|
|
|
|
self.sprite = self.add(Sprite(self.surf[self.cur_anim][0], 2))
|
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# Rectangle quand on est petit.
|
2019-12-09 13:21:33 +01:00
|
|
|
|
self.smol_rect = Rect(self.x, self.y,
|
|
|
|
|
self.sprite.width//2, self.sprite.width//2)
|
2019-12-09 15:29:35 +01:00
|
|
|
|
|
2019-12-09 13:21:33 +01:00
|
|
|
|
self.rect = Rect(self.x, self.y,
|
|
|
|
|
self.sprite.width, self.sprite.width)
|
2019-12-09 15:29:35 +01:00
|
|
|
|
|
2019-12-09 13:21:33 +01:00
|
|
|
|
self.phys = CollideRect(self.rect,
|
|
|
|
|
static=False,
|
|
|
|
|
cb=self.cb)
|
|
|
|
|
|
|
|
|
|
self.add(self.phys)
|
|
|
|
|
|
2019-12-09 12:36:42 +01:00
|
|
|
|
self.script = PacMan.update
|
2019-12-09 13:21:33 +01:00
|
|
|
|
self.smol = False
|
|
|
|
|
self.smol_since = None
|
|
|
|
|
|
|
|
|
|
def cb(self, c):
|
|
|
|
|
if not isinstance(c.parent, EnsmallmentDot):
|
|
|
|
|
return
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# On a mangé une graine de rapetissement.
|
2019-12-09 13:21:33 +01:00
|
|
|
|
if not self.smol:
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# Décalage au milieu du couloir
|
2019-12-09 13:21:33 +01:00
|
|
|
|
self.x += S//4
|
|
|
|
|
self.y += S//4
|
2019-12-09 15:29:35 +01:00
|
|
|
|
|
2019-12-09 13:21:33 +01:00
|
|
|
|
PacMan.SPEED *= 2
|
|
|
|
|
self.smol_rect.x = self.x
|
|
|
|
|
self.smol_rect.y = self.y
|
|
|
|
|
self.phys.rect = self.smol_rect
|
|
|
|
|
self.smol = True
|
|
|
|
|
self.smol_since = Game.cur_tick
|
2019-12-09 12:36:42 +01:00
|
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
|
p = self.phys
|
|
|
|
|
s = self.sprite
|
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# Expiration du rapetissement.
|
2019-12-09 13:21:33 +01:00
|
|
|
|
if self.smol and Game.cur_tick - self.smol_since > 60*2:
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# On se réaligne dans le couloir.
|
2019-12-09 13:21:33 +01:00
|
|
|
|
self.x -= self.x % S
|
|
|
|
|
self.y -= self.y % S
|
2019-12-09 15:29:35 +01:00
|
|
|
|
|
2019-12-09 13:21:33 +01:00
|
|
|
|
self.rect.x = self.x
|
|
|
|
|
self.rect.y = self.y
|
|
|
|
|
self.phys.rect = self.rect
|
|
|
|
|
self.smol = False
|
|
|
|
|
PacMan.SPEED = PacMan.SPEED//2
|
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# Avance l’état d’animation.
|
2019-12-09 12:36:42 +01:00
|
|
|
|
if Game.cur_tick % 5 == 0:
|
|
|
|
|
self.cur_anim += 1
|
|
|
|
|
self.cur_anim %= 2
|
|
|
|
|
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# Choix de la surface en fonction de la direction effective.
|
2019-12-09 12:36:42 +01:00
|
|
|
|
if p.vy < 0:
|
|
|
|
|
s.surf = self.surf[self.cur_anim][0]
|
|
|
|
|
elif p.vy > 0:
|
|
|
|
|
s.surf = self.surf[self.cur_anim][1]
|
|
|
|
|
elif p.vx < 0:
|
|
|
|
|
s.surf = self.surf[self.cur_anim][2]
|
|
|
|
|
elif p.vx > 0:
|
|
|
|
|
s.surf = self.surf[self.cur_anim][3]
|
|
|
|
|
|
2019-12-09 13:21:33 +01:00
|
|
|
|
if self.smol:
|
|
|
|
|
s.surf = self.smol_surf
|
|
|
|
|
|
2019-12-09 12:36:42 +01:00
|
|
|
|
inputs = key.get_pressed()
|
2019-12-09 13:21:33 +01:00
|
|
|
|
if self.smol:
|
2019-12-09 15:29:35 +01:00
|
|
|
|
# Quand on est petit on n’a pas de maintient de la
|
|
|
|
|
# vitesse.
|
2019-12-09 12:36:42 +01:00
|
|
|
|
p.vx = 0
|
|
|
|
|
p.vy = 0
|
|
|
|
|
if inputs[K_UP]:
|
|
|
|
|
p.vy = -PacMan.SPEED
|
|
|
|
|
if inputs[K_DOWN]:
|
|
|
|
|
p.vy = PacMan.SPEED
|
|
|
|
|
if inputs[K_LEFT]:
|
|
|
|
|
p.vx = -PacMan.SPEED
|
|
|
|
|
if inputs[K_RIGHT]:
|
|
|
|
|
p.vx = PacMan.SPEED
|