-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameOfLife.py
154 lines (117 loc) · 4.48 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
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
#Made by: Kouah Mohammed Aymen
#Computer science student at "National Computer science Engineering School, Algiers (ESI)"
#E-mail: [email protected]
#Github: https://github.com/aymenkouah
#Requires installaling "pygame"
#https://www.pygame.org/news
#This is not a game ( a zero player game)
#To understand the rules of this mini society visit
#https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
#Modules and packages
import pygame
import time
from random import randint
# Classes
class gen():
def __init__(self, rows, cols):
self.cols = cols
self.rows = rows
self.gen = 0
grid = []
for i in range(0, rows):
grid.append([])
for k in range(0, cols):
grid[i].append([])
grid[i][k] = randint(0, 1)
self.grid = grid
def around(self, row_num, col_num):
res = []
# if col_num+1 < self.cols:
# res.append(self.grid[row_num][col_num+1])
# if col_num-1 > -1:
# res.append(self.grid[row_num][col_num-1])
# if row_num+1 < self.rows:
# res.append(self.grid[row_num+1][col_num])
# if row_num-1 > -1:
# res.append(self.grid[row_num-1][col_num])
# if col_num+1 < self.cols and row_num+1 < self.rows:
# res.append(self.grid[row_num+1][col_num+1])
# if col_num+1 < self.cols and row_num-1 > -1:
# res.append(self.grid[row_num-1][col_num+1])
# if col_num-1 > -1 and row_num-1 > -1:
# res.append(self.grid[row_num-1][col_num-1])
# if col_num-1 > -1 and row_num+1 > self.rows:
# res.append(self.grid[row_num-1][col_num+1])
for i in range(max(0, row_num-1), min(row_num+1, self.rows-1)+1):
for k in range(max(0, col_num-1), min(col_num+1, self.cols-1)+1):
if i != row_num or k != col_num:
res.append(self.grid[i][k])
return res
def rule(self, row_num, col_num):
surround = self.around(row_num, col_num)
alive = 0
for i in range(len(surround)):
if surround[i] == 1:
alive += 1
if (alive == 3 or alive == 2) and self.grid[row_num][col_num] == 1:
return 1
elif alive == 3 and self.grid[row_num][col_num] == 0:
return 1
return 0
def next_gen(self):
self.gen += 1
new_gen = []
for i in range(self.rows):
new_gen.append([])
for k in range(self.cols):
new_gen[i].append([])
new_gen[i][k] = self.rule(i, k)
for i in range(self.rows):
for k in range(self.cols):
self.grid[i][k] = new_gen[i][k]
# Variables
row_number = 100
col_number = 100
width_of_a_square = 8
width = width_of_a_square * col_number
height = width_of_a_square * row_number
dead_color = (255, 255, 255) # #999999
alive_color = (255, 0, 0) # #990000
black = (0, 0, 0) # #000000
fps = pygame.time.Clock()
pygame.init()
window = pygame.display.set_mode((width, height))
running = True
grid = gen(row_number, col_number)
# Functions
def draw_the_grid(window, grid):
for i in range(grid.rows):
for k in range(grid.cols):
if grid.grid[i][k] == 1:
pygame.draw.rect(window, alive_color, (i*width_of_a_square,
k*width_of_a_square, width_of_a_square, width_of_a_square))
elif grid.grid[i][k] == 0:
pygame.draw.rect(window, dead_color, (i*width_of_a_square,
k*width_of_a_square, width_of_a_square, width_of_a_square))
pygame.draw.rect(window, black, (i*width_of_a_square, k *
width_of_a_square, width_of_a_square, width_of_a_square), 1)
def reset():
new_grid = gen(row_number, col_number)
return new_grid
# Main code
while running:
window.fill(black)
draw_the_grid(window, grid)
grid.next_gen()
###controls###
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
grid = reset()
print(grid.gen)
##############
# fps.tick(120)
pygame.display.update()