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

98 lines
3.2 KiB
Python
Raw Permalink Normal View History

2019-12-09 12:36:42 +01:00
from itertools import product
2019-12-09 18:15:05 +01:00
from enum import Enum, auto
2019-12-09 12:36:42 +01:00
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, Direction
2019-12-09 13:21:33 +01:00
from .pacdot import PacDot, EnsmallmentDot
2019-12-09 12:36:42 +01:00
class Node():
cpt = 0
2019-12-09 15:34:37 +01:00
def __init__(self):
self.id = self.__class__.cpt
self.__class__.cpt += 1
self.neighbors = [None for dir in Direction]
@property
def children(self):
children = []
for direction in Direction:
node = self.neighbors[direction.value]
if node is not None:
children.append((node, direction))
return children
2019-12-09 12:36:42 +01:00
class Level(Entity):
2019-12-09 18:15:05 +01:00
class Tile(Enum):
AIR = auto()
WALL = auto()
2019-12-09 12:36:42 +01:00
def __init__(self, path):
super().__init__('level')
self.path = path
2019-12-09 18:15:05 +01:00
colors = {(0, 0, 0): Tile.AIR, # empty
(0, 0, 255): Tile.WALL, # wall
(255, 0, 0): Tile.AIR, # tele
(255, 255, 0): Tile.WALL, # idk
(255, 0, 255): Tile.WALL, # inside cage
(0, 255, 0): Tile.WALL, # cage entrance
(0, 255, 255): Tile.WALL, # cage wall
(139, 139, 139): Tile.AIR, # pac-dot
(193, 193, 193): Tile.AIR} # ensmallment dot
2019-12-09 12:36:42 +01:00
def load(self):
desc = image.load(res(self.path))
w, h = desc.get_size()
2019-12-09 18:24:35 +01:00
self.w = w * S
self.h = h * S
2019-12-09 12:36:42 +01:00
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))
self.graph = [[None for x in range(w)] for y in range(h)]
2019-12-09 12:36:42 +01:00
for x, y in product(range(w), range(h)):
2019-12-09 18:15:05 +01:00
col = desc.get_at((x, y))[:3]
2019-12-09 18:15:05 +01:00
if Level.colors[col] == Level.Tile.AIR:
self.graph[y][x] = Node()
2019-12-09 18:15:05 +01:00
elif Level.colors[col] == Level.Tile.WALL:
2019-12-09 12:36:42 +01:00
self.add(CollideRect(Rect(x * S, y * S, S, S)))
2019-12-09 18:15:05 +01:00
wall.fill(col)
2019-12-09 12:36:42 +01:00
self.surf.blit(wall, (x * S, y * S))
2019-12-09 18:15:05 +01:00
if col == (139, 139, 139): # pac-dot
2019-12-09 12:36:42 +01:00
pc = PacDot()
pc.x = x * S
pc.y = y * S
self.scene.add(pc)
PacDot.tot += 1
2019-12-09 18:15:05 +01:00
elif col == (193, 193, 193): # ensmallment dot
2019-12-09 13:21:33 +01:00
pc = EnsmallmentDot()
pc.x = x * S
pc.y = y * S
self.scene.add(pc)
2019-12-09 12:36:42 +01:00
self.add(Sprite(self.surf, 0))
for y, line in enumerate(self.graph):
for x, node in enumerate(line):
if node is None:
continue
node.neighbors[Direction.LEFT.value] = \
2019-12-09 15:34:37 +01:00
self.graph[y][(x-1) % w]
node.neighbors[Direction.RIGHT.value] = \
2019-12-09 15:34:37 +01:00
self.graph[y][(x+1) % w]
node.neighbors[Direction.UP.value] = \
2019-12-09 15:34:37 +01:00
self.graph[(y-1) % h][x]
node.neighbors[Direction.DOWN.value] = \
2019-12-09 15:34:37 +01:00
self.graph[(y+1) % h][x]