Skip to content

Instantly share code, notes, and snippets.

@codeliger
Created December 17, 2014 02:35
Show Gist options
  • Save codeliger/f2093a839e444ff6bb87 to your computer and use it in GitHub Desktop.
Save codeliger/f2093a839e444ff6bb87 to your computer and use it in GitHub Desktop.
io.stdout:setvbuf("no")
if arg[#arg] == "-debug" then require("mobdebug").start() end
window = {}
window.w = 500
window.h = 500
love.window.setMode(window.w,window.h)
love.window.setTitle("Circle Boundaries")
circle = {}
circle.radius = 15
circle.x = circle.radius
circle.y = circle.radius
circle.speed = 20
window.x = {}
window.y = {}
window.x.min = circle.radius
window.x.max = window.w - circle.radius
window.y.min = circle.radius
window.y.max = window.h - circle.radius
function diagnostic()
print("--------------------------------")
print(string.format("window.x.min = %d", window.x.min))
print(string.format("circle.x = %d", circle.x))
print(string.format("window.x.max = %d", window.x.max))
print(" --- ")
print(string.format("window.y.min = %d", window.y.min))
print(string.format("circle.y = %d", circle.y))
print(string.format("window.y.max = %d", window.y.max))
end
diagnostic()
circle.move = function(x,y)
print(string.format("circle.move(%d,%d)", x, y))
if x > 0 then
-- Move circle right
if circle.x + circle.speed < window.x.max then
print("--------------------------------")
print(string.format("Adding %d to %d (circle.x)", circle.speed, circle.x))
circle.x = circle.x + circle.speed
diagnostic()
-- Move circle right a remainder
elseif window.x.max - circle.x < circle.speed and window.x.max - circle.x > 0 then
print("--------------------------------")
print(string.format("Adding remainder %d to %d (circle.x)", window.x.max - circle.x, circle.x))
circle.x = circle.x + (window.x.max - circle.x)
diagnostic()
end
elseif x < 0 then
-- Move circle left
if circle.x - circle.speed > window.x.min then
print("--------------------------------")
print(string.format("Subtracting %d from %d (circle.x)", circle.speed, circle.x))
circle.x = circle.x - circle.speed
diagnostic()
-- Move circle left a remainder
elseif circle.x - circle.speed < window.x.min and circle.x - window.x.min < circle.speed and circle.x - window.x.min > 0 then
print("--------------------------------")
print(string.format("Subtracting %d from %d (circle.x)", circle.speed, circle.x))
circle.x = circle.x - (circle.x - window.x.min)
diagnostic()
end
end
end
dtcount = 0
function love.update(dt)
dtcount = dtcount + dt
if dtcount > .5 then
--if( love.keyboard.isDown("w") ) then circle.move(0,-1) end
if( love.keyboard.isDown("a") ) then circle.move(-1,0) end
--if( love.keyboard.isDown("s") ) then circle.move(0,1) end
if( love.keyboard.isDown("d") ) then circle.move(1,0) end
dtcount = dtcount - .5
end
end
function love.draw()
love.graphics.setColor({255,255,255,255})
love.graphics.circle("fill", circle.x, circle.y, circle.radius, 50);
love.graphics.setColor({255,0,0,255})
love.graphics.setPointSize(3)
love.graphics.point(circle.x,circle.y)
love.graphics.point(circle.x-circle.radius,circle.y)
love.graphics.point(circle.x+circle.radius,circle.y)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment