Last active
September 29, 2021 06:31
-
-
Save dauuricus/ae03c05a8beac369448392971c8ce418 to your computer and use it in GitHub Desktop.
love2d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function love.conf(t) | |
t.window.width = 23 * 56 | |
t.window.height = 23 * 31 | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function love.load() | |
require("paddy") | |
gridXCount = 56 | |
gridYCount = 31 | |
offsetX = 0 | |
offsetY = 15 | |
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 == foodPosition1.x and foodY == foodPosition1.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 == foodPosition1.x and foodY == foodPosition1.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 | |
if foodPosition5 then | |
if foodX == foodPosition5.x and foodY == foodPosition5.y then | |
possible = false | |
end | |
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 == foodPosition1.x and foodY == foodPosition1.y then | |
possible = false | |
elseif foodX == foodPosition2.x and foodY == foodPosition2.y then | |
possible = false | |
end | |
if foodPosition4 then | |
if foodX == foodPosition4.x and foodY == foodPosition4.y then | |
possible = false | |
end | |
end | |
if foodPosition5 then | |
if foodX == foodPosition5.x and foodY == foodPosition5.y then | |
possible = false | |
end | |
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 == foodPosition1.x and foodY == foodPosition1.y then | |
possible = false | |
end | |
if foodPosition3 then | |
if foodX == foodPosition3.x and foodY == foodPosition3.y then | |
possible = false | |
end | |
end | |
if foodPosition4 then | |
if foodX == foodPosition4.x and foodY == foodPosition4.y then | |
possible = false | |
end | |
end | |
if foodPosition5 then | |
if foodX == foodPosition5.x and foodY == foodPosition5.y then | |
possible = false | |
end | |
end | |
end | |
if possible then | |
table.insert(possibleFoodPositions2, {x = foodX, y = foodY}) | |
end | |
end | |
end | |
foodPosition2 = possibleFoodPositions2[ | |
love.math.random(#possibleFoodPositions2) | |
] | |
end | |
function moveFood1() | |
local possibleFoodPositions1 = {} | |
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 foodPosition2 then | |
if foodX == foodPosition2.x and foodY == foodPosition2.y then | |
possible = false | |
end | |
end | |
if foodPosition3 then | |
if foodX == foodPosition3.x and foodY == foodPosition3.y then | |
possible = false | |
end | |
end | |
if foodPosition4 then | |
if foodX == foodPosition4.x and foodY == foodPosition4.y then | |
possible = false | |
end | |
end | |
if foodPosition5 then | |
if foodX == foodPosition5.x and foodY == foodPosition5.y then | |
possible = false | |
end | |
end | |
if possible then | |
table.insert(possibleFoodPositions1, {x = foodX, y = foodY}) | |
end | |
end | |
end | |
foodPosition1 = possibleFoodPositions1[ | |
love.math.random(#possibleFoodPositions1) | |
] | |
end | |
function reset() | |
snakeSegments = { | |
{x = 10, y = 10}, | |
{x = 9, y = 10}, | |
{x = 8, y = 10}, | |
{x = 7, y = 10}, | |
{x = 6, y = 10}, | |
{x = 5, y = 10}, | |
{x = 4, y = 10}, | |
{x = 3, y = 10}, | |
{x = 2, y = 10}, | |
{x = 1, y = 10}, | |
} | |
directionQueue = {'right'} | |
snakeAlive = true | |
timer = 0 | |
moveFood1() | |
moveFood2() | |
moveFood3() | |
moveFood4() | |
moveFood5() | |
end | |
reset() | |
end | |
function love.update(dt) | |
timer = timer + dt | |
thin = dt*600 | |
paddy.update(dt) | |
if snakeAlive then | |
if timer >= 0.06 then | |
timer = 0 | |
if paddy.isDown("right") or paddy.isDown("b") | |
and directionQueue[#directionQueue] ~= 'right' | |
and directionQueue[#directionQueue] ~= 'left' then | |
table.insert(directionQueue, 'right') | |
elseif paddy.isDown("left") or paddy.isDown("x") | |
and directionQueue[#directionQueue] ~= 'left' | |
and directionQueue[#directionQueue] ~= 'right' then | |
table.insert(directionQueue, 'left') | |
elseif paddy.isDown("up") or paddy.isDown("y") | |
and directionQueue[#directionQueue] ~= 'up' | |
and directionQueue[#directionQueue] ~= 'down' then | |
table.insert(directionQueue, 'up') | |
elseif paddy.isDown("down") or paddy.isDown("a") | |
and directionQueue[#directionQueue] ~= 'down' | |
and directionQueue[#directionQueue] ~= 'up' then | |
table.insert(directionQueue, 'down') | |
end | |
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 == foodPosition1.x | |
and snakeSegments[1].y == foodPosition1.y then | |
moveFood1() | |
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 = 23 | |
love.graphics.setColor(.1, .3, .3) | |
love.graphics.rectangle( | |
'fill', | |
0 + offsetX , | |
0 + offsetY , | |
gridXCount * cellSize, | |
(gridYCount) * cellSize | |
) | |
local function drawCell2(x, y) | |
love.graphics.rectangle( | |
'fill', | |
(x - 1) * cellSize + offsetX,--+ thin, | |
--(y - 1) * cellSize, | |
(y - 1) * cellSize + offsetY,-- + thin, | |
cellSize - 1, | |
cellSize - 1 | |
) | |
end | |
local function drawCell(x, y) | |
love.graphics.rectangle( | |
'line', | |
(x - 1) * cellSize + offsetX,-- + thin, | |
--(y - 1) * cellSize, | |
(y - 1) * cellSize + offsetY,-- + thin, | |
cellSize - 1, | |
cellSize - 1 | |
) | |
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(foodPosition1.x, foodPosition1.y) | |
drawCell2(foodPosition2.x, foodPosition2.y) | |
drawCell2(foodPosition3.x, foodPosition3.y) | |
drawCell2(foodPosition4.x, foodPosition4.y) | |
drawCell2(foodPosition5.x, foodPosition5.y) | |
love.graphics.setColor(.1, .3, .5) | |
-- local touches = love.touch.getTouches() | |
-- | |
-- for i, id in ipairs(touches) do | |
-- local x, y = love.touch.getPosition(id) | |
-- love.graphics.circle("line", x, y, 40) | |
-- love.graphics.setColor(.5, .3, .1) | |
-- love.graphics.print(i, x, y) | |
-- end | |
--control pad -- paddy.lua | |
paddy.draw() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment