-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (98 loc) · 3.34 KB
/
main.py
File metadata and controls
136 lines (98 loc) · 3.34 KB
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
#Conway's Game of Life
import pygame
import random
pygame.init()
BLACK = (0, 0, 0)
# GREY = (128, 128, 128)
GREY = (0,0,0)
YELLOW = (255, 255, 0)
WIDTH, HEIGHT = 800, 800
TILE_SIZE = 4
GRID_WIDTH = WIDTH // TILE_SIZE
GRID_HEIGHT = HEIGHT // TILE_SIZE
FPS = 120
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
def gen(num):
return set([(random.randrange(0,GRID_WIDTH),random.randrange(0,GRID_HEIGHT)) for _ in range(num)])
def draw_grid(positions):
for position in positions:
col, row = position
top_left = (col*TILE_SIZE, row*TILE_SIZE)
pygame.draw.rect(screen, YELLOW, (*top_left, TILE_SIZE, TILE_SIZE))
for row in range(GRID_HEIGHT):
pygame.draw.line(screen, BLACK,(0,row*TILE_SIZE),(WIDTH,row*TILE_SIZE))
for col in range(GRID_WIDTH):
pygame.draw.line(screen, BLACK,(col*TILE_SIZE,0),(col*TILE_SIZE,HEIGHT))
def adjust_grid(positions):
all_neighbours = set()
new_positions = set()
for position in positions:
neighbors = get_neighbours(position)
all_neighbours.update(neighbors)
neighbors = list(filter(lambda x: x in positions, neighbors))
if len(neighbors) in [2,3]:
new_positions.add(position)
for position in all_neighbours:
neighbors = get_neighbours(position)
neighbors = list(filter(lambda x: x in positions, neighbors))
if len(neighbors) == 3:
new_positions.add(position)
return new_positions
def get_neighbours(pos):
x,y = pos
neighbors = []
for dx in [-1,0,1]:
if x + dx < 0 or x + dx >= GRID_WIDTH:
continue
for dy in [-1,0,1]:
if y + dy < 0 or y + dy >= GRID_HEIGHT:
continue
if dx == 0 and dy == 0:
continue
neighbors.append((x+dx,y+dy))
return neighbors
def main():
running = True
playing = False
count = 0
update_freq = 1
positions = set()
# positions.add((10,10))
while running:
clock.tick(FPS)
if playing:
count +=1
if count >= update_freq:
count = 0
positions = adjust_grid(positions)
pygame.display.set_caption('Playing' if playing else 'Paused')
#show fps
# pygame.display.set_caption(f'FPS: {clock.get_fps():.2f}')
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
x,y = pygame.mouse.get_pos()
col = x // TILE_SIZE
row = y // TILE_SIZE
pos = (col, row)
if pos in positions:
positions.remove(pos)
else:
positions.add(pos)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
playing = not playing
if event.key == pygame.K_c:
positions = set()
playing = False
count = 0
if event.key == pygame.K_g:
positions = gen(random.randrange(2,30)*GRID_WIDTH)
screen.fill(GREY)
draw_grid(positions)
pygame.display.update()
pygame.quit()
if __name__ == '__main__':
main()