-
Notifications
You must be signed in to change notification settings - Fork 0
/
hotfix.py
158 lines (117 loc) 路 4.03 KB
/
hotfix.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import pygame, time
from copy import deepcopy
WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Game of life by mtnleo")
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (50, 50, 50)
BLOCK_SIZE = 160
MOUSE_POS = (WIDTH/2, HEIGHT/2)
FPS = 75
def get_empty_list():
empty_list = []
return empty_list
def get_matrix():
matrix = []
x, y = 0, 0
for i in range(0, WIDTH // BLOCK_SIZE):
row = get_empty_list()
for j in range(0, HEIGHT // BLOCK_SIZE):
row.append(False)
y += BLOCK_SIZE
matrix.append(row)
x += BLOCK_SIZE
return matrix
def update_matrix_coords(matrix, pos):
new_matrix = deepcopy(matrix)
x = pos[0]
y = pos[1]
new_matrix[x][y] = True
return new_matrix
def update_matrix(matrix, pos):
matrix[pos[0]][pos[1]] = True
return matrix
def dead_cell_check(new_matrix, matrix, pos):
alive_neighbors = 0
for i in range(pos[0] - 1, pos[0] + 2):
for j in range(pos[1] - 1, pos[1] + 2):
if j > -1 and i > -1:
try:
if i == pos[0] and j == pos[1]:
pass
elif matrix[i][j]:
alive_neighbors += 1
except IndexError: ## if it surpasses the list index, don't evaluate
pass
if alive_neighbors == 3:
new_matrix[pos[0]][pos[1]] = True
return new_matrix
def check_cells(new_matrix, matrix, pos):
alive_neighbors = 0
for i in range(pos[0] - 1, pos[0] + 2):
for j in range(pos[1] - 1, pos[1] + 2):
if j > -1 and i > -1:
try:
if i == pos[0] and j == pos[1]:
pass
elif matrix[i][j]:
alive_neighbors += 1
else:
new_matrix = dead_cell_check(new_matrix, matrix, (i, j))
except IndexError: ## if it surpasses the list index, don't evaluate
pass
if alive_neighbors < 2 or alive_neighbors > 3:
x = pos[0]
y = pos[1]
new_matrix[x][y] = False
return new_matrix
def check_matrix(matrix):
new_matrix = deepcopy(matrix)
for i in range(0, len(matrix)):
for j in range(0, len(matrix[0])):
if matrix[i][j]:
new_matrix = check_cells(new_matrix, matrix, (i, j))
return new_matrix
def draw_window():
WIN.fill(BLACK)
pygame.display.update()
def draw_grid_matrix(matrix):
draw_window()
for i in range(0, len(matrix)):
for j in range(0, len(matrix[0])):
rect = pygame.Rect(i * BLOCK_SIZE, j * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
if matrix[i][j]:
pygame.draw.rect(WIN, WHITE, rect, 0)
else:
pygame.draw.rect(WIN, GRAY, rect, 1)
def main():
clock = pygame.time.Clock()
run = True
matrix = get_matrix()
ready = False
while run:
while not ready and run: # Time for drawing the initial state, space to continue
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
x = pygame.mouse.get_pos()[0] // BLOCK_SIZE
y = pygame.mouse.get_pos()[1] // BLOCK_SIZE
matrix = update_matrix_coords(matrix, (x, y))
if event.type == pygame.K_SPACE:
ready = True
draw_grid_matrix(matrix)
pygame.display.update()
while ready and run: # Actual game
clock.tick(20)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
draw_grid_matrix(matrix)
matrix = check_matrix(matrix)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
print(pygame.font.get_fonts())