-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPipePair.lua
50 lines (39 loc) · 1.29 KB
/
PipePair.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
--[[
PipePair Class
Author: Colton Ogden
Used to represent a pair of pipes that stick together as they scroll, providing an opening
for the player to jump through in order to score a point.
]]
PipePair = Class{}
function PipePair:init(y, gapHight)
-- flag to hold whether this pair has been scored (jumped through)
self.scored = false
-- initialize pipes past the end of the screen
self.x = VIRTUAL_WIDTH + 32
-- y value is for the topmost pipe; gap is a vertical shift of the second lower pipe
self.y = y
-- instantiate two pipes that belong to this pair
self.pipes = {
['upper'] = Pipe('top', self.y),
['lower'] = Pipe('bottom', self.y + PIPE_HEIGHT + gapHight)
}
-- whether this pipe pair is ready to be removed from the scene
self.remove = false
end
function PipePair:update(dt)
-- remove the pipe from the scene if it's beyond the left edge of the screen,
-- else move it from right to left
if self.x > -PIPE_WIDTH then
self.x = self.x - PIPE_SPEED * dt
self.pipes['lower'].x = self.x
self.pipes['upper'].x = self.x
else
self.remove = true
end
end
function PipePair:render()
for l, pipe in pairs(self.pipes) do
pipe:render()
end
end