love2d绘制一个跟随鼠标移动的圆
2021-03-14 本文已影响0人
我和我的火柴
love2d
今天学习了love2d游戏开发引擎制作一个可以跟随鼠标移动的圆,并且通过勾股定理计算出圆到鼠标的distance。
there is code
function love.load()
circle = {};
circle.x = 200;
circle.y = 300;
circle.radius = 25;
circle.speed = 200;
end
function getDistance(x1,y1,x2,y2)
local horizantal_distance = x1 - x2;
local vertical_distance = y1 - y2;
local a = horizantal_distance ^ 2;
local b = vertical_distance ^ 2;
local c = a + b;
local distance = math.sqrt(c);
return distance;
end
function love.update(dt)
mouse_x,mouse_y = love.mouse.getPosition();
angle = math.atan2(mouse_y-circle.y,mouse_x-circle.x);
sin = math.sin(angle);
cos = math.cos(angle);
local distance = getDistance(mouse_x,mouse_y,circle.x,circle.y);
if distance<300 then
circle.x = circle.x + circle.speed * cos * dt;
circle.y = circle.y + circle.speed * sin * dt;
end
end
function love.draw()
love.graphics.circle("line",circle.x,circle.y,circle.radius)
love.graphics.print("The angle: "..angle,20,10);
love.graphics.line(circle.x,circle.y,mouse_x,mouse_y);
love.graphics.line(circle.x,circle.y,mouse_x,circle.y)
love.graphics.line(mouse_x,mouse_y,mouse_x,circle.y)
local distance = getDistance(mouse_x,mouse_y,circle.x,circle.y);
love.graphics.circle("line",circle.x,circle.y,distance);
end