love2d连接线段对象来制作绘制线条的铅笔工具

时间:2018-05-14 22:18:55

标签: animation graphics lua love2d game-development

我正在尝试创建一个类似于线路骑手的游戏。我现在有点编写一支铅笔,除了线条是分散的物体。

enter image description here

我希望能够连接这些红点以形成一条平滑的线条,以便球可以滚过它。

它应该更像这样

enter image description here

这是我的代码

function love.load()
  printx = 0    --position to be drawn on when the mouse is pressed
  printy = 0    --position to be drawn on when the mouse is pressed

  love.physics.setMeter(64) --the height of a meter our worlds will be 64px
  world = love.physics.newWorld(0, 9.81*64, true) --The world the everything exists in. Horizontal gravity=0. Bertical gravity=9.81. True says that the world is allowed to sleep.

  objects = {} -- table to hold all our physical objects

  --let's create the ground
  objects.lines = {}
  objects.ground = {}
  objects.ground.body = love.physics.newBody(world, 650/2, 650-50/2) 
  objects.ground.shape = love.physics.newRectangleShape(650, 50) --make a rectangle with a width of 650 and a height of 50
  objects.ground.fixture = love.physics.newFixture(objects.ground.body, objects.ground.shape); --attach shape to body

  --let's create a ball
  objects.ball = {}
  objects.ball.body = love.physics.newBody(world, 650/2, 600, "dynamic") --Determines where the object will start. In the center of the world and dynamic(moves around)
  objects.ball.shape = love.physics.newCircleShape(20) --the ball's shape has a radius of 20
  objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1) -- Attach fixture to body and give it a density of 1.


  --initial graphics setup
  love.graphics.setBackgroundColor(0.41, 0.53, 0.97) --set the background color to a nice blue
  love.window.setMode(650, 650) --set the window dimensions to 650 by 650
end


function love.update(dt)
  world:update(dt) --this puts the world into motion 

  if love.mouse.isDown(1) then
    local printx = love.mouse.getX()
    local printy = love.mouse.getY()
    line = {}
    line.body = love.physics.newBody(world, printx, printy, "static")
    line.shape = love.physics.newRectangleShape(0, 0, 5, 5)
    line.fixture = love.physics.newFixture(line.body, line.shape, 5) -- A higher density gives it more mass.
    table.insert(objects.lines, line)
    love.graphics.setColor(50,50,50)
    love.graphics.polygon("fill", line.body:getWorldPoints(line.shape:getPoints()))
  end

end 

function love.draw()
  love.graphics.setColor(0.28, 0.63, 0.05) -- set the drawing color to green for the ground
  love.graphics.polygon("fill", objects.ground.body:getWorldPoints(objects.ground.shape:getPoints())) -- draw a "filled in" polygon using the ground's coordinates

  love.graphics.setColor(0.76, 0.18, 0.05) --set the drawing color to red for the ball
  love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())

  for _, block in pairs(objects.lines) do
    love.graphics.polygon("fill", block.body:getWorldPoints(block.shape:getPoints()))
  end

end

1 个答案:

答案 0 :(得分:1)

在love.update中

foreach(GameObject item in cps){
    ClicksPerSecondManager cpsm = item.getComponent<ClicksPerSecondManager>(); 
    //now you have access to its public properties or methods
    Debug.log(cpsm.count);
    //etc
}

在love.draw()

  if not love.mouse.isDown(1) then
    oldx = nil
    oldy = nil
  end


  --draw lines when the mouse is down
  if love.mouse.isDown(1) then
    local printx = love.mouse.getX() + Camera.x --x coordinate of the mouse
    local printy = love.mouse.getY() + Camera.y --y coordinate of the mouse

    if oldx ~= nil then
        line = {}
        line.x1 = oldx
        line.x2 = printx
        line.y1 = oldy
        line.y2 = printy
        line.body = love.physics.newBody(world, 0, 0, "static")
        line.shape = love.physics.newEdgeShape(printx, printy, oldx, oldy)
        line.fixture = love.physics.newFixture(line.body, line.shape, 5)
        table.insert(objects.lines, line) 
    end

    oldx = printx
    oldy = printy

  end