forked from petya0927/game_of_life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_life_v1.pyw
138 lines (116 loc) · 4.86 KB
/
game_of_life_v1.pyw
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
import pygame
from pygame.locals import MOUSEBUTTONUP
import numpy as np
WINDOW_HEIGHT = 800
WINDOW_WIDTH = 800
BLOCK_SIZE = 10
OFFSET = 100
matrix = np.zeros((WINDOW_WIDTH // BLOCK_SIZE + OFFSET, WINDOW_HEIGHT // BLOCK_SIZE + OFFSET), dtype=int)
def draw_units():
for x in range(OFFSET // 2, matrix.shape[0] - OFFSET // 2):
for y in range(OFFSET // 2, matrix.shape[1] - OFFSET // 2):
if matrix[x][y] == 1:
rect = pygame.Rect((x - OFFSET // 2) * BLOCK_SIZE, (y - OFFSET // 2) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(screen, (0, 0, 0), rect)
else:
rect = pygame.Rect((x - OFFSET // 2) * BLOCK_SIZE, (y - OFFSET // 2) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(screen, (255, 255, 255), rect)
def draw_grid():
for x in range(0, WINDOW_WIDTH, BLOCK_SIZE):
for y in range(0, WINDOW_HEIGHT, BLOCK_SIZE):
rect = pygame.Rect(x, y, BLOCK_SIZE, BLOCK_SIZE)
pygame.draw.rect(screen, (100, 100, 100), rect, 1)
def edit_unit(x, y):
unit_pos = (x // BLOCK_SIZE, y // BLOCK_SIZE)
matrix[unit_pos[0] + OFFSET // 2, unit_pos[1] + OFFSET // 2] = int(not matrix[unit_pos[0] + OFFSET // 2, unit_pos[1] + OFFSET // 2])
def neighbours(unit_x, unit_y):
return [
matrix[unit_x-1][unit_y-1], matrix[unit_x][unit_y-1], matrix[unit_x+1][unit_y-1],
matrix[unit_x-1][unit_y], matrix[unit_x+1][unit_y],
matrix[unit_x-1][unit_y+1], matrix[unit_x][unit_y+1], matrix[unit_x+1][unit_y+1]
].count(1)
def compute_new_step():
global matrix
new_matrix = matrix.copy()
for x in range(0, matrix.shape[0] - 1):
for y in range(0, matrix.shape[1] - 1):
if matrix[x][y] == 1:
if neighbours(x, y) == 2 or neighbours(x, y) == 3:
new_matrix[x][y] = 1
else:
new_matrix[x][y] = 0
else:
if neighbours(x, y) == 3:
new_matrix[x][y] = 1
matrix = new_matrix.copy()
def reset():
matrix.fill(0)
def copy_matrixes(matrix, new_matrix):
for x in range(0, new_matrix.shape[0] - 1):
for y in range(0, new_matrix.shape[1] - 1):
try:
new_matrix[x][y] = matrix[x][y]
except:
new_matrix[x][y] = 0
def resize(w, h):
global screen, WINDOW_WIDTH, WINDOW_HEIGHT, matrix
WINDOW_WIDTH = w
WINDOW_HEIGHT = h
new_matrix = np.zeros((WINDOW_WIDTH // BLOCK_SIZE + OFFSET, WINDOW_HEIGHT // BLOCK_SIZE + OFFSET), dtype=int)
copy_matrixes(matrix, new_matrix)
matrix = new_matrix.copy()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), pygame.RESIZABLE)
def show_more():
global matrix, BLOCK_SIZE
BLOCK_SIZE -= 1 if BLOCK_SIZE > 3 else 0
new_matrix = np.zeros((WINDOW_WIDTH // BLOCK_SIZE + OFFSET, WINDOW_HEIGHT // BLOCK_SIZE + OFFSET), dtype=int)
copy_matrixes(matrix, new_matrix)
matrix = new_matrix.copy()
def show_less():
global matrix, BLOCK_SIZE
BLOCK_SIZE += 1
new_matrix = np.zeros((WINDOW_WIDTH // BLOCK_SIZE + OFFSET, WINDOW_HEIGHT // BLOCK_SIZE + OFFSET), dtype=int)
copy_matrixes(matrix, new_matrix)
matrix = new_matrix.copy()
def init_window():
global screen
running = True
is_grid_visible = True
is_editing = True
is_playing = False
title = 'Game of Life - Editing'
pygame.init()
screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT), pygame.RESIZABLE)
pygame.display.set_caption(title)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if pygame.key.name(event.key) == 'space':
is_playing = not is_playing
is_editing = not is_playing
title = 'Game of Life - Playing' if is_playing else 'Game of Life'
title = 'Game of Life - Editing' if is_editing else title
pygame.display.set_caption(title)
elif pygame.key.name(event.key) == 'm':
show_more()
elif pygame.key.name(event.key) == 'l':
show_less()
elif pygame.key.name(event.key) == 'g':
is_grid_visible = not is_grid_visible
elif pygame.key.name(event.key) == 'r':
reset()
elif event.type == MOUSEBUTTONUP and is_editing:
mouse_x, mouse_y = event.pos
edit_unit(mouse_x, mouse_y)
elif event.type == pygame.VIDEORESIZE:
resize(event.w, event.h)
if is_playing:
compute_new_step()
draw_units()
if is_grid_visible:
draw_grid()
pygame.display.update()
init_window()
pygame.quit()