/
game.py
85 lines (63 loc) · 1.95 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -----------------------------------------------------------------------------
# Game
# -----------------------------------------------------------------------------
import sys
import time
from GamePad import GamePad, GAMEPAD_BUTTON, GAMEPAD_AXIS_DIRECTION
from Player import Player
from Screen import Screen
from EntityManager import EntityManager
from World import World
class Game:
gamepad = None
player = None
screen = None
entityManager = None
world = None
FRAMES_PER_SECOND = 25
game_is_running = False
def __init__(self):
print("Press Ctrl-C to quit")
self.gamepad = GamePad()
self.screen = Screen()
self.entityManager = EntityManager();
self.player = Player(self)
self.world = World(self)
self.entityManager.add(self.player)
def update(self, dt):
self.gamepad.update()
if (self.gamepad.pressed[GAMEPAD_BUTTON.START]) :
self.game_is_running = False
return
self.world.update()
self.entityManager.update(dt)
def draw(self):
self.screen.clear()
self.world.draw()
self.entityManager.draw()
self.screen.draw()
def start(self):
print("GO!")
self.loop()
def loop(self):
self.game_is_running = True
lastFrameTime = time.time()
try:
while self.game_is_running :
currentTime = time.time()
dt = currentTime - lastFrameTime
sleepTime = 1./self.FRAMES_PER_SECOND - (currentTime - lastFrameTime)
if sleepTime > 0:
time.sleep(sleepTime)
self.update(dt)
self.draw()
lastFrameTime = currentTime
self.screen.off()
except KeyboardInterrupt:
self.screen.off()
sys.exit()
try:
game = Game()
game.start()
except OSError:
print('Bye :P')