Created
November 7, 2016 09:36
-
-
Save williamhogman/325eb8a6ee53334da9a0899d83f38668 to your computer and use it in GitHub Desktop.
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
| SIZE = 500 | |
| function love.load() | |
| reset() | |
| end | |
| function reset() | |
| previousPositions = {} | |
| lastMove = love.timer.getTime() | |
| loc = {0, 0} | |
| v = {0, 0} | |
| candies = {} | |
| score = 1 | |
| ensureCandies() | |
| end | |
| function ensureCandies() | |
| while table.getn(candies) < 3 do | |
| local x = math.random(1, 49) | |
| local y = math.random(1, 49) | |
| table.insert(candies, {x * 10, y * 10}) | |
| end | |
| end | |
| function pruneTableToN(a, n) | |
| while table.getn(a) > n do | |
| table.remove(a, 1) | |
| end | |
| end | |
| function movedasnakey() | |
| table.insert(previousPositions, loc) | |
| pruneTableToN(previousPositions, score + 10) | |
| local x = (loc[1] + v[1]) % SIZE | |
| local y = (loc[2] + v[2]) % SIZE | |
| loc = {x, y} | |
| end | |
| function collidesWithSnake(l) | |
| for _, v in ipairs(previousPositions) do | |
| if l[1] == v[1] and l[2] == v[2] then | |
| return true | |
| end | |
| end | |
| return false | |
| end | |
| function collidesWithCandy(l) | |
| for i, v in ipairs(candies) do | |
| if v[1] == l[1] and v[2] == l[2] then | |
| table.remove(candies, i) | |
| return true | |
| end | |
| end | |
| return false | |
| end | |
| function collisionDetect() | |
| if collidesWithSnake(loc) then | |
| reset() | |
| elseif collidesWithCandy(loc) then | |
| score = score + 1 | |
| ensureCandies() | |
| end | |
| end | |
| function love.update() | |
| local t = love.timer.getTime() | |
| if t - lastMove > 0.26 - (0.01 * score) then | |
| movedasnakey() | |
| collisionDetect() | |
| lastMove = t | |
| end | |
| end | |
| KEYS = { | |
| w = {0, -10}, | |
| s = {0, 10}, | |
| a = {-10, 0}, | |
| d = {10, 0} | |
| } | |
| function love.keyreleased(key) | |
| local newV = KEYS[key] | |
| if newV then | |
| v = newV | |
| end | |
| end | |
| function drawCandies() | |
| love.graphics.setColor(0, 255, 0, 255) | |
| for i, v in ipairs(candies) do | |
| love.graphics.rectangle("fill", v[1] + 10, v[2] + 10, 10, 10) | |
| end | |
| end | |
| function drawSnakey() | |
| love.graphics.setColor(0, 255, 0, 255) | |
| for i, v in ipairs(previousPositions) do | |
| love.graphics.rectangle("fill", v[1] + 10, v[2] + 10, 10, 10) | |
| end | |
| love.graphics.setColor(255, 0, 0, 255) | |
| love.graphics.rectangle("fill", loc[1] + 10, loc[2] + 10, 10, 10) | |
| end | |
| function drawArena() | |
| love.graphics.setColor(128, 128, 128, 255) | |
| love.graphics.rectangle("line", 10, 10, SIZE, SIZE) | |
| end | |
| function love.draw() | |
| drawSnakey() | |
| drawCandies() | |
| drawArena() | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment