archivage initial
This commit is contained in:
commit
639ed42143
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
__pycache__/
|
||||||
|
*.pyc
|
0
engine/components/__init__.py
Normal file
0
engine/components/__init__.py
Normal file
7
engine/components/collide_rect.py
Normal file
7
engine/components/collide_rect.py
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
from .component import Component
|
||||||
|
|
||||||
|
|
||||||
|
class CollideRect(Component):
|
||||||
|
def __init__(self, rect):
|
||||||
|
super().__init__()
|
||||||
|
self.rect = rect
|
14
engine/components/component.py
Normal file
14
engine/components/component.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from abc import abstractmethod
|
||||||
|
|
||||||
|
|
||||||
|
class Component:
|
||||||
|
def __init__(self):
|
||||||
|
self.parent = None
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def register(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def unregister(self):
|
||||||
|
pass
|
4
engine/components/script.py
Normal file
4
engine/components/script.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
class Script:
|
||||||
|
def __init__(self, parent, func):
|
||||||
|
super().__init__(parent)
|
||||||
|
self.func = func
|
16
engine/components/sprite.py
Normal file
16
engine/components/sprite.py
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
from .component import Component
|
||||||
|
from ..servers.graphics import GraphicsServer
|
||||||
|
|
||||||
|
|
||||||
|
class Sprite(Component):
|
||||||
|
def __init__(self, surf, z):
|
||||||
|
super().__init__()
|
||||||
|
self.surf = surf
|
||||||
|
self.width = self.surf.get_width()
|
||||||
|
self.z = z
|
||||||
|
|
||||||
|
def register(self):
|
||||||
|
GraphicsServer().register_component(self)
|
||||||
|
|
||||||
|
def unregister(self):
|
||||||
|
GraphicsServer().register_component(self)
|
18
engine/entity.py
Normal file
18
engine/entity.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
class Entity:
|
||||||
|
def __init__(self):
|
||||||
|
self._components = []
|
||||||
|
self.x = 0
|
||||||
|
self.y = 0
|
||||||
|
self.script = None
|
||||||
|
|
||||||
|
def add(self, component):
|
||||||
|
self._components.append(component)
|
||||||
|
component.parent = self
|
||||||
|
|
||||||
|
def register(self):
|
||||||
|
for component in self._components:
|
||||||
|
component.register()
|
||||||
|
|
||||||
|
def unregister(self):
|
||||||
|
for component in self._components:
|
||||||
|
component.unregister()
|
29
engine/game.py
Normal file
29
engine/game.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from pygame.time import Clock
|
||||||
|
|
||||||
|
from .servers.graphics import GraphicsServer
|
||||||
|
from .servers.sound import SoundServer
|
||||||
|
from .servers.physics import PhysicsServer
|
||||||
|
from .servers.input import InputServer, StopException
|
||||||
|
from .scene_manager import SceneManager
|
||||||
|
|
||||||
|
|
||||||
|
class Game:
|
||||||
|
def __init__(self, start_scene):
|
||||||
|
self._graphics_server = GraphicsServer()
|
||||||
|
self._sound_server = SoundServer()
|
||||||
|
self._physics_server = PhysicsServer()
|
||||||
|
self._input_server = InputServer()
|
||||||
|
self._scene_manager = SceneManager(start_scene)
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
clock = Clock()
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
self._input_server.step()
|
||||||
|
except StopException:
|
||||||
|
break
|
||||||
|
self._scene_manager.step()
|
||||||
|
self._physics_server.step()
|
||||||
|
self._sound_server.step()
|
||||||
|
self._graphics_server.step()
|
||||||
|
clock.tick(60)
|
5
engine/resources.py
Normal file
5
engine/resources.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from os.path import join
|
||||||
|
|
||||||
|
|
||||||
|
def res(filename):
|
||||||
|
return join('res', filename)
|
11
engine/scene.py
Normal file
11
engine/scene.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
class Scene:
|
||||||
|
def __init__(self):
|
||||||
|
self.entities = []
|
||||||
|
|
||||||
|
def unload(self):
|
||||||
|
for entity in self.entities:
|
||||||
|
entity.unregister()
|
||||||
|
|
||||||
|
def load(self):
|
||||||
|
for entity in self.entities:
|
||||||
|
entity.register()
|
18
engine/scene_manager.py
Normal file
18
engine/scene_manager.py
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
from .singleton import Singleton
|
||||||
|
|
||||||
|
|
||||||
|
class SceneManager(metaclass=Singleton):
|
||||||
|
def __init__(self, scene):
|
||||||
|
super().__init__()
|
||||||
|
self._scene = scene
|
||||||
|
self._scene.load()
|
||||||
|
|
||||||
|
def change_scene(self, new_scene):
|
||||||
|
self._scene.unload()
|
||||||
|
self._scene = new_scene
|
||||||
|
self._scene.load()
|
||||||
|
|
||||||
|
def step(self):
|
||||||
|
for entity in self._scene.entities:
|
||||||
|
if entity.script is not None:
|
||||||
|
entity.script(entity)
|
0
engine/servers/__init__.py
Normal file
0
engine/servers/__init__.py
Normal file
23
engine/servers/graphics.py
Normal file
23
engine/servers/graphics.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
from .server import Server
|
||||||
|
|
||||||
|
from pygame import display
|
||||||
|
|
||||||
|
|
||||||
|
class GraphicsServer(Server):
|
||||||
|
def __init__(self, size=(640, 480)):
|
||||||
|
super().__init__()
|
||||||
|
display.init()
|
||||||
|
self.window = display.set_mode(size)
|
||||||
|
|
||||||
|
def register_component(self, component):
|
||||||
|
super().register_component(component)
|
||||||
|
self._components.sort(key=lambda e: e.z)
|
||||||
|
|
||||||
|
def step(self):
|
||||||
|
for component in self._components:
|
||||||
|
self.window.blit(component.surf,
|
||||||
|
(component.parent.x, component.parent.y))
|
||||||
|
display.flip()
|
||||||
|
|
||||||
|
def resize(self, new_size):
|
||||||
|
self.window = display.set_mode(new_size)
|
17
engine/servers/input.py
Normal file
17
engine/servers/input.py
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
from .server import Server
|
||||||
|
|
||||||
|
from pygame import event
|
||||||
|
from pygame.locals import QUIT
|
||||||
|
|
||||||
|
|
||||||
|
class StopException(RuntimeError): pass
|
||||||
|
|
||||||
|
|
||||||
|
class InputServer(Server):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def step(self):
|
||||||
|
for e in event.get():
|
||||||
|
if e.type == QUIT:
|
||||||
|
raise StopException
|
10
engine/servers/physics.py
Normal file
10
engine/servers/physics.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from .server import Server
|
||||||
|
|
||||||
|
|
||||||
|
class PhysicsServer(Server):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def step(self):
|
||||||
|
for component in self._components:
|
||||||
|
pass
|
10
engine/servers/script.py
Normal file
10
engine/servers/script.py
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
from .server import Server
|
||||||
|
|
||||||
|
|
||||||
|
class ScriptServer(Server):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def step(self):
|
||||||
|
for component in self._components:
|
||||||
|
component.func(component.parent)
|
20
engine/servers/server.py
Normal file
20
engine/servers/server.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
from abc import abstractmethod
|
||||||
|
|
||||||
|
from ..singleton import Singleton
|
||||||
|
|
||||||
|
|
||||||
|
class Server(metaclass=Singleton):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
self._components = []
|
||||||
|
|
||||||
|
def register_component(self, component):
|
||||||
|
self._components.append(component)
|
||||||
|
|
||||||
|
def unregister_component(self, component):
|
||||||
|
self._components.remove(component)
|
||||||
|
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def step(self):
|
||||||
|
pass
|
9
engine/servers/sound.py
Normal file
9
engine/servers/sound.py
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
from .server import Server
|
||||||
|
|
||||||
|
|
||||||
|
class SoundServer(Server):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def step(self):
|
||||||
|
pass
|
23
engine/singleton.py
Normal file
23
engine/singleton.py
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
class Singleton(type):
|
||||||
|
def __init__(cls, name, bases, dict):
|
||||||
|
super(Singleton, cls).__init__(name, bases, dict)
|
||||||
|
cls.instance = None
|
||||||
|
|
||||||
|
def __call__(cls,*args,**kw):
|
||||||
|
if cls.instance is None:
|
||||||
|
cls.instance = super(Singleton, cls).\
|
||||||
|
__call__(*args, **kw)
|
||||||
|
return cls.instance
|
||||||
|
|
||||||
|
|
||||||
|
# class Singleton():
|
||||||
|
# instance = None
|
||||||
|
|
||||||
|
# def __init__(self):
|
||||||
|
# self.__class__.instance = self
|
||||||
|
|
||||||
|
# def get_instance():
|
||||||
|
# if self.__class__.instance == None:
|
||||||
|
# raise RuntimeError('Server has not been initialized.')
|
||||||
|
# else:
|
||||||
|
# return self.__class__.instance
|
4
engine/vec2.py
Normal file
4
engine/vec2.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
class Vec2:
|
||||||
|
def __init__(self, x=0, y=0):
|
||||||
|
self.x = x
|
||||||
|
self.y = y
|
7
pac-man.py
Executable file
7
pac-man.py
Executable file
@ -0,0 +1,7 @@
|
|||||||
|
#!/bin/python3
|
||||||
|
|
||||||
|
from engine.game import Game
|
||||||
|
from scenes.lvl0 import scene
|
||||||
|
|
||||||
|
|
||||||
|
Game(scene).run()
|
BIN
res/lvl0.png
Normal file
BIN
res/lvl0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 12 KiB |
77
scenes/lvl0.py
Normal file
77
scenes/lvl0.py
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
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
|
||||||
|
|
||||||
|
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)):
|
||||||
|
if desc.get_at((x, y)) == (0, 0, 255):
|
||||||
|
self.add(CollideRect(Rect(x * S, y * S, S, S)))
|
||||||
|
self.surf.blit(wall, (x * S, y * S))
|
||||||
|
self.add(Sprite(self.surf, 0))
|
||||||
|
|
||||||
|
|
||||||
|
class MovingEntity(Entity):
|
||||||
|
def __init__(self, surf):
|
||||||
|
super().__init__()
|
||||||
|
sprite = Sprite(surf, 1)
|
||||||
|
self.add(sprite)
|
||||||
|
self.add(CollideRect(Rect(self.x, self.y,
|
||||||
|
sprite.width, sprite.width)))
|
||||||
|
|
||||||
|
|
||||||
|
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):
|
||||||
|
pac_surf = Surface((S, S)).convert()
|
||||||
|
pac_surf.fill((0, 0, 0))
|
||||||
|
draw.circle(pac_surf, (255, 255, 0), (S//2, S//2), S//2)
|
||||||
|
pacman = MovingEntity(pac_surf)
|
||||||
|
def pacman_script(entity):
|
||||||
|
inputs = key.get_pressed()
|
||||||
|
if inputs[K_UP]:
|
||||||
|
entity.y -= 2
|
||||||
|
if inputs[K_DOWN]:
|
||||||
|
entity.y += 2
|
||||||
|
if inputs[K_LEFT]:
|
||||||
|
entity.x -= 2
|
||||||
|
if inputs[K_RIGHT]:
|
||||||
|
entity.x += 2
|
||||||
|
pacman.script = pacman_script
|
||||||
|
|
||||||
|
self.entities.append(pacman)
|
||||||
|
self.entities.append(Level('lvl0.png'))
|
||||||
|
|
||||||
|
super().load()
|
||||||
|
|
||||||
|
|
||||||
|
scene = Lvl0()
|
4
scenes/main_menu.py
Normal file
4
scenes/main_menu.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
class MainMenu(Scene):
|
||||||
|
pass
|
||||||
|
|
||||||
|
scene = MainMenu()
|
Reference in New Issue
Block a user