-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameoflife.py
66 lines (55 loc) · 1.65 KB
/
gameoflife.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
import pygame
import random
pygame.init()
population =[]
new_population =[]
sumof=0
clock = pygame.time.Clock()
state =0
neighbor = 0
window = pygame.display.set_mode((800,500))
pygame.display.set_caption("Game of Life!")
window.fill((255,255,255))
windowopen = True
for i in range(9):
individual_population =[]
for j in range(15):
y = random.randint(0,1)
individual_population.append(y)
population.append(individual_population)
def countneighbor(grid,x,y):
summ = 0
for i in range(-1,2):
for j in range(-1,2):
col = (x+i+9) %9
row = (y+j+15) %15
summ+=grid[col][row]
summ-=grid[x][y]
return summ
new_population = population
print(population)
while windowopen:
for i in range(9):
for j in range(15):
if population[i][j] == 0:
pygame.draw.rect(window,(0,0,0),[20+(j*50),20+(i*50),50,50],1)
else:
pygame.draw.rect(window,(0,0,0),[20+(j*50),20+(i*50),50,50])
pygame.display.update()
clock.tick(5)
for i in range(9):
for j in range(15):
state = population[i][j]
neighbor = countneighbor(population,i,j)
if state == 0 and neighbor ==3:
new_population[i][j]=1
elif state==1 and (neighbor<2 or neighbor>3):
new_population[i][j]=0
else:
new_population[i][j]=state
population=new_population
window.fill((255,255,255))
for events in pygame.event.get():
if events.type == pygame.QUIT:
windowopen = False
pygame.display.update()