fantôme basique
This commit is contained in:
parent
fbfedc539f
commit
a7c1b606d3
@ -1,11 +1,11 @@
|
|||||||
class Scene:
|
class Scene:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.entities = []
|
self.entities = {}
|
||||||
|
|
||||||
def unload(self):
|
def unload(self):
|
||||||
for entity in self.entities:
|
for entity in self.entities.values():
|
||||||
entity.unregister()
|
entity.unregister()
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
for entity in self.entities:
|
for entity in self.entities.values():
|
||||||
entity.register()
|
entity.register()
|
||||||
|
@ -4,15 +4,15 @@ from .singleton import Singleton
|
|||||||
class SceneManager(metaclass=Singleton):
|
class SceneManager(metaclass=Singleton):
|
||||||
def __init__(self, scene):
|
def __init__(self, scene):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self._scene = scene
|
self.scene = scene
|
||||||
self._scene.load()
|
self.scene.load()
|
||||||
|
|
||||||
def change_scene(self, new_scene):
|
def change_scene(self, new_scene):
|
||||||
self._scene.unload()
|
self.scene.unload()
|
||||||
self._scene = new_scene
|
self.scene = new_scene
|
||||||
self._scene.load()
|
self.scene.load()
|
||||||
|
|
||||||
def step(self):
|
def step(self):
|
||||||
for entity in self._scene.entities:
|
for entity in self.scene.entities.values():
|
||||||
if entity.script is not None:
|
if entity.script is not None:
|
||||||
entity.script(entity)
|
entity.script(entity)
|
||||||
|
@ -30,6 +30,7 @@ class PhysicsServer(Server):
|
|||||||
x_step = -1 if d.vx < 0 else 1
|
x_step = -1 if d.vx < 0 else 1
|
||||||
y_step = -1 if d.vy < 0 else 1
|
y_step = -1 if d.vy < 0 else 1
|
||||||
for i in range(abs(d.vx)):
|
for i in range(abs(d.vx)):
|
||||||
|
d.parent.x += x_step
|
||||||
c = self._check_collide(d)
|
c = self._check_collide(d)
|
||||||
if c is not None:
|
if c is not None:
|
||||||
if c.cb:
|
if c.cb:
|
||||||
@ -38,8 +39,8 @@ class PhysicsServer(Server):
|
|||||||
d.parent.x -= x_step
|
d.parent.x -= x_step
|
||||||
d.vx = 0
|
d.vx = 0
|
||||||
break
|
break
|
||||||
d.parent.x += x_step
|
|
||||||
for i in range(abs(d.vy)):
|
for i in range(abs(d.vy)):
|
||||||
|
d.parent.y += y_step
|
||||||
c = self._check_collide(d)
|
c = self._check_collide(d)
|
||||||
if c is not None:
|
if c is not None:
|
||||||
if c.cb:
|
if c.cb:
|
||||||
@ -48,6 +49,3 @@ class PhysicsServer(Server):
|
|||||||
d.parent.y -= y_step
|
d.parent.y -= y_step
|
||||||
d.vy = 0
|
d.vy = 0
|
||||||
break
|
break
|
||||||
d.parent.y += y_step
|
|
||||||
# d.vx = 0
|
|
||||||
# d.vy = 0
|
|
||||||
|
BIN
res/ghost.png
Normal file
BIN
res/ghost.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 652 B |
@ -4,6 +4,7 @@ from engine.entity import Entity
|
|||||||
from engine.components.sprite import Sprite
|
from engine.components.sprite import Sprite
|
||||||
from engine.components.collide_rect import CollideRect
|
from engine.components.collide_rect import CollideRect
|
||||||
from engine.servers.graphics import GraphicsServer
|
from engine.servers.graphics import GraphicsServer
|
||||||
|
from engine.scene_manager import SceneManager
|
||||||
from pygame import Rect
|
from pygame import Rect
|
||||||
from pygame import image
|
from pygame import image
|
||||||
from pygame import Surface
|
from pygame import Surface
|
||||||
@ -33,12 +34,41 @@ class PacDot(Entity):
|
|||||||
self.unregister()
|
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):
|
class Level(Entity):
|
||||||
def __init__(self, path):
|
def __init__(self, path):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
desc = image.load(res(path))
|
desc = image.load(res(path))
|
||||||
w, h = desc.get_size()
|
w, h = desc.get_size()
|
||||||
GraphicsServer().resize((w * S, h * S))
|
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 = Surface((w * S, h * S)).convert()
|
||||||
self.surf.fill((0, 0, 0))
|
self.surf.fill((0, 0, 0))
|
||||||
wall = Surface((S, S)).convert()
|
wall = Surface((S, S)).convert()
|
||||||
@ -53,6 +83,20 @@ class Level(Entity):
|
|||||||
pc.x = x * S
|
pc.x = x * S
|
||||||
pc.y = y * S
|
pc.y = y * S
|
||||||
entities.append(pc)
|
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)
|
Sprite(self, self.surf, 0)
|
||||||
|
|
||||||
|
|
||||||
@ -67,12 +111,19 @@ class MovingEntity(Entity):
|
|||||||
|
|
||||||
|
|
||||||
class Ghost(MovingEntity):
|
class Ghost(MovingEntity):
|
||||||
def update_ghost():
|
|
||||||
pass
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__(image.load(res('ghost.png')).convert())
|
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):
|
class Lvl0(Scene):
|
||||||
@ -105,10 +156,13 @@ class Lvl0(Scene):
|
|||||||
pacman.phys.rect.w = S//T
|
pacman.phys.rect.w = S//T
|
||||||
pacman.phys.rect.h = S//T
|
pacman.phys.rect.h = S//T
|
||||||
|
|
||||||
self.entities.append(Level('lvl0.png'))
|
self.entities['level'] = Level('lvl0.png')
|
||||||
for entity in entities:
|
for i, entity in enumerate(entities):
|
||||||
self.entities.append(entity)
|
self.entities[f'pacdot-{i}'] = entity
|
||||||
self.entities.append(pacman)
|
self.entities['pacman'] = pacman
|
||||||
|
self.entities['ghost'] = Ghost()
|
||||||
|
self.entities['ghost'].x = S*6
|
||||||
|
self.entities['ghost'].y = S*2
|
||||||
|
|
||||||
super().load()
|
super().load()
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user