-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
161 lines (139 loc) · 5.41 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
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
159
160
161
import pygame
from random import randint
from cell import Cell
class Life():
def __init__(self, screen, size):
self.grid = []
self.screen = pygame.display.set_mode(screen)
self.size = size
self.tiles_to_check = set()
self.new_tiles = []
self.alive = set()
self.dead = []
self.build_grid(self.screen.get_width() // self.size)
self.grid_size = len(self.grid)
self.nearest_neighbour_list = [
(-1, -1), (0, -1), (1, -1),
(-1, 0), (1, 0), (-1, 1),
(0, 1), (1, 1)
]
self.nearest_neighbour_list00 = [
(-1, -1), (0, -1), (1, -1),
(-1, 0), (0, 0), (1, 0), (-1, 1),
(0, 1), (1, 1)
]
def build_grid(self, tile_size):
for y in range(tile_size):
self.grid.append([])
for x in range(tile_size):
pos = (x, y)
cell = Cell(pos, self.size)
self.grid[y].append(cell)
def update_tilev2(self):
self.get_tiles_to_check()
self.find_neighbours()
self.screen.fill((0, 0, 0))
for tile in self.alive:
tile.state = True
tile.draw(self.screen)
for tile in self.dead:
tile.state = False
self.tiles_to_check.clear()
self.dead.clear()
def get_tiles_to_check(self):
for tile in self.alive:
for dx, dy in self.nearest_neighbour_list00:
pos_x = tile.pos_x + dx
pos_y = tile.pos_y + dy
if self.inside_grid(pos_y, pos_x):
current_tile = self.grid[pos_y][pos_x]
else:
continue
if current_tile not in self.tiles_to_check:
self.tiles_to_check.add(current_tile)
self.alive.clear()
def check_tiles(self, tile, neighbours):
if tile.state and (neighbours < 2 or neighbours > 3) and (tile not in self.alive):
self.dead.append(tile)
elif tile.state == False and neighbours == 3 and (tile not in self.dead):
self.alive.add(tile)
elif tile.state and (neighbours == 2 or neighbours == 3):
self.alive.add(tile)
def find_neighbours(self):
for tile in self.tiles_to_check:
neighbours = 0
for dx, dy in self.nearest_neighbour_list:
# if self.inside_grid(tile.pos_y + dy, tile.pos_x + dx):
try:
if self.grid[tile.pos_y + dy][tile.pos_x + dx].state:
neighbours += 1
except IndexError:
continue
self.check_tiles(tile, neighbours)
def build_obj(self, pos, obj=None):
if obj == None:
obj = [
[0, 0, 1],
[1, 0, 1],
[0, 1, 1]
]
dx = pos[0]
dy = pos[1]
for y in range(len(obj)):
for x in range(len(obj[y])):
if bool(obj[y][x]):
try:
tile = self.grid[dy + y][dx + x]
except IndexError:
continue
tile.state = True
self.alive.add(tile)
def inside_grid(self, y, x):
if (0 < x < (self.grid_size - 1)) and \
(0 < y < (self.grid_size - 1)):
return True
return False
def update_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.MOUSEBUTTONDOWN:
grid_pos_of_mouse = list(
pygame.mouse.get_pos()) # // game.tile_size
grid_pos_of_mouse[0] = grid_pos_of_mouse[0] // game.size
grid_pos_of_mouse[1] = grid_pos_of_mouse[1] // game.size
self.build_obj(grid_pos_of_mouse, cannon)
return True
if __name__ == '__main__':
game = Life((800, 800), 10)
print(game.grid_size)
cannon = [
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0,
0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
]
clock = pygame.time.Clock()
FPS = 120
run = True
game.build_obj([1, 1], cannon)
while run:
# clock.tick(FPS)
run = game.update_events()
game.update_tilev2()
pygame.display.update()
pygame.quit()