Commit 453f708c by anshve

Upload New File

parent ad1538c2
Showing with 52 additions and 0 deletions
Ball = Class{}
function Ball:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
self.dy = math.random(-50, 50)
end
function Ball:collides(box)
if self.x > box.x + box.width or self.x + self.width < box.x then
return false
end
if self.y > box.y + box.height or self.y + self.height < box.y then
return false
end
return true
end
function Ball:reset()
self.x = VIRTUAL_WIDTH / 2 - 2
self.y = VIRTUAL_HEIGHT / 2 - 2
self.dy = math.random(-50, 50) * 1.5
end
function Ball:update(dt)
-- ball hits upper or lower boundary, when bounce the ball
if self.y <= 0 then
self.dy = -self.dy
self.y = 0
gSounds['wall_hit']:play()
elseif self.y >= VIRTUAL_HEIGHT - 4 then
self.dy = -self.dy
self.y = VIRTUAL_HEIGHT -4
gSounds['wall_hit']:play()
end
self.x = self.x + self.dx * dt * 1.3
self.y = self.y + self.dy * dt * 1.3
end
function Ball:render()
love.graphics.setColor({1, 1, 1, 1})
love.graphics.rectangle('fill', self.x, self.y, 4, 4) -- renders the ball
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment