fantôme basique

This commit is contained in:
papush! 2019-12-09 09:57:53 +01:00
parent fbfedc539f
commit a7c1b606d3
5 changed files with 73 additions and 21 deletions

View File

@ -1,11 +1,11 @@
class Scene:
def __init__(self):
self.entities = []
self.entities = {}
def unload(self):
for entity in self.entities:
for entity in self.entities.values():
entity.unregister()
def load(self):
for entity in self.entities:
for entity in self.entities.values():
entity.register()

View File

@ -4,15 +4,15 @@ from .singleton import Singleton
class SceneManager(metaclass=Singleton):
def __init__(self, scene):
super().__init__()
self._scene = scene
self._scene.load()
self.scene = scene
self.scene.load()
def change_scene(self, new_scene):
self._scene.unload()
self._scene = new_scene
self._scene.load()
self.scene.unload()
self.scene = new_scene
self.scene.load()
def step(self):
for entity in self._scene.entities:
for entity in self.scene.entities.values():
if entity.script is not None:
entity.script(entity)

View File

@ -30,6 +30,7 @@ class PhysicsServer(Server):
x_step = -1 if d.vx < 0 else 1
y_step = -1 if d.vy < 0 else 1
for i in range(abs(d.vx)):
d.parent.x += x_step
c = self._check_collide(d)
if c is not None:
if c.cb:
@ -38,8 +39,8 @@ class PhysicsServer(Server):
d.parent.x -= x_step
d.vx = 0
break
d.parent.x += x_step
for i in range(abs(d.vy)):
d.parent.y += y_step
c = self._check_collide(d)
if c is not None:
if c.cb:
@ -48,6 +49,3 @@ class PhysicsServer(Server):
d.parent.y -= y_step
d.vy = 0
break
d.parent.y += y_step
# d.vx = 0
# d.vy = 0

BIN
res/ghost.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 652 B

View File

@ -4,6 +4,7 @@ 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 engine.scene_manager import SceneManager
from pygame import Rect
from pygame import image
from pygame import Surface
@ -33,12 +34,41 @@ class PacDot(Entity):
self.unregister()
def path(start, tgt, x, y, trace, length):
if start == tgt:
return length
if start is None or trace[y][x]:
return False
trace[y][x] = True
u = path(start.up, tgt, x, y-1, trace, length + 1)
d = path(start.down, tgt, x, y+1, trace, length + 1)
l = path(start.left, tgt, x-1, y, trace, length + 1)
r = path(start.right, tgt, x+1, y, trace, length + 1)
best = None
for subpath in (u, d, l, r):
if subpath and (best is None or subpath < best):
best = subpath
return best
class Node():
def __init__(self):
self.up = None
self.down = None
self.left = None
self.right = None
graph = None
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))
# graph = [[None for x in range(w)] for y in range(h)]
self.surf = Surface((w * S, h * S)).convert()
self.surf.fill((0, 0, 0))
wall = Surface((S, S)).convert()
@ -53,6 +83,20 @@ class Level(Entity):
pc.x = x * S
pc.y = y * S
entities.append(pc)
# elif col == (0, 0, 0):
# graph[y][x] = Node()
# for y, line in enumerate(graph):
# for x, node in enumerate(line):
# if node is None:
# continue
# if x > 0:
# node.left = graph[y][x-1]
# if x < w:
# node.right = graph[y][x+1]
# if y > 0:
# node.up = graph[y-1][x]
# if y < h:
# node.down = graph[y+1][y]
Sprite(self, self.surf, 0)
@ -67,12 +111,19 @@ class MovingEntity(Entity):
class Ghost(MovingEntity):
def update_ghost():
pass
def __init__(self):
super().__init__(image.load(res('ghost.png')).convert())
self.script = update_ghost()
self.script = Ghost.script
def script(self):
# cur_node = graph[self.y//S][self.x//S]
pacman = SceneManager().scene.entities['pacman']
# pacman_node = graph[pacman.y//S][pacman.x//S]
x_dist = pacman.x - self.x
y_dist = pacman.y - self.y
s = 1
self.phys.vx = s if x_dist > 0 else -s
self.phys.vy = s if y_dist > 0 else -s
class Lvl0(Scene):
@ -105,10 +156,13 @@ class Lvl0(Scene):
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)
self.entities['level'] = Level('lvl0.png')
for i, entity in enumerate(entities):
self.entities[f'pacdot-{i}'] = entity
self.entities['pacman'] = pacman
self.entities['ghost'] = Ghost()
self.entities['ghost'].x = S*6
self.entities['ghost'].y = S*2
super().load()