-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.py
52 lines (44 loc) · 1.4 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
import pygame
from GS_Game import GS_Game
class Game:
gamestateStack = []
display = None
clock = None
isRunning = True
def __init__(self):
"""
initialize game:
init pygame,
create window,
create clock,
add first gamestate
"""
pygame.init()
self.display = pygame.display.set_mode([200, 400])
self.clock = pygame.time.Clock()
self.gamestateStack.append(GS_Game(self.display))
def run(self):
"""
run game:
handle events
update and draw game
delay to lock game at 60 fps
"""
while self.isRunning and len(self.gamestateStack) > 0:
self.handleEvents()
self.update()
self.draw()
if not self.gamestateStack[len(self.gamestateStack) - 1].isAlive:
self.gamestateStack.pop()
self.clock.tick(60)
def handleEvents(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.isRunning = False
self.gamestateStack[len(self.gamestateStack) - 1].handleEvent(event)
def update(self):
self.gamestateStack[len(self.gamestateStack) - 1].update()
def draw(self):
self.display.fill((0, 0, 0))
self.gamestateStack[len(self.gamestateStack) - 1].draw()
pygame.display.flip()