Code: Select all
local p1 = {}
local p2 = {}
local p1.touch = {}
local p2.touch = {}
function love.load()
p1.img = love.graphics.newImage('player.png')
p1.x = p1.img:getWidth() / 2
p1.y = love.graphics.getHeight() / 2
p1.touch.x = p1.x
p1.touch.y = p1.y
p1.speed = 500
p2.img = p1.img
p2.x = love.graphics.getWidth() - p2.img:getWidth() / 2
p2.y = love.graphics.getHeight() / 2
p2.touch.x = p2.x
p2.touch.y = p2.y
p2.speed = 500
end
function love.update(dt)
local touches = love.touch.getTouches()
for i, id in ipairs(touches) do
local x, y = love.touch.getPosition(id)
if x < love.graphics.getWidth() / 2 then
p1.touch.x = x
p1.touch.y = y
else
p2.touch.x = x
p2.touch.y = y
end
end
local distance1 = math.dist(p1.x, p1.y, p1.touch.x, p1.touch.y)
local stepSize1 = p1.speed * 2.5 * dt
if distance1 > stepSize1 then
p1.x = p1.x + (p1.touch.x - p1.x) / distance1 * stepSize1
p1.y = p1.y + (p1.touch.y - p1.y) / distance1 * stepSize1
end
local distance2 = math.dist(p2.x, p2.y, p2.touch.x, p2.touch.y)
local stepSize2 = p2.speed * 2.5 * dt
if distance2 > stepSize2 then
p2.x = p2.x + (p2.touch.x - p2.x) / distance2 * stepSize2
p2.y = p2.y + (p2.touch.y - p2.y) / distance2 * stepSize2
end
end
function love.draw()
love.graphics.draw(p1.img, p1.x - p1.img:getWidth() / 2, p1.y - p1.img:getHeight() / 2)
love.graphics.draw(p2.img, p2.x - p2.img:getWidth() / 2, p2.y - p2.img:getHeight() / 2)
end
function math.dist(x1, y1, x2, y2) return ((x2 - x1) ^ 2 + (y2 - y1) ^ 2) ^ 0.5 end