-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
98 lines (85 loc) · 2.13 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
local bird = require('bird')
local base = require('base')
local pipes = require('pipes')
local banners = require('banners')
local sound = require('sound')
local backgrounds = {
love.graphics.newImage('assets/sprites/background-night.png'),
love.graphics.newImage('assets/sprites/background-day.png'),
}
function love.load()
math.randomseed(os.time())
Background = backgrounds[math.random(2)]
Gameover = false
Waiting = true
Score = 0
BirdIsPassing = false
BirdWasPassing = false
bird:load()
base:load()
pipes:load()
end
function love.keypressed(key)
if (key == 'space' or key == 'j') and Waiting then
Waiting = false
sound:playSwooshSound()
end
if (key == 'space' or key == 'j') and not Gameover then
bird.jumping = true
elseif key == 'return' and Gameover then
love.load()
sound:playSwooshSound()
end
if key == 'escape' or key == 'q' then
love.event.quit()
end
end
local function updateScore()
if BirdIsPassing then
if not BirdWasPassing then
sound:playPointSound()
Score = Score + 1
end
end
BirdWasPassing = BirdIsPassing
end
local function checkPassing()
for i = 1, #pipes.pipes do
if bird.x >= pipes.pipes[i].top.x and bird.x + bird.width <= pipes.pipes[i].top.x + pipes.pipes[i].top.width then
BirdIsPassing = true
return
end
end
BirdIsPassing = false
end
function love.update(dt)
if Waiting then
bird:waiting(dt)
return
end
if not Gameover then
checkPassing()
updateScore()
Gameover = bird:isCollidingWPipes(pipes.pipes) or bird:isCollidingWBase()
if Gameover then
sound:playDieSound()
end
base:update(dt)
pipes:update(dt)
end
bird:update(dt)
end
function love.draw()
love.graphics.draw(Background, 0, 0, 0, Scale, Scale)
pipes:draw()
base:draw()
bird:draw()
if Waiting then
banners:waiting()
else
banners:score(Score)
end
if Gameover then
banners:gameover()
end
end