-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.lua
66 lines (62 loc) · 1.8 KB
/
pipe.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
require 'params'
RANDOM_Y = 50
PIPE_GAP = 300
PIPE_DISTANCE = 200
pipe = {}
PIPE_IMG = 'pipe.png'
PIPE_IMG_INV = 'pipeInv.png'
pipe.img = love.graphics.newImage(PIPE_IMG)
pipe.imgInv = love.graphics.newImage(PIPE_IMG_INV)
pipe.width = pipe.img:getWidth()
pipe.height = pipe.img:getHeight()
PIPE_POSITION = 0
function initPipe()
-- body
-- number of pipes required for game
npipes = {}
n=0
i=1
while (n < WINDOW_WIDTH) do
table.insert(npipes, i)
n = n + PIPE_DISTANCE
i = i + 1
end
-- initialize the pipe values
n=0
for _,i in ipairs(npipes) do
pipe[i] = {}
pipe[i].pos = n*PIPE_DISTANCE --pipe position x co-ordinates
pipe[i].spawn = false --pipe not spawned
pipe[i].randomY = 0 --init random height of pipe
-- init pipe co-ordinates
pipe[i].x = 0
pipe[i].ytop = 0
pipe[i].ybottom = 0
n=n+1
end
end
function drawPipe(i)
-- body
if pipe[i].spawn then
-- top pipe
love.graphics.draw(pipe.imgInv, WINDOW_WIDTH-pipe[i].pos, -PIPE_GAP+pipe[i].randomY, 0, FACTOR, FACTOR)
-- bottom pipe
love.graphics.draw(pipe.img, WINDOW_WIDTH-pipe[i].pos, WINDOW_HEIGHT/2+100+pipe[i].randomY, 0, FACTOR, FACTOR)
end
end
function updatePipe(dt, i)
--body
PIPE_POSITION = (pipe[i].pos + groundScrollSpeed*dt)%(WINDOW_WIDTH+pipe.width+1) --'1' is added for the below comparision
-- spawn pipes with random heights
if (PIPE_POSITION > WINDOW_WIDTH+pipe.width) then
pipe[i].randomY = love.math.random(-RANDOM_Y,RANDOM_Y)
pipe[i].spawn = true
end
pipe[i].pos = PIPE_POSITION
--note down the co-ordinates of pipes for AABB collision only if pipe is spawned
if pipe[i].spawn then
pipe[i].x = WINDOW_WIDTH-pipe[i].pos
pipe[i].ytop = -PIPE_GAP+pipe[i].randomY
pipe[i].ybottom = WINDOW_HEIGHT/2+100+pipe[i].randomY
end
end