Advertisement
joseleeph

Untitled

Dec 30th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. Ball = Class{}
  2.  
  3. function Ball:init(x, y, width, height) -- inheritance allows you to have a baseclass that you can spawn children classes that takes all the attributes of the baseclass and add your own custom behavior
  4. self.x = x
  5. self.y = y
  6. self.width = width
  7. self.height = height
  8.  
  9. self.dx = math.random(2) == 1 and 100 or -100
  10. self.dy = math.random(-50, 50)
  11. end
  12.  
  13. function Ball:update(dt)
  14. self.x = self.x + self.dx * dt
  15. self.y = self.y + self.dy * dt
  16. --self.y = self.x + self.dy * dt
  17. end
  18.  
  19. function Ball:collides(box) -- will tell us if boxes overlap
  20. if self.x is > box.x + box.width or self.x + self.width < box.x then -- everything is relative to it's top left corner, we want to add the width to see of our left side is to the right of the other box
  21. return false
  22. end
  23. if self.y is > box.y + box.height or self.y + self.height < box.y then
  24. return false
  25. end
  26. return true
  27. end
  28.  
  29. function Ball:reset()
  30. self.x = VIRUTAL_WIDTH/2-2
  31. self.y = VIRTUAL_HEIGHT/2-2
  32.  
  33. self.dx = math.random(2) == 1 and -100 or 100
  34.  
  35. self.dy = math.random(-50,50) * 1.5
  36. end
  37.  
  38. function Ball:render()
  39. love.graphics.rectangle('fill', self.x, self.y, 5, 5)
  40. end
  41.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement