-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
144 lines (131 loc) · 3.51 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
push = require 'push'
require 'params'
require 'bird'
require 'pipe'
START = 0
JUMP = 0
function initWindow()
-- body
CUSTOM_WIDTH, CUSTOM_HEIGHT = love.graphics.getDimensions()
-- love.window.setMode(WINDOW_WIDTH, WINDOW_HEIGHT, {
push:setupScreen(WINDOW_WIDTH, WINDOW_HEIGHT, VIRTUAL_WIDTH, VIRTUAL_HEIGHT, {fullscreen = false,
resizable = true,
vsync = true,
})
love.window.setTitle(TITLE)
love.graphics.setDefaultFilter('nearest', 'nearest')
love.window.background_img = love.graphics.newImage(BACKGROUND_IMG)
love.window.ground_img = love.graphics.newImage(GROUND_IMG)
end
function drawWindow(...)
-- body
love.graphics.draw(love.window.background_img, -backgroundScroll, 0, 0, 5, 3)
love.graphics.draw(love.window.ground_img, -groundScroll, WINDOW_HEIGHT-150, 0, 5, 3)
love.graphics.printf("Press Enter to Start the game", 0, WINDOW_HEIGHT/2, WINDOW_WIDTH, 'center')
end
function updateWindow(dt)
-- body
backgroundScroll = (backgroundScroll + backgroundScrollSpeed*dt)%(BACKGROUND_IMG_LOOPING_POINT)
groundScroll = (groundScroll + groundScrollSpeed*dt)%(GROUND_IMG_LOOPING_POINT)
end
function love.keypressed(key)
-- body
love.keyboard.keyspressed[key] = true
if key == 'escape' then
love.event.quit()
end
end
function checkCollision(x1, y1, w1, h1, x2, y2, w2, h2)
-- body
objMarginW = 8
objMarginH = 4
return (x1 < x2+w2-objMarginW and x2 < x1+w1-objMarginW and y1 < y2+h2-objMarginH and y2 < y1+h1-objMarginH)
end
function playSound(sound)
-- body
source = love.audio.newSource(sound, 'static')
love.audio.play(source)
end
function love.touchpressed(id, x, y)
--Do stuff when a touch is released
if x < CUSTOM_WIDTH/2 then
START = 1
else
JUMP = 1
end
end
function love.load()
--init window
love.keyboard.keyspressed = {}
love.math.setRandomSeed(os.time())
initWindow()
initPipe()
end
function love.draw()
-- body
push:start()
if flag == 0 then
push:resize(CUSTOM_WIDTH, CUSTOM_HEIGHT)
flag=1
end
drawWindow()
drawBird()
for _,i in ipairs(npipes) do
drawPipe(i)
end
love.graphics.printf("Score: "..SCORE, 0, 0, 100, 'center')
love.graphics.printf("FPS: "..love.timer.getFPS(), 0, 15, 100, 'center')
push:finish()
end
function love.update(dt)
-- body
if love.keyboard.keyspressed['return'] or START == 1 then
-- reset environment
if game_state == 2 then
initPipe()
BIRD_Y = 0
BIRD_DY = 0
SCORE = 0
end
-- Start game
if game_state ~= 1 then
start_time = os.time()
end
game_state = 1
end
-- Game started
if game_state == 1 then
updateWindow(dt)
-- update bird
if love.keyboard.keyspressed['space'] or JUMP == 1 then
BIRD_DY = -bird.jump
playSound(JUMP_SOUND)
end
updateBird(dt)
-- update pipes
for _,i in ipairs(npipes) do
updatePipe(dt, i)
end
SCORE = os.difftime(os.time(),start_time)
end
-- detect bird and pipe collision
for _,i in ipairs(npipes) do
if pipe[i].spawn and game_state == 1 then
if checkCollision(bird.x, bird.y, bird.width, bird.height, pipe[i].x, pipe[i].ytop, pipe.width, pipe.height)
or checkCollision(bird.x, bird.y, bird.width, bird.height, pipe[i].x, pipe[i].ybottom, pipe.width, pipe.height) then
game_state = 2
playSound(HIT_SOUND)
end
end
end
-- reset keyspressed
love.keyboard.keyspressed = {}
START = 0
JUMP = 0
end
function love.resize(w, h)
-- body
push:resize(w, h)
CUSTOM_WIDTH=w
CUSTOM_WIDTH=h
end