This repository has been archived on 2019-12-09. You can view files and clone it, but cannot push or open issues or pull requests.
pacman2/scenes/game_objects/pacman.py

115 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
from .pacdot import EnsmallmentDot
class PacMan(Entity):
SPEED = 2
def __init__(self, x, y):
super().__init__('pacman')
surf_1 = image.load(res('pacman_1.png')).convert()
surf_2 = image.load(res('pacman_2.png')).convert()
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)
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]]
self.cur_anim = 0
self.sprite = self.add(Sprite(self.surf[self.cur_anim][0], 2))
self.smol_rect = Rect(self.x, self.y,
self.sprite.width//2, self.sprite.width//2)
self.rect = Rect(self.x, self.y,
self.sprite.width, self.sprite.width)
self.phys = CollideRect(self.rect,
static=False,
cb=self.cb)
self.add(self.phys)
self.script = PacMan.update
self.x = x
self.y = y
self.smol = False
self.smol_since = None
def cb(self, c):
if not isinstance(c.parent, EnsmallmentDot):
return
if not self.smol:
self.x += S//4
self.y += S//4
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
def update(self):
p = self.phys
s = self.sprite
# Expiration du pouvoir
if self.smol and Game.cur_tick - self.smol_since > 60*2:
self.x -= self.x % S
self.y -= self.y % S
self.rect.x = self.x
self.rect.y = self.y
self.phys.rect = self.rect
self.smol = False
PacMan.SPEED = PacMan.SPEED//2
# Avance lanimation
if Game.cur_tick % 5 == 0:
self.cur_anim += 1
self.cur_anim %= 2
# Nouvelle sprite
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]
if self.smol:
s.surf = self.smol_surf
inputs = key.get_pressed()
if self.smol:
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