-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
212 lines (195 loc) · 5.5 KB
/
main.lua
File metadata and controls
212 lines (195 loc) · 5.5 KB
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
-- PUSH
local push = require ("libs/push/push")
-- WINDOW
local gameWidth, gameHeight = 1024, 768
local windowWidth, windowHeight = love.window.getDesktopDimensions()
push:setupScreen(gameWidth, gameHeight, windowWidth, windowHeight, {fullscreen = true})
-- GLOBAL VARIABLES
gridXCount = 32
gridYcount = 24
cellSize = 32
lives = 3
score = 0
isHighScore = false
isGameOver = false
-- PLAYGROUND
local playground = require("playground")
-- UI Require
local ui = require("ui")
function love.load()
timerSpeed = 0.12
foodPosition = {
x = love.math.random(playground.startX, gridXCount),
y = love.math.random(playground.startY, gridYCount),
}
reset()
-- Fonts
font = love.graphics.newFont("fonts/PressStart2P.ttf", 24)
-- Keymap
keyMap = {
escape = function() love.event.quit(0) end,
left = function()
if directionQueue[#directionQueue] ~= 'left' and directionQueue[#directionQueue] ~= 'right' then
table.insert(directionQueue, 'left')
end
end,
right = function()
if directionQueue[#directionQueue] ~= 'right' and directionQueue[#directionQueue] ~= 'left' then
table.insert(directionQueue, 'right')
end
end,
up = function()
if directionQueue[#directionQueue] ~= 'up' and directionQueue[#directionQueue] ~= 'down' then
table.insert(directionQueue, 'up')
end
end,
down= function()
if directionQueue[#directionQueue] ~= 'down' and directionQueue[#directionQueue] ~= 'up' then
table.insert(directionQueue, 'down')
end
end
}
end
function love.update(delta)
local canMove = true
timer = timer + delta
if snakeAlive then
if timer >= timerSpeed then
timer = 0
if #directionQueue > 1 then
table.remove(directionQueue, 1)
end
local nextXPos = snakeSegments[1].x
local nextYPos = snakeSegments[1].y
if directionQueue[1] == 'left' then
nextXPos = nextXPos - 1
if nextXPos <= playground.startX then
canMove = false
end
elseif directionQueue[1] == 'right' then
nextXPos = nextXPos + 1
if nextXPos > gridXCount then
canMove = false
end
elseif directionQueue[1] == 'up' then
nextYPos = nextYPos - 1
if nextYPos <= playground.startY / cellSize then
canMove = false
end
elseif directionQueue[1] == 'down' then
nextYPos = nextYPos + 1
if nextYPos > gridYcount then
canMove = false
end
end
for segmentIndex, segment in ipairs(snakeSegments) do
if segmentIndex ~= #snakeSegments
and nextXPos == segment.x
and nextYPos == segment.y then
canMove = false
end
end
if canMove then
table.insert(snakeSegments, 1, {x = nextXPos, y = nextYPos})
if snakeSegments[1].x == foodPosition.x and snakeSegments[1].y == foodPosition.y then
moveFood()
score = score + 10
else
table.remove(snakeSegments)
end
else
handleSnakeDead()
if isGameOver then
fullReset()
end
end
end
elseif timer >= 2 then
love.load()
end
end
function love.draw()
push:start()
love.graphics.setFont(font)
love.graphics.setColor(.28, .28, .28)
love.graphics.rectangle(
'fill',
0,
0,
gridXCount * cellSize,
gridYcount * cellSize
)
for segmentIndex, segment in ipairs(snakeSegments) do
if snakeAlive then
love.graphics.setColor(.6, 1, .32)
else
love.graphics.setColor(.5, .5, .5)
end
drawCell(segment.x, segment.y)
end
love.graphics.setColor(1, .3, .3)
drawCell(foodPosition.x, foodPosition.y)
ui.draw()
push:finish()
end
function love.keypressed(key)
if(keyMap[key]) then
keyMap[key]()
end
end
function drawCell(x, y)
love.graphics.rectangle(
'fill',
(x - 1) * cellSize,
(y - 1) * cellSize,
cellSize - 1,
cellSize - 1
)
end
function moveFood()
local possibleFoodPositions = {}
for foodX = 1, gridXCount do
for foodY = 1, gridYcount do
local possible = true
for segmentIndex, segment in ipairs(snakeSegments) do
if foodX == segment.x and foodY == segment.y then
possible = false
end
if foodY <= (playground.startY / cellSize) then
possible = false
end
end
if possible then
table.insert(possibleFoodPositions, {x = foodX, y = foodY})
end
end
end
foodPosition = possibleFoodPositions[love.math.random(#possibleFoodPositions)]
end
function increaseDifficulty()
end
function handleSnakeDead()
snakeAlive = false
if lives > 0 then
lives = lives - 1
else
lives = 0
isGameOver = true
end
end
function reset()
snakeSegments = {
{x = 3, y = 4},
{x = 2, y = 4},
{x = 1, y = 4},
}
directionQueue = {'right'}
snakeAlive = true
timer = 0
moveFood()
isGameOver = false
end
function fullReset()
lives = 3
score = 0
end