initial commit
commit
16299410dd
|
|
@ -0,0 +1,2 @@
|
|||
.idea
|
||||
/.idea
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
import datetime
|
||||
from game_object import GameObject
|
||||
|
||||
|
||||
class GameManager:
|
||||
|
||||
_game_objects = list()
|
||||
def __init__(self, screen):
|
||||
|
||||
self._last_update = None
|
||||
self._screen = screen
|
||||
self._pause = False
|
||||
|
||||
@property
|
||||
def pause(self):
|
||||
return self._pause
|
||||
|
||||
@pause.setter
|
||||
def pause(self, pause: bool):
|
||||
self._pause = pause
|
||||
|
||||
def update(self):
|
||||
if not self._pause:
|
||||
now = datetime.datetime.now()
|
||||
self.__call_update(now - self._last_update)
|
||||
self.__call_draw()
|
||||
self._last_update = now
|
||||
|
||||
def __call_update(self, deltatime):
|
||||
for go in GameManager._game_objects:
|
||||
go.update(deltatime)
|
||||
|
||||
def __call_draw(self):
|
||||
for go in GameManager._game_objects:
|
||||
go.draw()
|
||||
|
||||
@classmethod
|
||||
def register_game_object(cls, go: GameObject) -> bool:
|
||||
if go not in cls._game_objects:
|
||||
cls._game_objects.append(go)
|
||||
return True
|
||||
return False
|
||||
|
||||
@classmethod
|
||||
def unregister_game_object(cls, go: GameObject) -> bool:
|
||||
if go in cls._game_objects:
|
||||
cls._game_objects.remove(go)
|
||||
return True
|
||||
return False
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
import abc
|
||||
|
||||
|
||||
class GameObject(abc.ABC):
|
||||
|
||||
def __init__(self, active, x, y, w, h):
|
||||
self._active = active
|
||||
self._x = x
|
||||
self._y = y
|
||||
self._width = w
|
||||
self._height = h
|
||||
|
||||
@property
|
||||
def active(self):
|
||||
return self._active
|
||||
|
||||
@active.setter
|
||||
def active(self, active: bool):
|
||||
self._active = active
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self._x
|
||||
|
||||
@x.setter
|
||||
def x(self, coord_x):
|
||||
self._x = coord_x
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self._y
|
||||
|
||||
@y.setter
|
||||
def y(self, coord_y):
|
||||
self._y = coord_y
|
||||
|
||||
@property
|
||||
def width(self):
|
||||
return self._width
|
||||
|
||||
@width.setter
|
||||
def width(self, width):
|
||||
self._width = width
|
||||
|
||||
@property
|
||||
def height(self):
|
||||
return self._height
|
||||
|
||||
@height.setter
|
||||
def height(self, height):
|
||||
self._height = height
|
||||
|
||||
@abc.abstractmethod
|
||||
def update(self, deltatime):
|
||||
if not self._active:
|
||||
return
|
||||
pass
|
||||
|
||||
@abc.abstractmethod
|
||||
def draw(self):
|
||||
if not self.active:
|
||||
return
|
||||
pass
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
import pygame
|
||||
from game_manager import GameManager
|
||||
from sprite2D import Sprite2D
|
||||
|
||||
pygame.init()
|
||||
|
||||
SCREEN_WIDTH = 600
|
||||
SCREEN_HEIGHT = 450
|
||||
|
||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
|
||||
gm = GameManager(screen)
|
||||
go = Sprite2D(0, 0, 20, 40)
|
||||
gm.register_game_object(go)
|
||||
|
||||
run = True
|
||||
|
||||
while run:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
run = False
|
||||
|
||||
pygame.quit()
|
||||
|
|
@ -0,0 +1 @@
|
|||
pygame==2.5.2
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
from game_object import GameObject
|
||||
|
||||
|
||||
class Sprite2D(GameObject):
|
||||
|
||||
def __init__(self, x, y, w, h, active=True):
|
||||
super().__init__(active, x, y, w, h)
|
||||
|
||||
def update(self, deltatime):
|
||||
pass
|
||||
|
||||
def draw(self):
|
||||
pass
|
||||
Loading…
Reference in New Issue