Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- CONFIG --
- BALL_MASS = 15
- PLAYER_MASS = 25
- MOVE_FORCE = 40
- MAX_VEL = 80
- ------------
- game = {}
- local function newCircle(world, x, y, radius, mass)
- local ret = {}
- ret.body = love.physics.newBody(world, x, y, mass)
- ret.shape = love.physics.newCircleShape(ret.body, 0, 0, radius)
- return ret
- end
- function love.load()
- game.world = love.physics.newWorld(0, 0, 660, 450)
- game.ball = newCircle(game.world, 660/2, 450/2, 8, BALL_MASS)
- game.player1 = newCircle(game.world, 660/2 - 150, 450/2, 12, PLAYER_MASS)
- game.player2 = newCircle(game.world, 660/2 + 150, 450/2, 12, PLAYER_MASS)
- love.graphics.setBackgroundColor(0, 255, 0)
- love.graphics.setLine(2)
- end
- local function check(num, max)
- if(math.abs(num) >= max) then
- return num < 0 and -max or max
- end
- return num
- end
- function love.update(dt)
- game.world:update(dt)
- if love.keyboard.isDown("right") then
- game.player1.body:applyForce(MOVE_FORCE, 0)
- elseif love.keyboard.isDown("left") then
- game.player1.body:applyForce(-MOVE_FORCE, 0)
- elseif love.keyboard.isDown("up") then
- game.player1.body:applyForce(0, -MOVE_FORCE)
- elseif love.keyboard.isDown("down") then
- game.player1.body:applyForce(0, MOVE_FORCE)
- end
- local mx, my = game.player1.body:getLinearVelocity()
- local mx, my = check(mx, MAX_VEL), check(my, MAX_VEL)
- game.player1.body:setLinearVelocity(mx, my)
- end
- local function drawCircle(o, color)
- love.graphics.setColor(unpack(color))
- love.graphics.circle("fill", o.body:getX() + 70, o.body:getY() + 70, o.shape:getRadius(), 180)
- love.graphics.setColor(0, 0, 0)
- love.graphics.circle("line", o.body:getX() + 70, o.body:getY() + 70, o.shape:getRadius(), 180)
- end
- local function drawMap()
- love.graphics.setColor(255, 255, 255)
- love.graphics.rectangle("line", 70, 70, 660, 450)
- love.graphics.line(70 + 660/2, 70, 70 + 660/2, 70 + 450)
- love.graphics.circle("line", 70 + 660/2, 70 + 450/2, 80, 180)
- end
- function love.draw()
- drawMap()
- drawCircle(game.ball, {255, 255, 255})
- drawCircle(game.player1, {0, 0, 255})
- drawCircle(game.player2, {255, 0, 0})
- end
Add Comment
Please, Sign In to add comment