Code: Select all
local w, h
local p = {}
for i = 1, 2 do
p[i] = {}
end
function love.load()
w, h = love.graphics.getDimensions()
for i = 1, 2 do
p[i].element = i
p[i].x, p[i].y = (2 * i - 1) * w / 4, h / 2
p[i].w, p[i].h = 100, 100
p[i].xx, p[i].yy = p[i].x, p[i].y
p[i].v = 750
end
end
function love.update(dt)
local touches = love.touch.getTouches()
for i, id in ipairs(touches) do
local x, y = love.touch.getPosition(id)
for i = 1, 2 do
if (3 - 2 * i) * x < (3 - 2 * i) * w / 2 and (3 - 2 * i) * x > 200 - (i - 1) * w then
p[i].xx = x
p[i].yy = y
end
end
end
for i = 1, 2 do
local d = ((p[i].xx - p[i].x) ^ 2 + (p[i].yy - p[i].y) ^ 2) ^ 0.5
if d > p[i].v * dt then
p[i].x = p[i].x + (p[i].xx - p[i].x) / d * p[i].v * dt
p[i].y = p[i].y + (p[i].yy - p[i].y) / d * p[i].v * dt
end
end
end
function love.draw()
love.graphics.setColor(1, 1, 1)
love.graphics.line(w / 2, 0, w / 2, h)
for i = 1, 2 do
if p[i].element == 1 then
love.graphics.setColor(0.25, 0.28, 0.8)
elseif p[i].element == 2 then
love.graphics.setColor(0.5, 0.5, 0.5)
elseif p[i].element == 3 then
love.graphics.setColor(0.93, 0.11, 0.14)
else
love.graphics.setColor(0.76, 0.76, 0.76)
end
love.graphics.rectangle('fill', p[i].x - p[i].w / 2, p[i].y - p[i].h / 2, p[i].w, p[i].h)
end
end
function love.keypressed(k)
if k == 'escape' then
love.event.quit()
end
end