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/level.py
2019-12-09 12:36:42 +01:00

40 lines
1.2 KiB
Python

from itertools import product
from pygame import Surface, Rect, image
from engine.entity import Entity
from engine.resources import res
from engine.servers.graphics import GraphicsServer
from engine.components.collide_rect import CollideRect
from engine.components.sprite import Sprite
from .common import S
from .pacdot import PacDot
class Level(Entity):
def __init__(self, path):
super().__init__('level')
self.path = path
def load(self):
desc = image.load(res(self.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):
self.add(CollideRect(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
self.scene.add(pc)
PacDot.tot += 1
self.add(Sprite(self.surf, 0))