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/lvl0.py
2019-12-08 23:47:32 +01:00

117 lines
3.0 KiB
Python

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
entities=[]
class PacDot(Entity):
s = Surface((S, S))
draw.circle(s, (255, 255, 0), (S//2, S//2), S//4)
def __init__(self):
super().__init__()
CollideRect(self, Rect(0, 0, S, S),
static=True, solid=False, cb=self.cb)
Sprite(self, PacDot.s, 1)
def cb(self):
self.unregister()
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)):
col = desc.get_at((x, y))
if col == (0, 0, 255):
CollideRect(self, Rect(x * S, y * S, S, S))
self.surf.blit(wall, (x * S, y * S))
elif col == (139, 139, 139):
pc = PacDot()
pc.x = x * S
pc.y = y * S
entities.append(pc)
Sprite(self, self.surf, 0)
class MovingEntity(Entity):
def __init__(self, surf):
super().__init__()
sprite = Sprite(self, surf, 2)
self.phys = CollideRect(
self,
Rect(self.x, self.y, sprite.width, sprite.width),
static=False)
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):
T = 1
v = 2
pac_surf = Surface((S//T, S//T)).convert()
pac_surf.fill((0, 0, 0))
draw.circle(pac_surf, (255, 255, 0), (S//(2*T), S//(2*T)), S//(2*T))
pacman = MovingEntity(pac_surf)
def pacman_script(entity):
p = pacman.phys
inputs = key.get_pressed()
if T > 1:
p.vx = 0
p.vy = 0
if inputs[K_UP]:
p.vy = -v
if inputs[K_DOWN]:
p.vy = v
if inputs[K_LEFT]:
p.vx = -v
if inputs[K_RIGHT]:
p.vx = v
pacman.script = pacman_script
pacman.x = pacman.phys.rect.x = S*1
pacman.y = pacman.phys.rect.y = S*1
pacman.phys.rect.w = S//T
pacman.phys.rect.h = S//T
self.entities.append(Level('lvl0.png'))
for entity in entities:
self.entities.append(entity)
self.entities.append(pacman)
super().load()
scene = Lvl0()