-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
69 lines (56 loc) · 2.13 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
57
58
59
60
61
62
63
64
65
66
67
68
69
import multiprocessing
import pygame
import numpy as np
from view import startGame
clr_alive = (255, 255, 215)
clr_background = (10, 10, 40)
clr_grid = (30, 30, 60)
def setScreen():
pygame.init()
pygame.display.set_caption("John Conway's Game of Life")
screen = pygame.display.set_mode((n * cellSize, n * cellSize))
screen.fill(clr_grid)
for row in range(n):
for col in range(n):
rect = pygame.Rect(col * cellSize, row * cellSize, cellSize - 1, cellSize - 1)
pygame.draw.rect(screen, clr_background, rect)
pygame.display.update()
return screen
def updateOneCell(stateRow, stateCol, startState, screen):
rect = pygame.Rect(stateRow * cellSize, stateCol * cellSize, cellSize - 1, cellSize - 1)
if startState[stateCol][stateRow] == 1:
clr = clr_background
else:
clr = clr_alive
pygame.draw.rect(screen, clr, rect)
startState[stateCol][stateRow] = not startState[stateCol][stateRow]
def updateAllCells(startState, screen, n):
for row in range(n):
for col in range(n):
updateOneCell(row, col, startState, screen)
def setAliveCells(n, cellSize):
screen = setScreen()
startState = np.zeros((n, n))
listening = True
while listening:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
button = pygame.mouse.get_pressed()
if button[0]:
stateRow = (event.pos[0] // cellSize)
stateCol = (event.pos[1] // cellSize)
updateOneCell(stateRow, stateCol, startState, screen)
pygame.display.update()
if button[1]:
startState = (np.random.rand(n ** 2).reshape(n, n) > 0.5).astype(np.int8)
updateAllCells(startState, screen, n)
pygame.display.update()
if button[2]:
listening = False
elif event.type == pygame.QUIT:
quit()
startGame(screen, startState, n, cellSize)
if __name__ == '__main__':
n = 50
cellSize = 10
setAliveCells(n, cellSize)