-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paddle.lua
62 lines (53 loc) · 2.13 KB
/
Paddle.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
--[[
GD50 2018
Pong Remake
-- Paddle Class --
Author: Colton Ogden
Represents a paddle that can move up and down. Used in the main
program to deflect the ball back toward the opponent.
]]
Paddle = Class{}
--[[
The `init` function on our class is called just once, when the object
is first created. Used to set up all variables in the class and get it
ready for use.
Our Paddle should take an X and a Y, for positioning, as well as a width
and height for its dimensions.
Note that `self` is a reference to *this* object, whichever object is
instantiated at the time this function is called. Different objects can
have their own x, y, width, and height values, thus serving as containers
for data. In this sense, they're very similar to structs in C.
]]
function Paddle:init(x, y, width, height)
self.x = x
self.y = y
self.width = width
self.height = height
self.dy = 0
end
function Paddle:update(dt)
-- math.max here ensures that we're the greater of 0 or the player's
-- current calculated Y position when pressing up so that we don't
-- go into the negatives; the movement calculation is simply our
-- previously-defined paddle speed scaled by dt
if self.dy < 0 then
self.y = math.max(0, self.y + self.dy * dt)
-- similar to before, this time we use math.min to ensure we don't
-- go any farther than the bottom of the screen minus the paddle's
-- height (or else it will go partially below, since position is
-- based on its top left corner)
else
self.y = math.min(VIRTUAL_HEIGHT - self.height, self.y + self.dy * dt)
end
end
--[[
To be called by our main function in `love.draw`, ideally. Uses
LÖVE2D's `rectangle` function, which takes in a draw mode as the first
argument as well as the position and dimensions for the rectangle. To
change the color, one must call `love.graphics.setColor`. As of the
newest version of LÖVE2D, you can even draw rounded rectangles!
]]
function Paddle:render()
love.graphics.rectangle('fill', self.x, self.y, self.width, self.height)
end