-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
49 lines (40 loc) · 1.58 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
import time
from random import randint
from grid import Grid
class Game(object):
def __init__(self, screen_service, grid):
self.grid = grid
self.screen_service = screen_service
self.dead_cells = []
self.generation_count = 0
self.start_timestamp = time.time()
self.running = False
def setup(self):
self.screen_service.draw_ui()
def start(self):
self.setup()
self.running = True
while self.running:
time.sleep(0.5)
self.update()
self.display()
if not self.screen_service.check_inputs():
self.screen_service.cleanup()
self.running = False
def update(self):
self.generation_count = self.generation_count + 1
generations_per_second = int(self.generation_count / (time.time() - self.start_timestamp))
self.screen_service.draw_status(self.generation_count,
generations_per_second,
len(self.grid.cells))
if len(self.grid.cells) > 0:
cell_to_kill = list(self.grid.cells)[0]
self.grid.kill_cell(cell_to_kill)
self.dead_cells = [cell_to_kill]
screen_height, screen_width = self.screen_service.get_dimensions()
random_cell = (randint(0, screen_height),
randint(0, screen_width))
self.grid.birth_cell(random_cell)
def display(self):
self.screen_service.clear_cells(self.dead_cells)
self.screen_service.draw_cells(list(self.grid.cells))