forked from 5outh/Game-of-Life
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
333 lines (292 loc) · 7.25 KB
/
main.lua
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
function love.load()
ggg = love.filesystem.load("Presets/GosperGliderGun.lua")
crush = love.audio.newSource("crush.mp3", "stream")
love.graphics.setMode(1000, 800, false, true, 8)
largeFont = love.graphics.newFont(36)
smallFont = love.graphics.newFont(12)
mediumFont = love.graphics.newFont(16)
nodeSize = 12
xSize = 864
ySize = 576
xNodes = xSize /nodeSize
yNodes = ySize / nodeSize
startButton = {
x = love.graphics.getWidth() - 200,
y = 20,
width = 160,
height = 30
}
smallSquare ={
width = 24,
height = 24,
x = 20,
y = 32
}
xPosition = (love.graphics.getWidth() - xSize- nodeSize) / 2
yPosition = (love.graphics.getHeight() - ySize- nodeSize) * 2/3
array = {0,0}
for i = 0, xNodes do
array[i] = {}
for j = 0, yNodes do
array[i][j] = 0
end
end
gameStarted = false
rainbowMode = false
musicOn = false
love.graphics.setBackgroundColor(34, 32, 40)
end
function get_next(cur_array)
vals = ''
newArray = {}
for i = 0, xNodes do
newArray[i] = {}
for j = 0, yNodes do
newArray[i][j] = 0
end
end
for i = 1, xNodes do
for j = 1, yNodes do
neighbors = 0
left = i == 1
right = i == xNodes
bottom = j == yNodes
top = j == 1
if left then
neighbors = neighbors + array[xNodes][j]
else
neighbors = neighbors + array[i-1][j]
end
if right then
neighbors = neighbors + array[1][j]
else
neighbors = neighbors + array[i+1][j]
end
if top then
neighbors = neighbors + array[i][yNodes]
else
neighbors = neighbors + array[i][j-1]
end
if bottom then
neighbors = neighbors + array[i][1]
else
neighbors = neighbors + array[i][j+1]
end
if left or bottom then
if left and bottom then
neighbors = neighbors + array[xNodes][1]
elseif left then
neighbors = neighbors + array[xNodes][j+1]
elseif bottom then
neighbors = neighbors + array[i-1][1]
end
else
neighbors = neighbors + array[i-1][j+1]
end
if right or bottom then
if right and bottom then
neighbors = neighbors + array[1][1]
elseif right then
neighbors = neighbors + array[1][j+1]
elseif bottom then
neighbors = neighbors + array[i+1][1]
end
else
neighbors = neighbors + array[i+1][j+1]
end
if top or left then
if top and left then
neighbors = neighbors + array[xNodes][yNodes]
elseif top then
neighbors = neighbors + array[i-1][yNodes]
elseif left then
neighbors = neighbors + array[xNodes][j-1]
end
else
neighbors = neighbors + array[i-1][j-1]
end
if top or right then
if top and right then
neighbors = neighbors + array[1][yNodes]
elseif top then
neighbors = neighbors + array[i+1][yNodes]
elseif right then
neighbors = neighbors + array[1][j-1]
end
else
neighbors = neighbors + array[i+1][j-1]
end
-- handle node generation, update array
if array[i][j] == 1 then
if neighbors == 2 or neighbors == 3 then
newArray[i][j] = 1
else
newArray[i][j] = 0
end
elseif array[i][j] == 0 then
if neighbors == 3 then
newArray[i][j] = 1
else
newArray[i][j] = 0
end
end
end
end
return newArray
end
function love.update(dt)
if gameStarted then
array = get_next(array)
end
-- node population
if love.mouse.isDown("r", "l") then
x = love.mouse.getX() - xPosition
y = love.mouse.getY() - yPosition
if x > 0 and y > 0 then
xCoordinate = (x - (x % nodeSize)) /nodeSize
yCoordinate = (y - (y % nodeSize)) /nodeSize
if xCoordinate < #array and yCoordinate < #array[1] then
if love.mouse.isDown("r") then
array[xCoordinate][yCoordinate] = 0
elseif love.mouse.isDown("l") then
array[xCoordinate][yCoordinate] = 1
end
end
end
end
end
function love.draw(dt)
love.graphics.setColor(197, 199, 182) -- top banner and grid (grey)
love.graphics.rectangle('fill', 0, 0, love.graphics.getWidth(), love.graphics.getHeight()/12)
step = nodeSize
while(step <= xSize) do
if(step <= ySize) then
love.graphics.line(nodeSize+xPosition, step+yPosition, xSize+xPosition, step+yPosition)
end
love.graphics.line(step+xPosition, nodeSize+yPosition, step+xPosition, ySize+yPosition)
step = step +nodeSize
end
love.graphics.setColor(255,248,211) -- nodes (off-white)
for i = 1, xNodes-1 do
for j = 1, yNodes-1 do
if array[i][j] == 1 then
if rainbowMode then
love.graphics.setColor(math.random(255), math.random(255), math.random(255))
end
love.graphics.rectangle('fill', i*nodeSize + xPosition, j*nodeSize + yPosition, nodeSize, nodeSize)
end
end
end
if rainbowMode then
love.graphics.setFont(largeFont)
love.graphics.print("FUCKIN RAINBOWMODE", 300, 80, 0)
love.graphics.setFont(mediumFont)
end
love.graphics.setColor(47, 56, 55) -- top banner underline, start button
love.graphics.rectangle('fill', startButton.x, startButton.y, startButton.width, startButton.height)
love.graphics.rectangle('fill', 0, love.graphics.getHeight()/12, love.graphics.getWidth(), love.graphics.getHeight()/192)
love.graphics.setColor(255, 248, 211) -- off-white
love.graphics.setFont(mediumFont)
if(gameStarted) then
love.graphics.print("Pause Game", startButton.x + 35, startButton.y - 5 + startButton.height /3, 0)
else
love.graphics.print("Start Game", startButton.x + 35, startButton.y - 5 + startButton.height /3, 0)
end
--love.graphics.print(love.mouse.getX()..','..love.mouse.getY(), love.mouse.getX(), love.mouse.getY())
end
function love.mousepressed(x, y, button)
if button == "l" then
if(onButton(x, y, startButton)) then
startPause()
end
end
end
function startPause()
if(rainbowMode) then
gameStarted = false
love.audio.pause(crush)
musicOn = false
rainbowMode = false
else
gameStarted = not gameStarted
end
end
function onButton(x, y, button)
return x >= button.x and x < button.x + button.width and y >= button.y and y <= button.y + button.height
end
function love.keypressed(key)
if key == "r" then
rainbowMode = not rainbowMode
gameStarted = rainbowMode
musicOn = not musicOn
if(musicOn) then
love.audio.play(crush)
love.audio.resume(crush)
else
love.audio.pause(crush)
end
end
if key == "c" then
clearArray()
end
if key == "p" then
startPause()
end
if key == "up" then
if nodeSize < 24 then
changeSize(6)
end
end
if key == "down" then
if nodeSize > 6 then
changeSize(-6)
end
end
end
function clearArray()
array = {0,0}
for i = 0, xNodes do
array[i] = {}
for j = 0, yNodes do
array[i][j] = 0
end
end
end
function changeSize(x)
prevXNodes = xNodes
prevYNodes = yNodes
nodeSize = nodeSize + x
xNodes = xSize / nodeSize
yNodes = ySize / nodeSize
resize(prevXNodes, prevYNodes, xNodes, yNodes)
end
function resize(xNodes, yNodes, newXNodes, newYNodes)
if xNodes <= newXNodes then
newArray = {0,0}
for i = 0, newXNodes do
newArray[i] = {}
for j = 0, newYNodes do
newArray[i][j] = 0
end
end
for i = 0, xNodes do
for j = 0, yNodes do
newArray[i][j] = array[i][j]
end
end
else
newArray = {0,0}
for i = 0, xNodes do
newArray[i] = {}
for j = 0, yNodes do
newArray[i][j] = 0
end
end
for i = 0, newXNodes do
for j = 0, newYNodes do
newArray[i][j] = array[i][j]
end
end
end
array = newArray
end