Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- it is very important to capitalize the file. it's the norm and expected that you capitalize classes
- Paddle = Class{}
- -- takes the class library and generates a new paddle object that will have methods and fields that can be described after the declaration
- -- in order to get it to work, you need to import the library in main.lua with class = require 'class'
- function Paddle:init(x, y, width, height)
- --[[
- the colon is important and allows objects to call methods.
- : means if an object exists it will use a function that works specifically on that object
- if you use . you don't need to instatiate an object
- 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
- in a module called love
- 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 :
- ]]
- 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
- 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
- self.width = width
- self.height = height
- 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
- end
- --[[
- any class defined needs a function that creates the object that does the work of assigning the right fields
- and getting it readey to use
- ]]
- function Paddle:update(dt) -- we have been doing this in main.lua. we will instead do it in the paddle class.
- -- 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
- if self.dy < 0 then -- if it's less than 0 it's going towards the top of the screen...
- 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
- elseif self.dy > 0 then
- self.y = math.min(VIRTUAL_HEIGHT - 20, self.y + self.dy * dt)
- -- same for other case. we nolonger have access to player1y we now only have access to self.
- -- check this very carefully, you were probably making a mistake here that you asked on reddit
- -- the video says
- --[[
- if self.dy < 0
- 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
- elseif self.dy > 0 then
- self.y
- the paddles now work as before but are now stored in a class instead of a variable stored in main.lua
- ]]
- end
- end
- function Paddle:render()
- love.graphics.rectangle('fill', self.x, self.y, self.width, self.height) -- we are storing references to these variables in the init method
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement