-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtetris.py
55 lines (41 loc) · 1.52 KB
/
tetris.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
import signal
import time
import traceback
from config import Config
from display import Display
from gameLogic import GameLogic
class Tetris:
def __init__(self):
signal.signal(signal.SIGINT, self.signal_handler) # ^C
signal.signal(signal.SIGTERM, self.signal_handler) # process termination
self.config = Config()
self.display = Display(self.config)
self.game_logic = GameLogic(self.config)
self.goOn = True
def run(self):
self.display.run()
while self.goOn:
time_A = time.time()
input_key = self.display.get_input_key()
if input_key == 113:
# quit
self.goOn = False
try:
self.display.update_grid(self.game_logic.do_logic(input_key))
self.display.update_score(self.game_logic.get_score())
except Exception as e:
if str(e).strip() == "Initial collision":
self.game_logic.set_game_ended(True)
self.display.set_game_ended(True)
else:
traceback.print_exc()
time_B = time.time()
time.sleep(self.config.refresh_rate - (time_B - time_A))
self.display.quit()
def signal_handler(self, sig, frame):
"""A custom signal handler to stop the game"""
print("Got Signal: %s " % sig)
if sig == signal.SIGINT or sig == signal.SIGTERM:
self.goOn = False
if __name__ == "__main__":
Tetris().run()