Code: Select all
Camera = require "camera"
function love.load()
player = {}
player.x= 500
player.y= 120
camera = Camera(player.x, player.y)
end
function love.update(dt)
local dx,dy = player.x - camera.x, player.y - camera.y
camera:move(dx/2, dy/2)
--movement control
if love.keyboard.isDown("a") and player.x > 0 then
player.x = player.x - 5
end
if love.keyboard.isDown("d") and player.x < 10000 then
player.x = player.x + 5
end
if love.keyboard.isDown("w") and player.x > 0 then
player.y = player.y - 5
end
if love.keyboard.isDown("s") and player.x < 10000 then
player.y = player.y + 5
end
end
function love.draw()
camera:attach()
--background
love.graphics.rectangle('fill',0,0,500,300)
love.graphics.setColor(11, 0, 12)
--player
love.graphics.rectangle('fill',player.x,player.y,100,100)
love.graphics.setColor(0, 0, 1)
camera:detach()
end
Code: Select all
local dx,dy = player.x - cam.x, player.y - cam.y
if(dx>50 or dy>50 or dx<-50 or dy<-50) then
cam:move(dx/10, dy/10) end