Advertisement
joseleeph

Untitled

Dec 25th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. -- it is very important to capitalize the file. it's the norm and expected that you capitalize classes
  2.  
  3. Paddle = Class{}
  4. -- takes the class library and generates a new paddle object that will have methods and fields that can be described after the declaration
  5. -- in order to get it to work, you need to import the library in main.lua with class = require 'class'
  6.  
  7. function Paddle:init(x, y, width, height)
  8. --[[
  9. the colon is important and allows objects to call methods.
  10. : means if an object exists it will use a function that works specifically on that object
  11. if you use . you don't need to instatiate an object
  12. 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
  13. in a module called love
  14. 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 :
  15. ]]
  16. 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
  17. 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
  18. self.width = width
  19. self.height = height
  20.  
  21. 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
  22. end
  23.  
  24. --[[
  25. any class defined needs a function that creates the object that does the work of assigning the right fields
  26. and getting it readey to use
  27. ]]
  28.  
  29. function Paddle:update(dt) -- we have been doing this in main.lua. we will instead do it in the paddle class.
  30. -- 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
  31. if self.dy < 0 then -- if it's less than 0 it's going towards the top of the screen...
  32. 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
  33. elseif self.dy > 0 then
  34. self.y = math.min(VIRTUAL_HEIGHT - 20, self.y + self.dy * dt)
  35. -- same for other case. we nolonger have access to player1y we now only have access to self.
  36. -- check this very carefully, you were probably making a mistake here that you asked on reddit
  37. -- the video says
  38. --[[
  39. if self.dy < 0
  40. 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
  41. elseif self.dy > 0 then
  42. self.y
  43.  
  44. the paddles now work as before but are now stored in a class instead of a variable stored in main.lua
  45. ]]
  46. end
  47. end
  48.  
  49. function Paddle:render()
  50. love.graphics.rectangle('fill', self.x, self.y, self.width, self.height) -- we are storing references to these variables in the init method
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement