-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
56 lines (47 loc) · 1.29 KB
/
main.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
import pygame, sys
from simulation import Simulation
pygame.init()
GREY = (29, 29, 29)
WINDOW_WIDTH = 750
WINDOW_HEIGHT = 750
CELL_SIZE = 25
FPS = 12
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
pygame.display.set_caption("Game of Life")
clock = pygame.time.Clock()
simulation = Simulation(WINDOW_WIDTH, WINDOW_HEIGHT, CELL_SIZE)
#Simulation Loop
while True:
# 1. Event Handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
row = pos[1] // CELL_SIZE
column = pos[0] // CELL_SIZE
simulation.toggle_cell(row, column)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN:
simulation.start()
pygame.display.set_caption("Game of Life is running")
elif event.key == pygame.K_SPACE:
simulation.stop()
pygame.display.set_caption("Game of Life has stopped")
elif event.key == pygame.K_f:
FPS += 2
elif event.key == pygame.K_s:
if FPS > 5:
FPS -= 2
elif event.key == pygame.K_r:
simulation.create_random_state()
elif event.key == pygame.K_c:
simulation.clear()
# 2. Updating State
simulation.update()
# 3. Drawing
window.fill(GREY)
simulation.draw(window)
pygame.display.update()
clock.tick(FPS)