Advertisement
joseleeph

Untitled

Dec 27th, 2020
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.05 KB | None | 0 0
  1. function love.update(dt)
  2.  
  3. if ball:collides(player1) then -- collides() is stored in Ball.lua
  4. -- deflect the ball to the right
  5. -- change velocity from -100, to 100
  6. ball.dx = -ball.dx
  7. end
  8.  
  9. if ball:collides(player2) then
  10. -- deflect ball to the left
  11. ball.dx = -ball.dx
  12. end
  13.  
  14. if ball.y <= 0 then -- the top of the screen
  15. --deflect the ball down
  16. ball.dy = -ball.dy
  17. -- reset the ball in the case of it hitting the top of the screen
  18. ball.y = 0
  19. end
  20.  
  21. if ball.y >= VIRTUAL_HEIGHT - 4 then -- the ball is 4 pixels tall so offset collision by 4 pixels
  22. ball.dy = -ball.dy
  23. ball.y = VIRTUAL_HEIGHT - 4
  24. end
  25.  
  26.  
  27.  
  28. --[[
  29. if we reach the left or right edge of the screen
  30. go back to start and update the score
  31. ]]
  32. if ball.x < 0 then
  33. player2Score = player2Score + 1
  34. serveringPlayer = 1
  35. ball:reset() -- randomizes the balls dx. we want to set that based on who scored
  36. if player2Score >= 3 then
  37. gameState = 'victory'
  38. winningPlayer = 2
  39. else
  40. gameState = 'serve'
  41. end
  42.  
  43. end
  44.  
  45. if ball.x > VIRTUAL_WIDTH then
  46. player1Score = player1Score + 1 -- serving player not working... always says serving player 2
  47. servingPlayer = 2
  48. ball:reset()
  49. --ball.dx = -100
  50. --gameState = 'serve'
  51. if player1Score >= 3 then
  52. gameState = 'victory'
  53. winningPlayer = 1
  54. else
  55. gameState = 'serve'
  56. end
  57. end
  58.  
  59. -- player 1 movement
  60. if love.keyboard.isDown('w') then
  61. player1.dy = -PADDLE_SPEED -- negative is up
  62. elseif love.keyboard.isDown('s') then
  63. player1.dy = PADDLE_SPEED -- replaces math.min(VIRTUAL_HEIGHT - 2-,PLAYER1Y + PADDLE_SPEED * dt)
  64. else
  65. player1.dy = 0 -- if neither is true, the paddle speed is 0
  66. end
  67. -- player 2 movement
  68. if love.keyboard.isDown('up') then
  69. player2.dy = -PADDLE_SPEED
  70. elseif love.keyboard.isDown('down') then
  71. player2.dy = PADDLE_SPEED
  72. end
  73. else
  74. player2.dy = 0
  75. end
  76. if gameState == 'play' then
  77. ball:update(dt)
  78. --ballX = ballX + ballDX * dt these will instead be done with update
  79. --ballY = ballY + ballDY * dt
  80. end
  81.  
  82. player1:update(dt) -- replaces player1Y = math.min(VIRTUAL_HEIGHT - 20, player1Y + PADDLE_SPEED * dt)... now stored in the paddle class and replace player1Y with self.y
  83. player2:update(dt)
  84. ball:render()
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement