2019-11-23 10:46:25 +01:00
|
|
|
from engine.resources import res
|
|
|
|
from engine.scene import Scene
|
|
|
|
from engine.entity import Entity
|
|
|
|
from engine.components.sprite import Sprite
|
|
|
|
from engine.components.collide_rect import CollideRect
|
|
|
|
from engine.servers.graphics import GraphicsServer
|
|
|
|
from pygame import Rect
|
|
|
|
from pygame import image
|
|
|
|
from pygame import Surface
|
|
|
|
from pygame import draw
|
|
|
|
from pygame.locals import *
|
|
|
|
from pygame import key
|
|
|
|
from itertools import product
|
|
|
|
|
|
|
|
|
|
|
|
S=20
|
|
|
|
|
|
|
|
class Level(Entity):
|
|
|
|
def __init__(self, path):
|
|
|
|
super().__init__()
|
|
|
|
desc = image.load(res(path))
|
|
|
|
w, h = desc.get_size()
|
|
|
|
GraphicsServer().resize((w * S, h * S))
|
|
|
|
self.surf = Surface((w * S, h * S)).convert()
|
|
|
|
self.surf.fill((0, 0, 0))
|
|
|
|
wall = Surface((S, S)).convert()
|
|
|
|
wall.fill((0, 0, 255))
|
|
|
|
for x, y in product(range(w), range(h)):
|
|
|
|
if desc.get_at((x, y)) == (0, 0, 255):
|
|
|
|
self.add(CollideRect(Rect(x * S, y * S, S, S)))
|
|
|
|
self.surf.blit(wall, (x * S, y * S))
|
|
|
|
self.add(Sprite(self.surf, 0))
|
|
|
|
|
|
|
|
|
|
|
|
class MovingEntity(Entity):
|
|
|
|
def __init__(self, surf):
|
|
|
|
super().__init__()
|
|
|
|
sprite = Sprite(surf, 1)
|
|
|
|
self.add(sprite)
|
2019-12-05 11:31:53 +01:00
|
|
|
self.phys = CollideRect(Rect(self.x, self.y,
|
|
|
|
sprite.width, sprite.width),
|
|
|
|
static=False)
|
|
|
|
self.add(self.phys)
|
2019-11-23 10:46:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
class Ghost(MovingEntity):
|
|
|
|
def update_ghost():
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__(image.load(res('ghost.png')).convert())
|
|
|
|
self.script = update_ghost()
|
|
|
|
|
|
|
|
|
|
|
|
class Lvl0(Scene):
|
|
|
|
def load(self):
|
2019-12-05 11:31:53 +01:00
|
|
|
T = 2
|
|
|
|
v = 2
|
|
|
|
|
|
|
|
pac_surf = Surface((S//T, S//T)).convert()
|
2019-11-23 10:46:25 +01:00
|
|
|
pac_surf.fill((0, 0, 0))
|
2019-12-05 11:31:53 +01:00
|
|
|
draw.circle(pac_surf, (255, 255, 0), (S//(2*T), S//(2*T)), S//(2*T))
|
2019-11-23 10:46:25 +01:00
|
|
|
pacman = MovingEntity(pac_surf)
|
|
|
|
def pacman_script(entity):
|
2019-12-05 11:31:53 +01:00
|
|
|
p = pacman.phys
|
2019-11-23 10:46:25 +01:00
|
|
|
inputs = key.get_pressed()
|
|
|
|
if inputs[K_UP]:
|
2019-12-05 11:31:53 +01:00
|
|
|
# entity.y -= 2
|
|
|
|
p.vy = -v
|
2019-11-23 10:46:25 +01:00
|
|
|
if inputs[K_DOWN]:
|
2019-12-05 11:31:53 +01:00
|
|
|
# entity.y += 2
|
|
|
|
p.vy = v
|
2019-11-23 10:46:25 +01:00
|
|
|
if inputs[K_LEFT]:
|
2019-12-05 11:31:53 +01:00
|
|
|
# entity.x -= 2
|
|
|
|
p.vx = -v
|
2019-11-23 10:46:25 +01:00
|
|
|
if inputs[K_RIGHT]:
|
2019-12-05 11:31:53 +01:00
|
|
|
# entity.x += 2
|
|
|
|
p.vx = v
|
2019-11-23 10:46:25 +01:00
|
|
|
pacman.script = pacman_script
|
2019-12-05 11:31:53 +01:00
|
|
|
pacman.x = pacman.phys.rect.x = S*15
|
|
|
|
pacman.y = pacman.phys.rect.y = S*15
|
|
|
|
|
|
|
|
pacman.phys.rect.w = S//T
|
|
|
|
pacman.phys.rect.h = S//T
|
2019-11-23 10:46:25 +01:00
|
|
|
|
|
|
|
self.entities.append(pacman)
|
|
|
|
self.entities.append(Level('lvl0.png'))
|
|
|
|
|
|
|
|
super().load()
|
|
|
|
|
|
|
|
|
|
|
|
scene = Lvl0()
|