Skip to content

Instantly share code, notes, and snippets.

@radgeRayden
Last active September 10, 2015 01:59
Show Gist options
  • Save radgeRayden/903d093740c3bd578392 to your computer and use it in GitHub Desktop.
Save radgeRayden/903d093740c3bd578392 to your computer and use it in GitHub Desktop.
snake prototype
overlay = {}
local fps = 0
function overlay.draw()
love.graphics.print('FPS: ' .. fps, 25, 25)
end
function overlay.update(dt)
fps = 1 / dt - (1 / dt % 1)
end
return overlay
local vector2 = require "vector2"
local dbgoverlay = require "debugoverlay"
local score = 0
local segmentSize = 32
local speed = 5
local currentDirection = vector2.new(1, 0)
segment = {}
function segment.new(parent, position, direction)
local self = {}
if not parent then
self.position = position
self.direction = direction
else
parent.child = self
self.position = parent.position - parent.direction
self.direction = parent.direction
end
function self:changeDirection(direction)
if self.child then
self.child:changeDirection(self.direction)
end
--avoid turning back on itself
if self.direction * -1 ~= direction then
self.direction = direction
end
--alignment correction (irregular dts might cause the segments to be unaligned)
if self.child then
self.child.position = self.position - self.child.direction
end
end
function self:addSegment()
if self.child then self.child:addSegment(); return end
self.child = segment.new(self)
end
function self:update(dt)
if self.child then
self.child:update(dt)
end
self.position = self.position + self.direction * dt * speed
end
return self
end
head = segment.new(nil, vector2.new(5, 5), vector2.new(1, 0))
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
head:addSegment()
function love.load()
end
function love.draw()
--draw head
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle('fill', head.position.x * segmentSize, head.position.y * segmentSize, segmentSize, segmentSize)
love.graphics.setColor(255, 255, 255)
--draw segments
local currentSegment = head
while currentSegment.child do
love.graphics.rectangle('fill', currentSegment.child.position.x * segmentSize + 0.5, currentSegment.child.position.y * segmentSize + 0.5, segmentSize - 2, segmentSize - 2)
currentSegment = currentSegment.child
end
dbgoverlay.draw()
end
function love.keypressed(key)
if key == 'left' then
currentDirection = vector2.new(-1, 0)
end
if key == 'right' then
currentDirection = vector2.new(1, 0)
end
if key == 'up' then
currentDirection = vector2.new(0, -1)
end
if key == 'down' then
currentDirection = vector2.new(0, 1)
end
end
local timer = 1 / speed
function love.update(dt)
dbgoverlay.update(dt)
timer = timer - dt
if timer <= 0 then
timer = 1 / speed
head:changeDirection(currentDirection)
end
head:update(dt)
end
--[[
Copyright (c) 2015, Westerbly (radgeRayden) Snaydley
This is licensed under the MIT license (http://opensource.org/licenses/MIT).
Time of creation: June 30th, 2015 12:32 GMT
Description:
Helper script for handling of Vector2 type objects.
--]]
local vector2 = {}
function vector2.new(x, y)
local self = {}
setmetatable(self, vector2.meta)
self.x = x
self.y = y
function self:magnitude()
local x = math.abs(self.x)
local y = math.abs(self.y)
return math.sqrt(x ^ 2 + y ^ 2)
end
function self:normalized()
local magnitude = self:magnitude()
return vector2.new(self.x / magnitude, self.y / magnitude)
end
return self
end
function vector2.add(v1, v2)
return vector2.new(v1.x + v2.x, v1.y + v2.y)
end
function vector2.sub(v1, v2)
return vector2.new(v1.x - v2.x, v1.y - v2.y)
end
function vector2.multbynumber(v, number)
if type(number) == "number" then
return vector2.new(v.x * number, v.y * number)
else
error("Tried to multiply a vector but the other operand wasn't a number.")
end
end
function vector2.divbynumber(v, number)
if type(number) == "number" then
return vector2.new(v.x / number, v.y / number)
else
error("Tried to divide a vector but the other operand wasn't a number.")
end
end
function vector2.compare(v1, v2)
return v1.x == v2.x and v1.y == v2.y
end
function vector2.distance(v1, v2)
return (v2 - v1):magnitude()
end
function vector2.tostring(v)
return "vector2(" .. v.x .. ", " .. v.y ..")"
end
function vector2.zero()
return vector2.new(0, 0)
end
function vector2.one()
return vector2.new(1, 1)
end
vector2.meta = {}
vector2.meta.__add = vector2.add
vector2.meta.__sub = vector2.sub
vector2.meta.__mul = vector2.multbynumber
vector2.meta.__div = vector2.divbynumber
vector2.meta.__eq = vector2.compare
vector2.meta.__tostring = vector2.tostring
return vector2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment