Code: Select all
function load()
font = love.graphics.newFont(love.default_font, 12)
love.graphics.setFont(font)
message1 = "Snake"
message2 = "Press Space To Start"
peice = love.graphics.newImage("SnakePart.png")
started = false
direction = 4
PeicesX = {6,5,4,3,2,1}
PeicesY = {1,1,1,1,1,1}
Peices = 6
X=6
Y=1
Score = 0
time = 0
FoodX = math.random(1,25)
FoodY = math.random(1,18)
end
function draw()
if love.keyboard.isDown(love.key_space) then
started = true
end
if started == false then
love.graphics.draw(message1,400,200)
love.graphics.draw(message2,350,300)
else
for v = 1, Peices, 1 do
love.graphics.draw(peice,PeicesX[v]*32+16,PeicesY[v]*32+16)
end
love.graphics.draw(peice,FoodX*32+16,FoodY*32+16)
end
end
function play()
if direction == 1 then
Y=Y-1
end
if direction == 2 then
Y=Y+1
end
if direction == 3 then
X=X-1
end
if direction == 4 then
X=X+1
end
for v=Peices,1,-1 do
if v+1 <= Peices then
PeicesX[v+1]=PeicesX[v]
PeicesY[v+1]=PeicesY[v]
end
end
PeicesX[1]=X
PeicesY[1]=Y
if X==FoodX and Y==FoodY then
Peices=Peices+1
PeicesX[Peices]=PeicesX[Peices-1]
PeicesY[Peices]=PeicesY[Peices-1]
FoodX = math.random(1,25)
FoodY = math.random(1,18)
end
end
function reset()
started = false
direction = 4
PeicesX = {6,5,4,3,2,1}
PeicesY = {1,1,1,1,1,1}
Peices = 6
X=6
Y=1
Score = 0
time = 0
FoodX = math.random(1,25)
FoodY = math.random(1,18)
end
function update(dt)
time = dt + time
if time > .1 then
play()
time = 0
end
if love.keyboard.isDown(love.key_up) then
direction = 1
end
if love.keyboard.isDown(love.key_down) then
direction = 2
end
if love.keyboard.isDown(love.key_left) then
direction = 3
end
if love.keyboard.isDown(love.key_right) then
direction = 4
end
end