Created
June 28, 2012 01:10
-
-
Save slembcke/3008039 to your computer and use it in GitHub Desktop.
Snake
This file contains 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
TILE_SIZE = 10 | |
TILES_WIDE = love.graphics.getWidth()/TILE_SIZE | |
TILES_HIGH = love.graphics.getHeight()/TILE_SIZE | |
LENGTH_INCREMENT = 5 | |
TRUD_INCREMENT = 5 | |
ACCUMULATOR = 0 | |
snake = { | |
speed = 20, | |
position = {x = love.graphics.getWidth()/2, y = love.graphics.getHeight()/2}, | |
velocity = {x = 1, y = 0}, | |
length = 5, | |
trudCount = 0, | |
body = {}, | |
} | |
truds = {{x = 5, y = 5}} | |
function coord_eql(a, b) | |
return (a.x == b.x and a.y == b.y) | |
end | |
function love.load() | |
end | |
function love.keypressed(key) | |
if key == "up" then | |
snake.velocity = {x = 0, y = -1} | |
elseif key == "down" then | |
snake.velocity = {x = 0, y = 1} | |
elseif key == "left" then | |
snake.velocity = {x = -1, y = 0} | |
elseif key == "right" then | |
snake.velocity = {x = 1, y = 0} | |
end | |
end | |
function step(fixed_dt) | |
snake.position.x = snake.position.x + snake.velocity.x*TILE_SIZE | |
snake.position.y = snake.position.y + snake.velocity.y*TILE_SIZE | |
local pos = { | |
x = math.floor(snake.position.x/TILE_SIZE), | |
y = math.floor(snake.position.y/TILE_SIZE) | |
} | |
local len = #snake.body | |
if len == 0 or not coord_eql(pos, snake.body[len]) then | |
table.insert(snake.body, pos) | |
end | |
for i, body in pairs(snake.body) do | |
if (not i == len) and coord_eql(pos, body) then | |
print("die") | |
end | |
end | |
if #snake.body > snake.length then | |
if snake.trudCount > 0 then | |
table.insert(truds, snake.body[1]) | |
snake.trudCount = snake.trudCount - 1 | |
end | |
table.remove(snake.body, 1) | |
end | |
local trudsCopy = {} | |
for i, trud in pairs(truds) do | |
if coord_eql(pos, trud) then | |
snake.length = snake.length + LENGTH_INCREMENT | |
snake.trudCount = snake.trudCount + TRUD_INCREMENT | |
else | |
table.insert(trudsCopy, trud) | |
end | |
end | |
truds = trudsCopy | |
end | |
function love.update(dt) | |
ACCUMULATOR = ACCUMULATOR + dt | |
local fixed_dt = 1.0/snake.speed | |
while ACCUMULATOR > fixed_dt do | |
ACCUMULATOR = ACCUMULATOR - fixed_dt | |
step(fixed_dt) | |
end | |
end | |
function love.draw() | |
local len = #snake.body | |
for i, p in pairs(snake.body) do | |
if i == len then | |
love.graphics.setColor(255, 0, 0) | |
else | |
love.graphics.setColor(0, 255, 0) | |
end | |
love.graphics.rectangle("fill", p.x*TILE_SIZE, p.y*TILE_SIZE, TILE_SIZE, TILE_SIZE) | |
end | |
for i, p in pairs(truds) do | |
love.graphics.setColor(143, 80, 18) | |
love.graphics.rectangle("fill", p.x*TILE_SIZE, p.y*TILE_SIZE, TILE_SIZE, TILE_SIZE) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment