/
ball.lua
41 lines (34 loc) · 899 Bytes
/
ball.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
Ball = Entity:extend()
function Ball:new()
Ball.super.new(self, 400, 300, 15, 15)
self.xSpeed = 400
self.ySpeed = -500
end
function Ball:update(dt)
Ball.super.update(self, dt)
end
function Ball:bounce(e)
local left1 = self.x
local right1 = self.x + self.width
local top1 = self.y
local bottom1 = self.y + self.height
local left2 = e.x
local right2 = e.x + e.width
local top2 = e.y
local bottom2 = e.y + e.height
if left1 < right2 and right1 > left2 and top1 < bottom2 and bottom1 > top2 then
self.xSpeed = -self.xSpeed
love.audio.play(sounds["hit"])
end
end
function Ball:checkScreen()
if self.x + self.width < 0 then
love.audio.play(sounds["score"])
return "left"
elseif self.x > 800 then
love.audio.play(sounds["score"])
return "right"
else
return false
end
end