function love.load()
love.graphics.setScreen('bottom')
players = 2
lose = {}
oldSquare = {}
direction = {}
startx = {}
starty = {}
snake1 = {}
lose.snake1 = false
if players == 2 then
snake2 = {}
lose.snake2 = false
end
startx.snake1 = 40
startx.snake2 = 270
starty.snake1 = 110
starty.snake2 = 110
createSnake(snake1, 'snake1')
createSnake(snake2, 'snake2')
direction.snake1 = 'right'
direction.snake2 = 'left'
framecount = {}
framecount.snake1 = 0
framecount.snake2 = 0
love.graphics.setColor(255, 255, 255)
end
function love.draw()
for i, square in ipairs(snake1) do
love.graphics.setColor(255, 255, 255)
love.graphics.setScreen('bottom')
love.graphics.rectangle('fill', square.x, square.y, 10, 10)
end
if players == 2 then
for i, square in ipairs(snake2) do
love.graphics.setColor(127, 127, 127)
love.graphics.setScreen('bottom')
love.graphics.rectangle('fill', square.x, square.y, 10, 10)
end
end
love.graphics.setColor(255, 255, 255)
love.graphics.setScreen('top')
love.graphics.print(direction.snake1, 0,0)
love.graphics.setScreen('top')
love.graphics.print(direction.snake2, 300,0)
if lose.snake1 == true then
love.graphics.print('you lose', 0, 20)
end
if lose.snake2 == true then
love.graphics.print('you lose', 300, 20)
end
end
function love.update(dt)
moveSnake(snake1, 'snake1')
moveSnake(snake2, 'snake2')
end
function love.keypressed(key)
if key == 'start' then
love.event.quit()
end
if key == 'select' then
love.load()
end
if key == 'a' and direction ~= 'left' then
direction.snake2 = 'right'
end
if key == 'y' and direction ~= 'right' then
direction.snake2 = 'left'
end
if key == 'x' and direction ~= 'down' then
direction.snake2 = 'up'
end
if key == 'b' and direction ~= 'up' then
direction.snake2 = 'down'
end
if key == 'dright' and direction ~= 'left' then
direction.snake1 = 'right'
end
if key == 'dleft' and direction ~= 'right' then
direction.snake1 = 'left'
end
if key == 'dup' and direction ~= 'down' then
direction.snake1 = 'up'
end
if key == 'ddown' and direction ~= 'up' then
direction.snake1 = 'down'
end
end
function love.quit()
end
function createSnake(snake, snakeName)
local newSquare = {x = startx[snakeName], y = starty[snakeName]}
table.insert(snake, newSquare)
_G['oldSquare' .. snakeName] = newSquare
end
function moveSnake(snake, snakeName)
if framecount[snakeName] == 20 and lose[snakeName] == false then
local oldSquareName = _G['oldSquare' .. snakeName]
local newSquare = {x = 0, y = 0}
if direction[snakeName] == 'right' then
newSquare.x = oldSquareName.x + 10
elseif direction[snakeName] == 'left' then
newSquare.x = oldSquareName.x - 10
else
newSquare.x = oldSquareName.x
end
if direction[snakeName] == 'down' then
newSquare.y = oldSquareName.y + 10
elseif direction[snakeName] == 'up' then
newSquare.y = oldSquareName.y - 10
else
newSquare.y = oldSquareName.y
end
if newSquare.x < 0 or newSquare.x >= 320 or newSquare.y < 0 or newSquare.y >= 240 then
lose[snakeName] = true
end
for i, square in ipairs(snake1) do
if square.x == newSquare.x and square.y == newSquare.y then
lose[snakeName] = true
end
end
for i, square in ipairs(snake2) do
if square.x == newSquare.x and square.y == newSquare.y then
lose[snakeName] = true
end
end
framecount[snakeName] = 0
table.insert(snake, newSquare)
_G['oldSquare' .. snakeName] = newSquare
end
framecount[snakeName] = tonumber(framecount[snakeName]) + 1
end