-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
63 lines (49 loc) · 1.6 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
from threading import Thread
from time import sleep
import pygame
import pygame.gfxdraw
class Game(object):
def __init__(self):
self.__canvas = None
self.__model = None
self.__running = False
self.__surface = pygame.Surface((400, 400), pygame.SRCALPHA, 32)
def get_surface(self):
return self.__surface
def resize_surface(self, w, h):
self.__surface = pygame.Surface((w, h), pygame.SRCALPHA, 32)
def start(self, canvas):
if self.__running:
print("game thread is already running")
return
self.__canvas = canvas
self.__running = True
self.__thread = Thread(target=self.game_loop)
self.__thread.start()
def restart(self):
if self.__running:
print("game thread is already running")
return
self.__running = True
self.__thread = Thread(target=self.game_loop)
self.__thread.start()
def game_loop(self):
time = 0
while self.__running:
sleep(1/30)
time += 8/30
self.__surface.fill((0, 0, 0, 0))
if self.__model is not None:
self.__model.draw(self.__surface, time)
if self.__canvas is None:
print("missing canvas to update")
else:
self.__canvas.update()
def stop(self):
if not self.__running:
print("game thread is not running")
return
self.__running = False
self.__thread.join()
def set_model(self, model):
self.__model = model