forked from limherhuey/CS50g
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.lua
52 lines (41 loc) · 1.18 KB
/
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
42
43
44
45
46
47
48
49
50
51
52
--[[
This is CS50 2019.
Games Track
Pong
-- Ball Class --
Author: Colton Ogden
cogden@cs50.harvard.edu
]]
Ball = Class{}
function Ball:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
--and ... or here functions the same as ___ == 1 ? -100 : 100 in C or JavaScript
--if math.random() has only one argument, 1 is the other argument
self.dx = math.random(2) == 1 and math.random(80, 100) or math.random(-80, -100)
self.dy = math.random(2) == 1 and -100 or 100
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(2) == 1 and -100 or 100
self.dx = math.random(100, 140)
end
function Ball:update(dt)
self.x = self.x + self.dx * dt
self.y = self.y + self.dy * dt
end
function Ball:render()
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end