画移动球体

来源:互联网 发布:linux 路由表怎么看 编辑:程序博客网 时间:2024/04/30 03:35

移动球体,包括下落加速度设定和移动设定,有碰撞效果


function love.load()


love.physics.setMeter(64) --the height of a meter our worlds will be 64px
world = love.physics.newWorld(0, 9.81*64, true) --create a world for the bodies to exist in with horizontal gravity of 0 and vertical gravity of 9.81
size = 650
object = {}
--create ground
object.ground = {}
object.ground.body = love.physics.newBody(world,size/2,size - 50/2)
object.ground.sharp = love.physics.newRectangleShape(size,50)
--attach the sharp to the body
object.ground.fixture = love.physics.newFixture(object.ground.body,object.ground.sharp)


--create a ball
object.ball = {}
--place the body in the center of the world and make it dynamic, so it can move around
object.ball.body = love.physics.newBody(world,size/2,size/2,"dynamic")
--create a circle
object.ball.sharp = love.physics.newCircleShape(20)
--attache the shape the body
object.ball.fixture = love.physics.newFixture(object.ball.body,object.ball.sharp,1)
object.ball.fixture:setRestitution(0,9)


--let's create a couple blocks to play around with
object.block1 = {}
object.block1.body = love.physics.newBody(world, 200, 550, "dynamic")
object.block1.shape = love.physics.newRectangleShape(0, 0, 50, 100)
object.block1.fixture = love.physics.newFixture(object.block1.body, object.block1.shape, 5) -- A higher density gives it more mass.


object.block2 = {}
object.block2.body = love.physics.newBody(world, 200, 400, "dynamic")
object.block2.shape = love.physics.newRectangleShape(0, 0, 100, 50)
object.block2.fixture = love.physics.newFixture(object.block2.body, object.block2.shape, 2)


--set the background color to a nice blue
love.graphics.setBackgroundColor(104, 136, 248)
--set the window dimensions to 650 by 650 with no fullscreen, vsync on, and no antialiasing
love.graphics.setMode(650, 650, false, true, 0)


end


function love.update(dt)


--this puts the world into motion
world:update(dt)
--press the right arrow key to push the ball to the right
if love.keyboard.isDown("right") then
object.ball.body:applyForce(400,0)
elseif love.keyboard.isDown("left") then
object.ball.body:applyForce(-400,0)
--press the up arrow key to set the ball in the air
elseif love.keyboard.isDown("up") then
object.ball.body:setPosition(size/2,size/2)
end


end


function love.draw()


-- set the drawing color to green for the ground
love.graphics.setColor(72, 160, 14)
-- draw a "filled in" polygon using the ground's coordinates
--love.graphics.polygon("fill", object.ground.body:getWorldPoints(object.ground.sharp:getPoints()))
--自己测试polygon的点算法,行Ч?
love.graphics.polygon("fill", 0,650,0,600,650,600,650,650)


love.graphics.setColor(193, 47, 14) --set the drawing color to red for the ball
love.graphics.circle("fill", object.ball.body:getX(), object.ball.body:getY(), object.ball.sharp:getRadius())


love.graphics.setColor(50, 50, 50) -- set the drawing color to grey for the blocks
love.graphics.polygon("fill", object.block1.body:getWorldPoints(object.block1.shape:getPoints()))
love.graphics.polygon("fill", object.block2.body:getWorldPoints(object.block2.shape:getPoints()))
end


function love.keypressed(key)


end










原创粉丝点击