-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ball.lua
67 lines (55 loc) · 1.64 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
--[[
GD50 2018
Pong Remake
-- Ball Class --
Author: Colton Ogden
Represents a ball which will bounce back and forth between paddles
and walls until it passes a left or right boundary of the screen,
scoring a point for the opponent.
]]
Ball = Class{}
function Ball:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
-- these variables are for keeping track of our velocity on both the
-- X and Y axis, since the ball can move in two dimensions
self.dy = 0
self.dx = 0
end
--[[
Expects a paddle as an argument and returns true or false, depending
on whether their rectangles overlap.
]]
function Ball:collides(paddle)
-- first, check to see if the left edge of either is farther to the right
-- than the right edge of the other
if self.x > paddle.x + paddle.width or paddle.x > self.x + self.width then
return false
end
-- then check to see if the bottom edge of either is higher than the top
-- edge of the other
if self.y > paddle.y + paddle.height or paddle.y > self.y + self.height then
return false
end
-- if the above aren't true, they're overlapping
return true
end
--[[
Places the ball in the middle of the screen, with no movement.
]]
function Ball:reset()
self.x = VIRTUAL_WIDTH / 2 - 2
self.y = VIRTUAL_HEIGHT / 2 - 2
self.dx = 0
self.dy = 0
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