81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
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
|
||
|
||
|
||
class PacMan(Entity):
|
||
SIZE = 1
|
||
SPEED = 2
|
||
|
||
def __init__(self, x, y):
|
||
super().__init__('pacman')
|
||
|
||
# surf = Surface((S//PacMan.SIZE, S//PacMan.SIZE)).convert()
|
||
# surf.fill((0, 0, 0))
|
||
# draw.circle(surf,
|
||
# (255, 255, 0),
|
||
# (S//(2*PacMan.SIZE), S//(2*PacMan.SIZE)),
|
||
# S//(2*PacMan.SIZE))
|
||
surf_1 = image.load(res('pacman_1.png')).convert()
|
||
surf_2 = image.load(res('pacman_2.png')).convert()
|
||
|
||
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.phys = self.add(
|
||
CollideRect(Rect(self.x, self.y,
|
||
self.sprite.width, self.sprite.width),
|
||
static=False))
|
||
self.script = PacMan.update
|
||
self.x = x
|
||
self.y = y
|
||
|
||
def update(self):
|
||
p = self.phys
|
||
s = self.sprite
|
||
|
||
# Avance l’animation
|
||
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]
|
||
|
||
inputs = key.get_pressed()
|
||
if PacMan.SIZE > 1:
|
||
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
|