Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Ball = Class{}
- 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
- self.x = x
- self.y = y
- self.width = width
- self.height = height
- self.dx = math.random(2) == 1 and 100 or -100
- self.dy = math.random(-50, 50)
- end
- function Ball:update(dt)
- self.x = self.x + self.dx * dt
- self.y = self.y + self.dy * dt
- --self.y = self.x + self.dy * dt
- end
- function Ball:collides(box) -- will tell us if boxes overlap
- 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
- return false
- end
- if self.y is > box.y + box.height or self.y + self.height < box.y then
- return false
- end
- return true
- end
- function Ball:reset()
- self.x = VIRUTAL_WIDTH/2-2
- self.y = VIRTUAL_HEIGHT/2-2
- self.dx = math.random(2) == 1 and -100 or 100
- self.dy = math.random(-50,50) * 1.5
- end
- function Ball:render()
- love.graphics.rectangle('fill', self.x, self.y, 5, 5)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement