Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require('player')
- Ball = {}
- setmetatable(Ball, {
- __call = function(self, x, y, color, speed, dir, ascend)
- ball = {
- x = x,
- y = y,
- color = color,
- speed = 5,
- dir = 1,
- ascend = math.random(-8, 8)
- }
- return setmetatable(ball, {__index = Ball})
- end
- })
- function Ball:spawn()
- self.x = width/2
- self.y = trueHeight/2
- self.color = randColor()
- self.speed = 3
- self.dir = math.random(2)
- self.ascend = math.random(-8, 8)
- end
- function Ball:checkCollision()
- -- Paddle collision
- local leftCol = (self.x >= width-20 and self.y >= players[2].pos and self.y <= players[2].pos+64)
- local rightCol = (self.x <= 20 and self.y >= players[1].pos and self.y <= players[1].pos+64)
- if leftCol or rightCol then
- self.ascend = math.random(-8, 8)
- self.speed = math.random(6, 11)
- if self.ascend < 0 then
- self.ascend = self.ascend * -1
- end
- self.dir = leftCol and 2 or 1
- end
- -- Screen collision
- if (self.y >= gfx.getHeight()) or (self.y <= 4) then
- self.ascend = self.ascend * -1
- end
- end
- function Ball:move()
- -- Update ball positions
- self.x = (self.dir == 1) and (self.x + self.speed) or (self.x - self.speed)
- self.y = self.y + self.ascend
- self:checkCollision()
- -- Spawn ball & add score
- local offLeft, offRight = (self.x < 0), (self.x > width)
- if offLeft or offRight then
- players[offLeft and 2 or 1]:updateScore()
- ball:spawn()
- love.timer.sleep(1)
- end
- end
Add Comment
Please, Sign In to add comment