Advertisement
joseleeph

Untitled

Dec 26th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. -- it is very important to capitalize the file. it's the norm and expected that you capitalize classes
  6.  
  7. Paddle = Class{}
  8. -- takes the class library and generates a new paddle object that will have methods and fields that can be described after the declaration
  9. -- in order to get it to work, you need to import the library in main.lua with class = require 'class'
  10.  
  11. function Paddle:init(x, y, width, height)
  12. --[[
  13. the colon is important and allows objects to call methods.
  14. : means if an object exists it will use a function that works specifically on that object
  15. if you use . you don't need to instatiate an object
  16. all the love methods are .methods. you don't need to create a love object to use love.load, it's just a function that exists somewhere
  17. in a module called love
  18. if we are dealing with objects and we want every object to call init on itself and use its own data you need the colon :
  19. ]]
  20. self.x = x -- initializing fields to specify in main.lua where you create a new paddle you give it those fields, and they are stored here instead of in main.lua
  21. self.y = y -- self keyword means that this particular object that we are instatiating inside main.lua will refer to itself. self.something means it belongs to that object. we can create multiple paddles as opposed to a global variable
  22. self.width = width
  23. self.height = height
  24.  
  25. self.dy = 0 -- normally there is no velocity unless we press a key. dy will keep track of the y value with regards to dy
  26. end
  27.  
  28. --[[
  29. any class defined needs a function that creates the object that does the work of assigning the right fields
  30. and getting it readey to use
  31. ]]
  32.  
  33. function Paddle:update(dt) -- we have been doing this in main.lua. we will instead do it in the paddle class.
  34. -- we need access to however much time has passed since the last frame we can pass that from main.lua to the paddle class with dt and making sure in main.lua we make sure to feed dt into paddle update
  35. if self.dy < 0 then -- if it's less than 0 it's going towards the top of the screen...
  36. self.y = math.max(0, self.y + self.dy * dt) -- we want to take the functionality from main.lua and use it in the paddle class
  37. elseif self.dy > 0 then
  38. self.y = math.min(VIRTUAL_HEIGHT - 20, self.y + self.dy * dt)
  39. -- same for other case. we nolonger have access to player1y we now only have access to self.
  40. -- check this very carefully, you were probably making a mistake here that you asked on reddit
  41. -- the video says
  42. --[[
  43. if self.dy < 0
  44. self.y.math.max(0,self.y + -self.dy * dt) -- it was a mistake on their part we should always be adding self.dy instead
  45. elseif self.dy > 0 then
  46. self.y
  47.  
  48. the paddles now work as before but are now stored in a class instead of a variable stored in main.lua
  49. ]]
  50. end
  51. end
  52.  
  53. function Paddle:render()
  54. love.graphics.rectangle('fill', self.x, self.y, self.width, self.height) -- we are storing references to these variables in the init method
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement