Skip to content

Instantly share code, notes, and snippets.

@dauuricus
Last active September 28, 2021 03:21
Show Gist options
  • Save dauuricus/a4a2e1d08d0a5cfa5554992f9f3b5f4e to your computer and use it in GitHub Desktop.
Save dauuricus/a4a2e1d08d0a5cfa5554992f9f3b5f4e to your computer and use it in GitHub Desktop.
LÖVE 2D snake game for 10inch amazon fire tablet.
function love.conf(t)
t.window.width = 12 * 105
t.window.height = 12 * 57
end
function love.load()
gridXCount = 106
gridYCount = 57
offsetY = 10
function moveFood5()
local possibleFoodPositions5 = {}
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
elseif foodX == foodPosition.x and foodY == foodPosition.y then
possible = false
elseif foodX == foodPosition2.x and foodY == foodPosition2.y then
possible = false
elseif foodX == foodPosition3.x and foodY == foodPosition3.y then
possible = false
elseif foodX == foodPosition4.x and foodY == foodPosition4.y then
possible = false
end
end
if possible then
table.insert(possibleFoodPositions5, {x = foodX, y = foodY})
end
end
end
foodPosition5 = possibleFoodPositions5[
love.math.random(#possibleFoodPositions5)
]
end
function moveFood4()
local possibleFoodPositions4 = {}
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
elseif foodX == foodPosition.x and foodY == foodPosition.y then
possible = false
elseif foodX == foodPosition2.x and foodY == foodPosition2.y then
possible = false
elseif foodX == foodPosition3.x and foodY == foodPosition3.y then
possible = false
end
end
if possible then
table.insert(possibleFoodPositions4, {x = foodX, y = foodY})
end
end
end
foodPosition4 = possibleFoodPositions4[
love.math.random(#possibleFoodPositions4)
]
end
function moveFood3()
local possibleFoodPositions3 = {}
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
elseif foodX == foodPosition.x and foodY == foodPosition.y then
possible = false
elseif foodX == foodPosition2.x and foodY == foodPosition2.y then
possible = false
end
end
if possible then
table.insert(possibleFoodPositions3, {x = foodX, y = foodY})
end
end
end
foodPosition3 = possibleFoodPositions3[
love.math.random(#possibleFoodPositions3)
]
end
function moveFood2()
local possibleFoodPositions2 = {}
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
elseif foodX == foodPosition.x and foodY == foodPosition.y then
possible = false
end
end
if possible then
table.insert(possibleFoodPositions2, {x = foodX, y = foodY})
end
end
end
foodPosition2 = possibleFoodPositions2[
love.math.random(#possibleFoodPositions2)
]
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
end
if possible then
table.insert(possibleFoodPositions, {x = foodX, y = foodY})
end
end
end
foodPosition = possibleFoodPositions[
love.math.random(#possibleFoodPositions)
]
end
function reset()
snakeSegments = {
{x = 17, y = 30},
{x = 16, y = 30},
{x = 15, y = 30},
{x = 14, y = 30},
{x = 13, y = 30},
{x = 12, y = 30},
{x = 11, y = 30},
{x = 10, y = 30},
{x = 9, y = 30},
{x = 8, y = 30},
{x = 7, y = 30},
{x = 6, y = 30},
{x = 5, y = 30},
{x = 4, y = 30},
{x = 3, y = 30},
{x = 2, y = 30},
{x = 1, y = 30},
}
directionQueue = {'right'}
snakeAlive = true
timer = 0
moveFood()
moveFood2()
moveFood3()
moveFood4()
moveFood5()
end
reset()
end
function love.update(dt)
timer = timer + dt
thin = 1 + dt*1000
if snakeAlive then
if timer >= 0.06 then
timer = 0
if #directionQueue > 1 then
table.remove(directionQueue, 1)
end
local nextXPosition = snakeSegments[1].x
local nextYPosition = snakeSegments[1].y
if directionQueue[1] == 'right' then
nextXPosition = nextXPosition + 1
if nextXPosition > gridXCount then
nextXPosition = 1
end
elseif directionQueue[1] == 'left' then
nextXPosition = nextXPosition - 1
if nextXPosition < 1 then
nextXPosition = gridXCount
end
elseif directionQueue[1] == 'down' then
nextYPosition = nextYPosition + 1
if nextYPosition > gridYCount then
nextYPosition = 1
end
elseif directionQueue[1] == 'up' then
nextYPosition = nextYPosition - 1
if nextYPosition < 1 then
nextYPosition = gridYCount
end
end
local canMove = true
for segmentIndex, segment in ipairs(snakeSegments) do
if segmentIndex ~= #snakeSegments
and nextXPosition == segment.x
and nextYPosition == segment.y then
canMove = false
end
end
if canMove then
table.insert(snakeSegments, 1, {
x = nextXPosition, y = nextYPosition
})
if snakeSegments[1].x == foodPosition.x
and snakeSegments[1].y == foodPosition.y then
moveFood()
elseif snakeSegments[1].x == foodPosition2.x
and snakeSegments[1].y == foodPosition2.y then
moveFood2()
elseif snakeSegments[1].x == foodPosition3.x
and snakeSegments[1].y == foodPosition3.y then
moveFood3()
elseif snakeSegments[1].x == foodPosition4.x
and snakeSegments[1].y == foodPosition4.y then
moveFood4()
elseif snakeSegments[1].x == foodPosition5.x
and snakeSegments[1].y == foodPosition5.y then
moveFood5()
else
table.remove(snakeSegments)
end
else
snakeAlive = false
end
end
elseif timer >= 2 then
reset()
end
end
function love.keypressed(key)
if key == 'right'
and directionQueue[#directionQueue] ~= 'right'
and directionQueue[#directionQueue] ~= 'left' then
table.insert(directionQueue, 'right')
elseif key == 'left'
and directionQueue[#directionQueue] ~= 'left'
and directionQueue[#directionQueue] ~= 'right' then
table.insert(directionQueue, 'left')
elseif key == 'up'
and directionQueue[#directionQueue] ~= 'up'
and directionQueue[#directionQueue] ~= 'down' then
table.insert(directionQueue, 'up')
elseif key == 'down'
and directionQueue[#directionQueue] ~= 'down'
and directionQueue[#directionQueue] ~= 'up' then
table.insert(directionQueue, 'down')
end
end
function love.draw()
local cellSize = 12
love.graphics.setColor(1, .3, .3)
love.graphics.rectangle(
'line',
0 + thin,
0 + thin + offsetY,
gridXCount * cellSize - thin,
gridYCount * cellSize - thin + offsetY
)
local function drawCell2(x, y)
love.graphics.rectangle(
'fill',
(x - 1) * cellSize + thin,
--(y - 1) * cellSize,
(y - 1) * cellSize + offsetY + thin,
cellSize - thin,
cellSize - thin
)
end
local function drawCell(x, y)
love.graphics.rectangle(
'fill',
(x - 1) * cellSize + thin,
--(y - 1) * cellSize,
(y - 1) * cellSize + offsetY + thin,
cellSize - thin,
cellSize - thin
)
end
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)
drawCell2(foodPosition.x, foodPosition.y)
drawCell2(foodPosition2.x, foodPosition2.y)
drawCell2(foodPosition3.x, foodPosition3.y)
drawCell2(foodPosition4.x, foodPosition4.y)
drawCell2(foodPosition5.x, foodPosition5.y)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment