-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
263 lines (199 loc) · 6.19 KB
/
game.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import universe as u
import sys
import time
import random
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
from data import dbManager
from rule import Rule
from algorithm.init import getBaysSpaceRule
if len(sys.argv) != 4:
print("\nERROR: game.py usage: \n\npython game.py"
+ "\n\t[rule_name] "
+ "\n\t[window's dimension] "
+ "\n\t[universe's dimension]")
exit(-1)
BLUE = [0.0, 0.0, 1.0]
GREEN = [0.0, 1.0, 0.0]
RED = [1.0, 0.0, 0.0]
YELLOW = [1.0, 1.0, 0.0]
BLACK = [0.0, 0.0, 0.0]
WHITE = [1.0, 1.0, 1.0]
GRAY = [0.15, 0.15, 0.15]
PINK = [1.0, 0.0, 1.0]
PURPLE = [0.5, 0.0, 1.0]
winColor = BLACK
gridColor = GRAY
cellColor = WHITE
# Window dimensions
dimXwindow = int(sys.argv[2])
dimYwindow = int(sys.argv[2])
# Universe dimensions
dimXgrid = int(sys.argv[3])
dimYgrid = int(sys.argv[3])
cellSize = dimXwindow / dimXgrid
# Universe's rule
try:
lifeRule = Rule(dbManager.getRuleByName(sys.argv[1]))
rule = lifeRule.getRule()
except:
initialRuleName = sys.argv[1]
splitRule = initialRuleName.split("/")
Eb = int(splitRule[0][0])
Eh = int(splitRule[0][1])
Fb = int(splitRule[1][0])
Fh = int(splitRule[1][1])
initialRule = (Eb, Eh, Fb, Fh)
rule = getBaysSpaceRule(initialRule).getRule()
# First state of the universe
universe = [[ 1 for i in range(dimXgrid)] for j in range(dimYgrid)]
speed = 0.1
stop = True
paintGrid = True
# Window setup
def init():
global winColor
glClearColor(winColor[0], winColor[1], winColor[2], 1)
glMatrixMode(GL_PROJECTION)
gluOrtho2D( 0, dimXwindow, -dimYwindow, 0 )
# Returns the cell's position on the universe
def getCell(posX, posY):
x = int(posX / (dimXwindow / dimXgrid))
y = int(posY / (dimYwindow / dimYgrid))
return x,y
# Returns the real cell's position on the window
def getRealPosition(xCell, yCell):
x = int(xCell * (dimXwindow / dimXgrid))
y = -1 * int(yCell * (dimYwindow / dimYgrid))
return x,y
# Draws all the living cells
def drawAliveCells():
for j in range(dimYgrid):
for i in range(dimXgrid):
if universe[j][i] == 0:
x,y = getRealPosition(i,j)
drawSquare(x,y)
glFlush()
def drawSquare(xPos, yPos):
global cellColor
glColor3fv(cellColor)
glBegin(GL_POLYGON)
glVertex2f(xPos, yPos)
glVertex2f(xPos + cellSize, yPos)
glVertex2f(xPos + cellSize, yPos - cellSize)
glVertex2f(xPos, yPos - cellSize)
glEnd()
# Draws a grid on the universe
def grid():
global paintGrid
glColor3fv(gridColor)
glLineWidth(0.001)
glBegin(GL_LINES)
saltoX = dimXwindow / dimXgrid
saltoY = - dimYwindow / dimYgrid
i = 0
while i <= dimXwindow:
glVertex2f(i,0)
glVertex2f(i,-dimYwindow)
i += saltoX
j = 0
while j >= -dimYwindow:
glVertex2f(0,j)
glVertex2f(dimXwindow,j)
j += saltoY
glEnd()
# Mouse and Keyboard actions
def mouseHandler(button, state, x, y):
global universe
global stop
# When clicked, a cell is reborned or killed
if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN :
xCell, yCell = getCell(x, y)
previousState = universe[yCell][xCell]
universe[yCell][xCell] = 0 if previousState == 1 else 1
def keyboard(key, x, y):
global stop
global paintGrid
global universe
global winColor
global cellColor
# Clears the universe
if key == b'c' or key == b'C':
universe = [[ 1 for i in range(dimXgrid)] for j in range(dimYgrid)]
# Random universe generator
elif key == b'r' or key == b'R':
universe = [[ random.choice([1,1,1,1,1,0]) for i in range(dimXgrid)] for j in range(dimYgrid)]
# Random universe generator inside a 40x40 middle square
elif key == b's' or key == b'S':
centerY = int(dimYgrid/2)
centerX = int(dimXgrid/2)
for j in range(dimYgrid):
for i in range(dimXgrid):
if i >= centerX - 20 and i <= centerX + 20 and j >= centerY - 20 and j <= centerY + 20:
universe[j][i] = random.choice([1,1,1,0])
else:
universe[j][i] = 1
# Random universe generator inside a 10x10 middle square
elif key == b'a' or key == b'A':
centerY = int(dimYgrid/2)
centerX = int(dimXgrid/2)
for j in range(dimYgrid):
for i in range(dimXgrid):
if i >= centerX - 5 and i <= centerX + 5 and j >= centerY - 5 and j <= centerY + 5:
universe[j][i] = random.choice([1,0])
else:
universe[j][i] = 1
# Freeze the current universe state
elif key == b' ':
stop = not stop
# Draws the grid
elif key == b'g' or key == b'B':
paintGrid = not paintGrid
# Changes color of the automata
elif (key == b'm' or key == b'M') and cellColor == WHITE:
cellColor = BLACK
winColor = WHITE
glClearColor(winColor[0], winColor[1], winColor[2], 1)
elif (key == b'm' or key == b'M') and cellColor == BLACK:
cellColor = WHITE
winColor = BLACK
glClearColor(winColor[0], winColor[1], winColor[2], 1)
glutPostRedisplay()
def specialKeyboard(key, x, y):
global speed
global universe
global rule
# Speeds up the game
if key == GLUT_KEY_UP:
speed -= speed * 0.5
# Speeds down the game
if key == GLUT_KEY_DOWN:
speed += speed * 0.5
# If the game is stopped, paints next state
if key == GLUT_KEY_RIGHT and stop:
universe = u.nextState(rule, universe)
glutPostRedisplay()
def main():
global universe
global stop
global rule
glClear(GL_COLOR_BUFFER_BIT)
if paintGrid:
grid()
drawAliveCells()
if not stop:
time.sleep(speed)
universe = u.nextState(rule, universe)
glutPostRedisplay()
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowPosition(200,200)
glutInitWindowSize(dimXwindow,dimYwindow)
glutCreateWindow(b'Game of Life')
init()
glutDisplayFunc(main)
glutMouseFunc(mouseHandler)
glutKeyboardFunc(keyboard)
glutSpecialFunc(specialKeyboard)
glutMainLoop()