-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
485 lines (434 loc) · 13.5 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
local gameState = "menu"
local folder = love.filesystem.getSaveDirectory()
local path = folder.."/HiScore"
function getHiScore()
if not love.filesystem.getInfo("HiScore.txt") then
local newScore = love.filesystem.newFile("HiScore.txt")
newScore:open("w")
newScore:write("0")
newScore:close()
scoreData = newScore:read()
scoreData = tonumber(scoreData)
newScore:write(tostring(scoreData))
newScore:close()
end
local scoreFile = love.filesystem.newFile("HiScore.txt")
scoreFile:open("r")
high_score = scoreFile:read()
high_score = tonumber(high_score)
end
function updateHiScore()
if score > high_score then
local scoreFile = love.filesystem.newFile("HiScore.txt")
scoreFile:open("w")
scoreFile:write(tostring(score))
scoreFile:close()
getHiScore()
end
end
function menu(xDim, yDim, yMenuPos)
menuTbl = {}
local opts = {"easy", "medium", "hard"}
local diffTimers = {0.3, 0.15, 0.06}
for i = 1, #opts do
table.insert(menuTbl,
{text = opts[i], timer = diffTimers[i], x = 10,
y = yMenuPos + yDim*(i-1),
wdth = xDim, hght = yDim})
end
end
local function start()
height = love.graphics.getHeight();
width = love.graphics.getWidth();
cellSize = 15
heightMod = 20
widthMod = 25
boxHeight = heightMod * cellSize
boxWidth = widthMod * cellSize
startX = (width / 2) - (boxWidth / 2)
startY = (height / 2) - (boxHeight / 2)
snake = {
{x = (2*cellSize) + startX , y = 0 + startY},
{x = (1*cellSize) + startX, y = 0 + startY},
{x = 0 + startX, y = 0 + startY}
}
touchCntrlTbl = {}
gameOverTimer = 3
foodTbl = {}
wurms = {}
spawnFood(foodTbl);
snakeDir = "right"
dirQueue = "left"
bullets = {}
scoreTimer = 3
scoreTimerMax = scoreTimer
score = 0
getHiScore()
menu(startX - 10, boxHeight / 3, startY)
end
local function moveSnake(dir)
local nextXPos = snake[1].x
local nextYPos = snake[1].y
if dir == "right" then
nextXPos = nextXPos + cellSize
elseif dir == "left" then
nextXPos = nextXPos - cellSize
elseif dir == "down" then
nextYPos = nextYPos + cellSize
elseif dir == "up" then
nextYPos = nextYPos - cellSize
end
table.insert(snake, 1,
{x = nextXPos, y = nextYPos})
if snake[1].x == foodTbl[1].x and snake[1].y == foodTbl[1].y then
table.remove(foodTbl)
spawnFood(foodTbl)
score = score + 5 * math.floor(scoreTimer + 1)
scoreTimer = scoreTimerMax
else
table.remove(snake)
end
end
function offScreen(obj)
if obj.x < startX or
obj.x > (startX + (widthMod - 1) * cellSize) or
obj.y < startY or
obj.y > (startY + (heightMod - 1) * cellSize) then
return true
end
end
function isAliveCheck()
if offScreen(snake[1]) then
gameState = "game over"
end
for i = 2, #snake do
if collisionChck(snake[1], snake[i]) then
gameState = "game over"
end
end
for index, wurm in ipairs(wurms) do
for j, sctn in ipairs(snake) do
for i, seg in ipairs(wurm) do
if sctn.x == seg.x and
sctn.y == seg.y then
gameState = "game over"
end
end
end
end
end
function love.keypressed(key, scancode, isrepeat)
if key == "up" then
dirQueue = "up"
elseif key == "down" then
dirQueue = "down"
elseif key == "right" then
dirQueue = "right"
elseif key == "left" then
dirQueue = "left"
end
end
function compareDir()
if dirQueue == "up" and snakeDir ~= "down" then
snakeDir = dirQueue
elseif dirQueue == "down" and snakeDir ~= "up" then
snakeDir = dirQueue
elseif dirQueue == "right" and snakeDir ~= "left" then
snakeDir = dirQueue
elseif dirQueue == "left" and snakeDir ~= "right" then
snakeDir = dirQueue
end
end
function love.mousepressed(x, y, button, isTouch, presses)
if gameState == "menu" then
for i = 1, #menuTbl do
if x >= menuTbl[i].x and x <= menuTbl[i].x + menuTbl[i].wdth and
y >= menuTbl[i].y and y <= menuTbl[i].y + menuTbl[i].hght then
timer = menuTbl[i].timer
timerMax = timer
gameState = "playing"
end
end
end
end
function love.touchpressed(id, x, y, dx, dy)
if gameState == "playing" then
table.insert(touchCntrlTbl,
{id = id, x = x, y = y,
dx = 0, dy = 0})
elseif gameState == "menu" then
for i = 1, #menuTbl do
if x >= menuTbl[i].x and x <= menuTbl[i].x + menuTbl[i].wdth and
y >= menuTbl[i].y and y <= menuTbl[i].y + menuTbl[i].hght then
timer = menuTbl[i].timer
timerMax = timer
gameState = "playing"
end
end
end
end
function love.touchmoved(id, x, y, dx, dy)
if gameState == "playing" and touchCntrlTbl[1] then
touchCntrlTbl[1].dx = touchCntrlTbl[1].dx + dx
touchCntrlTbl[1].dy = touchCntrlTbl[1].dy + dy
end
end
function love.touchreleased(id, x, y, dx, dy)
if gameState == "playing" and touchCntrlTbl[1] then
local xDist = touchCntrlTbl[1].dx
local yDist = touchCntrlTbl[1].dy
if math.abs(xDist) > math.abs(yDist) then
if math.abs(xDist) >= 5 then
if xDist > 0 and dirQueue ~= "left" then
dirQueue = "right"
elseif dirQueue ~= "right" then
dirQueue = "left"
end
end
elseif math.abs(yDist) > math.abs(xDist) then
if math.abs(yDist) >= 5 then
if yDist > 0 and dirQueue ~= "up" then
dirQueue = "down"
elseif dirQueue ~= "down" then
dirQueue = "up"
end
end
end
table.remove(touchCntrlTbl, 1)
end
end
function spawnFood(tbl)
local spwnPnts = {}
local possibleDir = {"up", "down", "left", "right"}
for xPos = 0, widthMod - 1 do
for yPos = 0, heightMod - 1 do
local spwnX = (xPos*cellSize) + startX
local spwnY = (yPos*cellSize) + startY
local viableSpwnPnt = true
for index, segment in ipairs(snake) do
if segment.x == spwnX and segment.y == spwnY then
viableSpwnPnt = false
end
end
for index, wurm in ipairs(wurms) do
for i, seg in ipairs(wurm) do
if seg.x == spwnX and seg.y == spwnY then
viableSpwnPnt = false
end
end
end
if viableSpwnPnt then
table.insert(spwnPnts, {x = spwnX, y = spwnY,
activeHazardTimer = 3, shotDelay = 0,
shotDelayMax = 1, release = false,
shotDir = possibleDir[love.math.random(1, #possibleDir)]})
end
end
end
table.insert(tbl, spwnPnts[love.math.random(1, #spwnPnts)])
end
function spawnBlltSeed(tbl)
local nextXPos = tbl.x
local nextYPos = tbl.y
if tbl.shotDir == "right" then
nextXPos = nextXPos + cellSize
elseif tbl.shotDir == "left" then
nextXPos = nextXPos - cellSize
elseif tbl.shotDir == "down" then
nextYPos = nextYPos + cellSize
elseif tbl.shotDir == "up" then
nextYPos = nextYPos - cellSize
end
table.insert(bullets,
{x = nextXPos, y = nextYPos,
dir = tbl.shotDir})
end
function spawnWurm(tbl)
local nextXPos = tbl.x
local nextYPos = tbl.y
local applBirthX = nextXPos
local applBirthY = nextYPos
if tbl.shotDir == "right" then
nextXPos = nextXPos + cellSize
elseif tbl.shotDir == "left" then
nextXPos = nextXPos - cellSize
elseif tbl.shotDir == "down" then
nextYPos = nextYPos + cellSize
elseif tbl.shotDir == "up" then
nextYPos = nextYPos - cellSize
end
table.insert(wurms, {
{x = nextXPos, y = nextYPos,
dir = tbl.shotDir, appleX = applBirthX, appleY = applBirthY}
})
end
function moveWurms(wurm)
if #wurm < 1 then
return
end
local appleChck = false
--for index, wurm in ipairs(tbl) do
local nextXPos = wurm[1].x
local nextYPos = wurm[1].y
if wurm[1].dir == "right" then
nextXPos = nextXPos + cellSize
elseif wurm[1].dir == "left" then
nextXPos = nextXPos - cellSize
elseif wurm[1].dir == "down" then
nextYPos = nextYPos + cellSize
elseif wurm[1].dir == "up" then
nextYPos = nextYPos - cellSize
end
table.insert(wurm, 1,
{x = nextXPos, y = nextYPos,
dir = wurm[1].dir, appleX = wurm[1].appleX,
appleY = wurm[1].appleY})
for index, apple in ipairs(foodTbl) do
if wurm[1].appleX == apple.x and wurm[1].appleY == apple.y then
appleChck = true
break
end
end
if not appleChck then
table.remove(wurm)
end
if offScreen(wurm[1]) then
table.remove(wurm, 1)
end
--end
end
function moveBllts(tbl)
for index, bullet in ipairs(tbl) do
local nextXPos = bullet.x
local nextYPos = bullet.y
if bullet.dir == "right" then
nextXPos = nextXPos + cellSize
elseif bullet.dir == "left" then
nextXPos = nextXPos - cellSize
elseif bullet.dir == "down" then
nextYPos = nextYPos + cellSize
elseif bullet.dir == "up" then
nextYPos = nextYPos - cellSize
end
bullet.x = nextXPos
bullet.y = nextYPos
if offScreen(bullet) then
table.remove(tbl, index)
end
for i = 2, #snake do
if collisionChck(bullet, snake[i]) then
table.remove(tbl, index)
end
end
end
end
function collisionChck(obj1, obj2)
if obj1.x == obj2.x and obj1.y == obj2.y then
return true
end
end
local function drwElements(tbl, red, green, blue, alpha)
alpha = alpha or 1
love.graphics.setColor(red,green,blue, alpha)
for index, item in ipairs(tbl) do
love.graphics.rectangle(
"fill",
item.x,
item.y,
cellSize - 1,
cellSize - 1)
end
end
function love.load()
start()
end
function love.update(dt)
if gameState == "playing" then
if timer <= 0 then
compareDir()
moveSnake(snakeDir)
for index, wurm in ipairs(wurms) do
moveWurms(wurm)
end
moveBllts(bullets)
isAliveCheck()
updateHiScore()
timer = timerMax
end
timer = timer - dt
if scoreTimer > 0 then
scoreTimer = scoreTimer - dt
else
scoreTimer = 0
end
for index, apple in ipairs(foodTbl) do
if apple.activeHazardTimer <= 0 and apple.release == false then
--if apple.shotDelay <= 0 then
apple.release = true
spawnWurm(apple)
--apple.shotDelay = apple.shotDelayMax
--else
--apple.shotDelay = apple.shotDelay - dt
--end
else
apple.activeHazardTimer = apple.activeHazardTimer - dt
apple.shotDelay = apple.shotDelay - dt
if apple.shotDelay <= 0 and apple.release == false then
spawnBlltSeed(apple)
apple.shotDelay = apple.shotDelayMax
else
apple.shotDelay = apple.shotDelay - dt
end
end
end
elseif gameState == "game over" then
if gameOverTimer <= 0 then
gameOverTimer = 3
gameState = "menu"
start()
end
gameOverTimer = gameOverTimer - dt
end
end
function love.draw()
love.graphics.setColor(1,1,1)
love.graphics.print("High Score: "..high_score, 10, 0)
love.graphics.print("Score: "..score, 250, 0)
--draw the field
love.graphics.rectangle("line",
startX,
startY,
boxWidth, boxHeight)
--draw the snake
if gameState == "playing" then
love.graphics.setScissor(startX,
startY,
boxWidth + 1,
boxHeight)
drwElements(snake, 0, 1, 0)
--draw the apples
drwElements(foodTbl, 1, 0, 0)
--draw the wurms
for index, wurm in ipairs(wurms) do
drwElements(wurm, 0, 1, 1)
end
--draw the bullets
for index, bullet in ipairs(bullets) do
drwElements(bullets, 1, 1, 1, 0.5)
end
love.graphics.setScissor()
elseif gameState == "menu" then
--draw the menu
love.graphics.setColor(0,1,1)
for index, opt in ipairs(menuTbl) do
love.graphics.rectangle("line",
opt.x,
opt.y,
opt.wdth,
opt.hght)
love.graphics.print(opt.text, opt.x + 10, opt.y + 10)
end
else
love.graphics.print("Game over "..math.floor(gameOverTimer), startX, startY)
end
end