Skip to content

Instantly share code, notes, and snippets.

@jhbruhn
Created December 26, 2012 23:38
Show Gist options
  • Save jhbruhn/4384043 to your computer and use it in GitHub Desktop.
Save jhbruhn/4384043 to your computer and use it in GitHub Desktop.
Löve ist ja schon nais
paddleWidth = 10*2
paddleHeight = 200
ballRadius = 10
width = 800
height = 600
function love.load()
love.physics.setMeter(64)
world = love.physics.newWorld(0, 0, true)
objects = {}
objects.wall1 = {}
objects.wall1.body = love.physics.newBody(world, 50/2, 613/2)
objects.wall1.shape = love.physics.newRectangleShape(0, 0, 50, 613)
objects.wall1.fixture = love.physics.newFixture(objects.wall1.body, objects.wall1.shape, 1)
objects.wall1.fixture:setRestitution(1)
objects.wall2 = {}
objects.wall2.body = love.physics.newBody(world, 761/2+50, 50/2)
objects.wall2.shape = love.physics.newRectangleShape(0, 0, 761, 50)
objects.wall2.fixture = love.physics.newFixture(objects.wall2.body, objects.wall2.shape, 1)
objects.wall2.fixture:setRestitution(1)
objects.wall3 = {}
objects.wall3.body = love.physics.newBody(world, 761/2+50, height-50/2)
objects.wall3.shape = love.physics.newRectangleShape(0, 0, 761, 50)
objects.wall3.fixture = love.physics.newFixture(objects.wall3.body, objects.wall3.shape, 1)
objects.wall3.fixture:setRestitution(1)
objects.paddle = {}
objects.paddle.body = love.physics.newBody(world, width-20/2, height/2, "kinematic")
w = paddleWidth
h = paddleHeight
objects.paddle.shape = love.physics.newPolygonShape( 0, -h/2,
-w/2, -h/6,
-w/2, h/6,
0, h/2,
w/2, h/2,
w/2, -h/2)
objects.paddle.fixture = love.physics.newFixture(objects.paddle.body, objects.paddle.shape, 1)
objects.paddle.fixture:setRestitution(1)
objects.ball = {}
objects.ball.body = love.physics.newBody(world, width/2, height/2, "dynamic")
objects.ball.shape = love.physics.newCircleShape(ballRadius)
objects.ball.fixture = love.physics.newFixture(objects.ball.body, objects.ball.shape, 1)
objects.ball.fixture:setRestitution(1)
objects.ball.fixture:setFriction(0)
objects.ball.body:setLinearDamping(0)
objects.ball.body:setMass(0.057)
objects.ball.body:applyForce(500, 500)
love.graphics.setBackgroundColor(0, 0, 0)
love.graphics.setMode(width, height, false, true, 0)
end
function love.update(dt)
world:update(dt)
if love.keyboard.isDown("up") then
newY = objects.paddle.body:getY() - 500 * dt
if newY - paddleHeight / 2 > objects.wall2.body:getY() + 50/2 then
objects.paddle.body:setY(newY)
end
elseif love.keyboard.isDown("down") then
newY = objects.paddle.body:getY() + 500 * dt
if newY + paddleHeight / 2 < objects.wall3.body:getY() - 50/2 then
objects.paddle.body:setY(newY)
end
end
if love.keyboard.isDown(" ") then
objects.ball.body:setPosition(width/2, height/2)
end
end
function love.draw()
love.graphics.setColor(255, 255, 255)
love.graphics.polygon("fill", objects.wall1.body:getWorldPoints(objects.wall1.shape:getPoints()))
love.graphics.polygon("fill", objects.wall2.body:getWorldPoints(objects.wall2.shape:getPoints()))
love.graphics.polygon("fill", objects.wall3.body:getWorldPoints(objects.wall3.shape:getPoints()))
love.graphics.setColor(255, 0, 0)
love.graphics.polygon("fill", objects.paddle.body:getWorldPoints(objects.paddle.shape:getPoints()))
love.graphics.setColor(0, 255, 0)
love.graphics.circle("fill", objects.ball.body:getX(), objects.ball.body:getY(), objects.ball.shape:getRadius())
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment