-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameOfLife_v1.py
140 lines (115 loc) · 6.68 KB
/
gameOfLife_v1.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
# ---------------------------------------------------------------------------
# SEGA97
# Simulation of Conway's Game of Life in pygame
# ---------------------------------------------------------------------------
# Possible speedups:
# TODO: Instead of redrawing the entire grid every frame, only update the cells that have changed since the last frame
# TODO: Rather than iterating over every cell in the grid every frame, only update cells that are near living cells
# TODO: Use Numpy array operations
# TODO: Parallelize computations?
import pygame
import numpy as np
# import win32gui
# import win32con
pygame.font.init()
# Define colors for the cells and background
col_about_to_die = (200, 200, 225)
col_about_to_die = (255, 0, 0) #red
col_alive = (255, 255, 215)
col_background = (10, 10, 40)
col_grid = (30, 30, 60)
# Initialize a font for displaying generation information
myfont = pygame.font.SysFont("monospace", 16)
# Function to update the game state and draw cells
def update(surface, cur, sz, gen):
nxt = np.zeros((cur.shape[0], cur.shape[1]))
# Iterate over each cell in the current state
for r, c in np.ndindex(cur.shape):
# Count the number of alive neighbors for each cell
num_alive = np.sum(cur[r-1:r+2, c-1:c+2]) - cur[r, c]
# Update cell colors based on Conway's Game of Life rules
if cur[r, c] == 1 and num_alive < 2 or num_alive > 3:
col = col_about_to_die
elif (cur[r, c] == 1 and 2 <= num_alive <= 3) or (cur[r, c] == 0 and num_alive == 3):
nxt[r, c] = 1
col = col_alive
# Draw cells on the surface with appropriate colors
col = col if cur[r, c] == 1 else col_background
pygame.draw.rect(surface, col, (c*sz, r*sz, sz-1, sz-1))
# Display the current generation information on the surface
gentext = myfont.render("Generation: {0}".format(gen), 1, (255, 255, 255))
surface.blit(gentext, (0, len(cur)*sz-16))
return nxt
# Initialize the game grid with a given x and y size
def init(dimx, dimy, pattern):
cells = np.zeros((dimy, dimx))
cells = pattern
return cells
# Initialize the game grid with gliders pattern
def init_gliders(dimx, dimy, pattern):
cells = np.zeros((dimy, dimx))
# Define patterns for one or two Gosper's glider guns
if('1' in pattern):
# A single Gosper's glider gun creating gliders
pattern = np.array([[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,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,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,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,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,0,0,0],
[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,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,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,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,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,0,0,0]])
if('2' in pattern):
# Two Gosper's glider gun creating gliders
pattern = np.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,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,0,0,0,0,0,0,1,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,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,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,1,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,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]])
# Position the pattern in the game grid
pos = (3,3)
cells[pos[0]:pos[0]+pattern.shape[0], pos[1]:pos[1]+pattern.shape[1]] = pattern
return cells
# Main function to run the game
def main(dimx, dimy, cellsize, pattern):
pygame.init()
surface = pygame.display.set_mode((dimx * cellsize, dimy * cellsize))
pygame.display.set_caption("Py Game of Life")
# Initialize the game grid based on user input
if('yes' in pattern):
cells = init_gliders(dimx, dimy, pattern)
else:
cells = init(dimx, dimy, pattern)
gen= 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
return
# Fill the surface with the grid color
surface.fill(col_grid)
# Update the game state and draw cells
cells = update(surface, cells, cellsize, gen)
pygame.display.update()
gen+= 1
# Get user input to determine the game setup
gliders = input("Run a Gosper's Glider Gun, creating gliders? (Yes, No): ").lower()
if(gliders == 'yes'):
gliders += input("1 or 2 Gliders? (1,2): ")
if(gliders == 'no'):
n= int(input("Enter world width: "))
m= int(input("Enter world height: "))
s= int(input("Enter cell size: "))
c= float(input("Determine chance of each cell starting with a life (0:1): "))
pattern = np.random.choice([0, 1], size=(n,m), p=[1-c, c])
if __name__ == "__main__":
if(gliders == 'no'):
main(n, m, s, pattern)
if('yes' in gliders):
main(120, 90, 8, gliders)