Guest User

Untitled

a guest
Apr 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. -- CONFIG --
  2. BALL_MASS = 15
  3. PLAYER_MASS = 25
  4.  
  5. MOVE_FORCE = 40
  6. MAX_VEL = 80
  7. ------------
  8.  
  9. game = {}
  10.  
  11. local function newCircle(world, x, y, radius, mass)
  12. local ret = {}
  13. ret.body = love.physics.newBody(world, x, y, mass)
  14. ret.shape = love.physics.newCircleShape(ret.body, 0, 0, radius)
  15.  
  16. return ret
  17. end
  18.  
  19. function love.load()
  20. game.world = love.physics.newWorld(0, 0, 660, 450)
  21.  
  22. game.ball = newCircle(game.world, 660/2, 450/2, 8, BALL_MASS)
  23. game.player1 = newCircle(game.world, 660/2 - 150, 450/2, 12, PLAYER_MASS)
  24. game.player2 = newCircle(game.world, 660/2 + 150, 450/2, 12, PLAYER_MASS)
  25.  
  26. love.graphics.setBackgroundColor(0, 255, 0)
  27. love.graphics.setLine(2)
  28. end
  29.  
  30. local function check(num, max)
  31. if(math.abs(num) >= max) then
  32. return num < 0 and -max or max
  33. end
  34. return num
  35. end
  36. function love.update(dt)
  37. game.world:update(dt)
  38.  
  39. if love.keyboard.isDown("right") then
  40. game.player1.body:applyForce(MOVE_FORCE, 0)
  41. elseif love.keyboard.isDown("left") then
  42. game.player1.body:applyForce(-MOVE_FORCE, 0)
  43. elseif love.keyboard.isDown("up") then
  44. game.player1.body:applyForce(0, -MOVE_FORCE)
  45. elseif love.keyboard.isDown("down") then
  46. game.player1.body:applyForce(0, MOVE_FORCE)
  47. end
  48.  
  49. local mx, my = game.player1.body:getLinearVelocity()
  50. local mx, my = check(mx, MAX_VEL), check(my, MAX_VEL)
  51. game.player1.body:setLinearVelocity(mx, my)
  52. end
  53.  
  54. local function drawCircle(o, color)
  55. love.graphics.setColor(unpack(color))
  56. love.graphics.circle("fill", o.body:getX() + 70, o.body:getY() + 70, o.shape:getRadius(), 180)
  57.  
  58. love.graphics.setColor(0, 0, 0)
  59. love.graphics.circle("line", o.body:getX() + 70, o.body:getY() + 70, o.shape:getRadius(), 180)
  60. end
  61.  
  62. local function drawMap()
  63. love.graphics.setColor(255, 255, 255)
  64. love.graphics.rectangle("line", 70, 70, 660, 450)
  65.  
  66. love.graphics.line(70 + 660/2, 70, 70 + 660/2, 70 + 450)
  67. love.graphics.circle("line", 70 + 660/2, 70 + 450/2, 80, 180)
  68. end
  69.  
  70. function love.draw()
  71. drawMap()
  72.  
  73. drawCircle(game.ball, {255, 255, 255})
  74.  
  75. drawCircle(game.player1, {0, 0, 255})
  76. drawCircle(game.player2, {255, 0, 0})
  77. end
Add Comment
Please, Sign In to add comment