Hi all!
I decided to take a break from my latest huge project, and I thought, “What the heck, I’ll make a snake game.”
So here you go. Enjoy!
--# Main
-- Snake
displayMode(FULLSCREEN)
supportedOrientations(PORTRAIT)
function setup()
start()
bar = HEIGHT-WIDTH
joyPos = vec2(WIDTH-bar,bar/2)
light = color(110, 120, 130, 255)
-- medium = color(70, 80, 90, 255)
medium = color(100, 110, 120, 255)
dark = color(30, 40, 50, 255)
drawButtons()
fontSize(48)
font("Inconsolata")
font("Futura-CondensedExtraBold")
end
function draw()
background(light)
counter = counter + DeltaTime
if counter > delay then
while counter > delay do counter = counter - delay end
if endGame == false and pause == false then move() end
end
pushMatrix()
translate(0,HEIGHT-WIDTH)
scale(32)
noSmooth()
-- sprite(screen,12,16)
fill(medium)
for i = 1,24 do
for j = 1,24 do
rect(i-0.9,j-0.9,0.8,0.8)
end
end
fill(dark)
stroke(dark)
for i,s in pairs(snake) do
rect(s.x-0.9,s.y-0.9,0.8,0.8)
end
rect(pnt.x-0.9,pnt.y-0.9,0.8,0.8)
popMatrix()
pushStyle()
stroke(dark)
strokeWidth(10)
fill(medium)
-- rect(0,0,WIDTH,bar)
rect(0,-10,WIDTH,bar+10)
popStyle()
noSmooth()
sprite(pad,joyPos.x,joyPos.y,bar*3/4)
if joyDir == "up" then
sprite(joystick,joyPos.x,joyPos.y+80,bar/2)
elseif joyDir == "down" then
sprite(joystick,joyPos.x,joyPos.y-80,bar/2)
elseif joyDir == "left" then
sprite(joystick,joyPos.x-80,joyPos.y,bar/2)
elseif joyDir == "right" then
sprite(joystick,joyPos.x+80,joyPos.y,bar/2)
else
sprite(joystick,joyPos.x,joyPos.y,bar/2)
end
sprite(joystick,48,bar-48,32)
sprite(joystick,WIDTH-48,bar-48,32)
sprite(joystick,48,48,32)
sprite(joystick,WIDTH-48,48,32)
stroke(medium)
strokeWidth(8)
line(48,36,48,60)
line(36,bar-48,60,bar-48)
line(WIDTH-48,bar-36,WIDTH-48,bar-60)
line(WIDTH-36,48,WIDTH-60,48)
pushMatrix()
translate(WIDTH/2-bar/1.5,bar/2)
fontSize(48)
sprite(reset,0,0,bar/1.2)
if endGame == true then
text("Restart",0,0)
elseif pause == false then
text("Pause",0,0)
else
text("Play",0,0)
end
fontSize(32)
text("Score "..score,0,88)
text("Highscore "..math.ceil(highscore),0,-88)
popMatrix()
if endGame == true then
if score > highscore then saveLocalData("highscore",score) end
if score > highscore and math.ceil(ElapsedTime)%2 == 1 then
pushStyle()
fontSize(64)
textAlign(CENTER)
text("New\
High Score", WIDTH/2,HEIGHT/2+bar/2)
popStyle()
elseif math.ceil(ElapsedTime)%2 == 1 then
pushStyle()
fontSize(64)
textAlign(CENTER)
text("Game Over", WIDTH/2,HEIGHT/2+bar/2)
popStyle()
end
end
end
function move()
local x,y = snake[1].x,snake[1].y
local grow
if direct == "up" then y = y + 1
if pnt == vec2(x,y) then grow = true end
if y > 24 then endGame = true end
for i = 2,#snake-1 do if vec2(x,y) == snake[i] then endGame = true end end
end
if direct == "down" then y = y - 1
if pnt == vec2(x,y) then grow = true end
if y < 1 then endGame = true end
for i = 2,#snake-1 do if vec2(x,y) == snake[i] then endGame = true end end
end
if direct == "left" then x = x - 1
if pnt == vec2(x,y) then grow = true end
if x < 1 then endGame = true end
for i = 2,#snake-1 do if vec2(x,y) == snake[i] then endGame = true end end
end
if direct == "right" then x = x + 1
if pnt == vec2(x,y) then grow = true end
if x > 24 then endGame = true end
for i = 2,#snake-1 do if vec2(x,y) == snake[i] then endGame = true end end
end
if endGame == false then
if grow == true then newPos = snake[#snake] end
for i = 1,#snake-1 do
snake[#snake-i+1] = snake[#snake-i]
end
if grow == true then
table.insert(snake,newPos)
pnt = vec2(math.random(1,24),math.random(1,24))
delay = delay * 0.95
if delay < 0.1 then delay = 0.1 end
score = score + 1
end
snake[1] = vec2(x,y)
end
grow = false
prevDir = direct
end
function touched(touch)
if touch.state == BEGAN and vec2(touch.x,touch.y):dist(joyPos) < 50 and touch.x > WIDTH/2 then
active = true
joyId = touch.id
elseif touch.state == BEGAN and touch.x < WIDTH/2 and touch.y < bar then
buttonId = touch.id
end
if touch.state ~= ENDED and joyId == touch.id then
dx,dy = touch.x-joyPos.x,touch.y-joyPos.y
if vec2(touch.x,touch.y):dist(joyPos) > 50 and active == true then
if dy > math.abs(dx) and prevDir ~= "down" then direct = "up"
elseif dy < -math.abs(dx) and prevDir ~= "up" then direct = "down"
elseif dx < -math.abs(dy) and prevDir ~= "right" then direct = "left"
elseif dx > math.abs(dy) and prevDir ~= "left" then direct = "right"
end
if dy > math.abs(dx) then joyDir = "up"
elseif dy < -math.abs(dx) then joyDir = "down"
elseif dx < -math.abs(dy) then joyDir = "left"
elseif dx > math.abs(dy) then joyDir = "right"
end
elseif joyId == touch.id then
joyDir = "center"
end
else
active = false
joyDir = "center"
end
if touch.state == ENDED and buttonId == touch.id and touch.tapCount > 0 then
if endGame == true then restartGame() else pauseGame() end
end
end
function start()
snake = {vec2(12,12),vec2(12,11),vec2(12,10)}
prevDir = "up"
direct = "up"
joyDir = "center"
delay = 0.5
counter = 0
pnt = vec2(math.random(1,24),math.random(1,24))
endGame = false
pause = false
score = 0
highscore = readLocalData("highscore",0)
end
function pauseGame()
if pause == true then pause = false else pause = true end
end
function restartGame()
start()
end
function drawButtons()
joystick = image(8,8)
setContext(joystick)
fill(dark)
ellipse(4,4,8)
for i = 1,4 do
for j = 1,4 do
local r,g,b,a = joystick:get(i,j)
if a > 0 then
joystick:set(i,j,dark)
joystick:set(9-i,j,dark)
joystick:set(i,9-j,dark)
joystick:set(9-i,9-j,dark)
end
end
end
pad = image(12,12)
setContext(pad)
stroke(dark)
strokeWidth(0.1)
noFill()
ellipse(6,6,12)
for i = 1,6 do
for j = 1,6 do
local r,g,b,a = pad:get(i,j)
if a > 0 then
pad:set(i,j,light)
pad:set(13-i,j,light)
pad:set(i,13-j,light)
pad:set(13-i,13-j,light)
end
end
end
reset = image(24,12)
setContext(reset)
for i = 2,23 do
reset:set(i,1,dark)
reset:set(i,2,dark)
reset:set(i,11,dark)
reset:set(i,12,dark)
end
for j = 2,11 do
reset:set(1,j,dark)
reset:set(2,j,dark)
reset:set(23,j,dark)
reset:set(24,j,dark)
end
reset:set(2,2,dark)
reset:set(23,2,dark)
reset:set(2,11,dark)
reset:set(23,11,dark)
setContext()
noStroke()
end