-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
268 lines (225 loc) · 7.33 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
push = require "push"
Class = require "class"
require "Box"
require "Grid"
-- WINDOW_WIDTH = 599
-- WINDOW_HEIGHT = 599
WINDOW_WIDTH = 1080
WINDOW_HEIGHT = 800
PASSED_TIME = 0
BOXES = {}
CONFIG = {
["choice"] = {
GRID_WIDTH = 4,
GRID_HEIGHT = 4,
-- GRID_START_X = 108,
-- GRID_START_Y = 108,
GRID_START_X = 338,
GRID_START_Y = 198,
GRID_GAP = 11,
BOX_WIDTH = 80,
BOX_HEIGHT = 80,
BOX_GAP = 21
},
["game"] = {
-- GRID_WIDTH = 16,
-- GRID_HEIGHT = 16,
-- GRID_START_X = 32,
-- GRID_START_Y = 32,
-- GRID_GAP = 5,
-- BOX_WIDTH = 25,
-- BOX_HEIGHT = 25,
-- BOX_GAP = 9
GRID_WIDTH = 30,
GRID_HEIGHT = 30,
GRID_START_X = 198,
GRID_START_Y = 58,
GRID_GAP = 4,
BOX_WIDTH = 16,
BOX_HEIGHT = 16,
BOX_GAP = 7
},
-- OFFSET_WIDTH = 6,
-- OFFSET_HEIGHT = 6
OFFSET_WIDTH = 13,
OFFSET_HEIGHT = 13,
REFRESH_TIME = 0.2
}
function love.load()
love.graphics.setDefaultFilter("nearest", "nearest")
love.window.setTitle("Game of Life")
smallFont = love.graphics.newFont("font.ttf", 20)
mediumFont = love.graphics.newFont("font.ttf", 32)
largeFont = love.graphics.newFont("font.ttf", 40)
extraLargeFont = love.graphics.newFont("font.ttf", 48)
push:setupScreen(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, {
fullscreen = false,
resizable = false,
vsync = true
})
gamestate = "choice" --initialize gamestate
config = CONFIG[gamestate] -- initialize config
grid = Grid(config) -- initialize grid
initialize_boxes() -- initialize boxes
end
function love.resize(width, height)
push:resize(width, height)
end
function love.update(dt)
PASSED_TIME = PASSED_TIME + dt
if PASSED_TIME < CONFIG.REFRESH_TIME then
return
end
love.graphics.clear()
PASSED_TIME = 0
if gamestate == "game" then
local change_x = {-1, -1, -1, 0, 0, 1, 1, 1}
local change_y = {-1, 0, 1, -1, 1, -1, 0, 1}
local neighbors = {}
for row = 0, config.GRID_HEIGHT - 1 do
neighbors[row] = {}
for col = 0, config.GRID_WIDTH - 1 do
local count = 0
for i = 1, 8 do
local _x = row + change_x[i]
local _y = col + change_y[i]
if
0 <= _x and
_x < config.GRID_HEIGHT and
0 <= _y and
_y < config.GRID_WIDTH and
BOXES[_x][_y].status
then
count = count + 1
end
end
neighbors[row][col] = count -- store alive neighbors' count
end
end
-- if cell is active and has no neighbors or more than 5 neighbors then make it inactive
-- if cell is inactive and has more than 3 neighbors make it active
-- if cell is at border make it inactive
for row = 0, config.GRID_HEIGHT - 1 do
for col = 0, config.GRID_WIDTH - 1 do
if BOXES[row][col].status then
if
neighbors[row][col] < 1 or
neighbors[row][col] > 5 or
row == 0 or
row == config.GRID_HEIGHT - 1 or
col == 0 or
col == config.GRID_WIDTH - 1
then
BOXES[row][col].status = false
end
else
if neighbors[row][col] >=3 then
BOXES[row][col].status = true
end
end
end
end
end
end
function love.draw()
grid:render()
print_instructions()
for row = 0, config.GRID_HEIGHT - 1 do
for col = 0, config.GRID_WIDTH - 1 do
BOXES[row][col]:render()
end
end
end
function print_instructions()
if gamestate == "choice" then
love.graphics.setFont(extraLargeFont)
love.graphics.print("Conway's Game of Life", 250, 60)
love.graphics.setFont(largeFont)
love.graphics.print("Pick configuration", 340, 620)
love.graphics.setFont(mediumFont)
love.graphics.print("Select - s", 280, 680)
love.graphics.print("Cancel - u", 600, 680)
love.graphics.print("Start - space", 280, 720)
love.graphics.print("Quit - esc", 600, 720)
elseif gamestate == "game" then
love.graphics.setFont(smallFont)
love.graphics.print("Quit - esc", 280, 770)
love.graphics.print("Return - space", 600, 770)
end
end
function initialize_boxes()
for row = 0, config.GRID_HEIGHT - 1 do
BOXES[row] = {}
for col = 0, config.GRID_WIDTH - 1 do
BOXES[row][col] = Box(
config.GRID_START_X + col * (config.BOX_WIDTH + config.BOX_GAP),
config.GRID_START_Y + row * (config.BOX_HEIGHT + config.BOX_GAP),
config.BOX_WIDTH,
config.BOX_HEIGHT
)
end
end
end
function alter_gamestate()
if gamestate == "choice" then
gamestate = "game"
config = CONFIG[gamestate]
local selected_status = {}
for row = 0, CONFIG["choice"].GRID_HEIGHT - 1 do
selected_status[row] = {}
for col = 0, CONFIG["choice"].GRID_WIDTH - 1 do
selected_status[row][col] = BOXES[row][col].status
end
end
initialize_boxes()
initialize_grid()
for row = 0, CONFIG["choice"].GRID_HEIGHT - 1 do
for col = 0, CONFIG["choice"].GRID_WIDTH - 1 do
BOXES[CONFIG.OFFSET_HEIGHT + row][CONFIG.OFFSET_WIDTH + col].status = selected_status[row][col]
end
end
else
gamestate = "choice"
config = CONFIG[gamestate]
initialize_grid()
initialize_boxes()
end
end
function love.keypressed(key)
if key == "escape" then
love.event.quit()
elseif key == "space" then
alter_gamestate()
elseif key == "s" and gamestate == "choice" then
for row = 0, config.GRID_HEIGHT - 1 do
for col = 0, config.GRID_WIDTH - 1 do
box = BOXES[row][col]
if
box.x <= love.mouse.getX() and
love.mouse.getX() <= box.x + box.width and
box.y <= love.mouse.getY() and
love.mouse.getY() <= box.y + box.height
then
box.status = true
end
end
end
elseif key == "u" and gamestate == "choice" then
for row = 0, config.GRID_HEIGHT - 1 do
for col = 0, config.GRID_WIDTH - 1 do
box = BOXES[row][col]
if
box.x <= love.mouse.getX() and
love.mouse.getX() <= box.x + box.width and
box.y <= love.mouse.getY() and
love.mouse.getY() <= box.y + box.height
then
box.status = false
end
end
end
end
end
function initialize_grid()
grid = Grid(config)
end