-
Notifications
You must be signed in to change notification settings - Fork 0
/
playground.lua
297 lines (240 loc) · 8.49 KB
/
playground.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
local composer = require( "composer" )
local scene = composer.newScene()
local buttonsGroup = display.newGroup()
local bird
local bulletPow = 2
local bulletSpeed = 1000
local score = 0
-- variables to increase the game's dificulties
local minWall = 1
local maxWall = 10
local turnNum = 0
math.randomseed(os.time())
-- -----------------------------------------------------------------------------------
-- Code outside of the scene event functions below will only be executed ONCE unless
-- the scene is removed entirely (not recycled) via "composer.removeScene()"
-- -----------------------------------------------------------------------------------
local function goToMenu()
composer.gotoScene( "menu" )
end
local function tap()
bird:applyLinearImpulse( 0, -0.5, bird.x, bird.y )
-- tapCount = tapCount + 1
-- tapText.text = tapCount
end
-- shoot()
function shoot()
for i = 1, bulletPow, 1
do
local bullet = display.newImageRect( "laser.png", 30 , 30)
bullet.x = bird.x
bullet.y = bird.y
bullet.isBullet = true
bullet.myName = "bullet"
sceneGroup:insert(bullet)
physics.addBody( bullet, "dynamic", { isSensor=true } )
transition.to( bullet, { y = bird.y, x=320, time=1000,
onComplete = function() display.remove( bullet ) end
} )
end
end
-- pause()
function pause()
physics.pause()
transition.pause()
timer.pause( gameLoopTimer1 )
timer.pause( gameLoopTimer3 )
composer.showOverlay( "pause", { isModal = true, time=300, effect="fade" } )
end
-- Custom function for resuming the game (from pause state)
function scene:resumeGame()
--code to resume game
transition.resume()
physics.start()
timer.resume( gameLoopTimer1 )
timer.resume( gameLoopTimer3 )
end
-- -----------------------------------------------------------------------------------
-- Scene event functions
-- -----------------------------------------------------------------------------------
-- create()
function scene:create( event )
sceneGroup = self.view
-- Code here runs when the scene is first created but has not yet appeared on screen
-- background
local background = display.newImageRect( "background.png", 320, 580 )
background.x = display.contentCenterX
background.y = display.contentCenterY
sceneGroup:insert( background )
--buttons
local menuButton = display.newText( sceneGroup, "Back To Menu", 50, 0, native.systemFontBold, 10 )
menuButton:addEventListener( "tap", goToMenu )
local pauseButton = display.newText( sceneGroup, "Pause", 120, 0, native.systemFontBold, 10 )
pauseButton:addEventListener( "tap", pause )
pauseButton:toFront()
currentScore = display.newText( sceneGroup, "" .. score, display.contentCenterX, 50, native.systemFontBold, 30 )
bird = display.newImageRect( "flappy-bird.png", 100, 100 )
bird.x = 16
bird.y = display.contentCenterY
bird.myName = "bird"
sceneGroup:insert( bird )
physics = require( "physics" )
physics.start()
physics.addBody( bird, "dynamic", { radius=40, bounce=0.3 } )
wall()
background:addEventListener( "tap", tap )
end
-- gameover()
function gameover()
bird.alpha = 0
composer.setVariable( "finalScore", score )
composer.gotoScene( "highscores", { time=300, effect="fade" } )
end
-- power(x, y)
function power(x, y)
local powerup = display.newImageRect( "flappy-bird.png", 50 , 50)
powerup.x = x
powerup.y = y
powerup.myName = "powerup"
sceneGroup:insert(powerup)
physics.addBody( powerup, "kinematic", { radius=20, bounce=0, isSensor=true } )
transition.to( powerup, { y = y, x=-50, time=10000,
onComplete = function() display.remove( powerup ) end
} )
end
-- collision()
function collision(event)
if ( event.phase == "began" ) then
local obj1 = event.object1
local obj2 = event.object2
if ( (obj2.myName == "stone" ) or (obj2.myName == "scoreDisplay" )
or ( obj1.myName == "scoreDisplay") or ( obj1.myName == "stone") )
then
--if bird hit the stone
if ((obj2.myName == "bird") or
(obj1.myName == "bird"))
then
gameover()
--in this case the bullet hit the stone
else
if (obj2.score == 1 or bulletPow == -1) then
display.remove(obj2.scoreDisplay)
display.remove(obj2)
else
obj2.score = obj2.score - 1
obj2.scoreDisplay.text = obj2.score
end
display.remove(obj1)
end
end
if (obj2.myName == "powerup" or obj1.myName == "powerup") and (obj2.myName == "bird" or obj1.myName == "bird")
then
local type = math.random(1,3)
print(type)
if type == 1 then bulletPow = bulletPow + 1
elseif type == 2 then bulletSpeed = bulletSpeed/2
elseif type == 3 then bulletPow = score end
if(obj1.myName == "bird") then display.remove(obj2)
else display.remove(obj1) end
end
end
end
-- wall()
function wall()
for i = 0, 7, 1
do
local stone = display.newImageRect( "stone.jpg", 50 , 50)
stone.x = 295
stone.y = 70 * i
stone.myName = "stone"
sceneGroup:insert(stone)
--generate random score for stone
stone.score = math.random(minWall, maxWall)
if stone.score == 5 then power(stone.x, stone.y) end
--display the score next to the stone
stone.scoreDisplay = display.newText( sceneGroup, stone.score, stone.x + 50,
stone.y, native.systemFontBold, 20 )
stone.scoreDisplay:setTextColor( 0, 0, 0 )
stone.scoreDisplay.myName = "scoreDisplay"
physics.addBody( stone, "kinematic", { radius=20, bounce=0, isSensor=true } )
transition.to( stone, { y = stone.y, x=-50, time=10000,
onComplete = function() display.remove( stone ) end
} )
physics.addBody( stone.scoreDisplay, "kinematic", { radius=5})
transition.to( stone.scoreDisplay, { y = stone.scoreDisplay.y, x=0, time=10000,
onComplete = function() display.remove( stone.scoreDisplay ) end
} )
end
end
-- shootLoop()
function shootLoop()
shoot()
end
-- gameLoop()
function gameLoop()
shoot()
end
-- wallLoop()
function wallLoop()
score = score + 1
currentScore.text = "" .. score
if turnNum == 3 then
minWall = minWall + 2
maxWall = maxWall + 2
turnNum = 0
else
turnNum = turnNum + 1
end
wall()
end
function boundCheck()
if (bird ~= nil and bird.y ~= nil and (bird.y < 0 or bird.y > 520))
then
gameover()
end
end
-- show()
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Code here runs when the scene is still off screen (but is about to come on screen)
elseif ( phase == "did" ) then
-- Code here runs when the scene is entirely on screen
Runtime:addEventListener( "collision", collision )
gameLoopTimer1 = timer.performWithDelay( 500, gameLoop, 0 )
gameLoopTimer2 = timer.performWithDelay( 10, boundCheck, 0 )
gameLoopTimer3 = timer.performWithDelay( 15000, wallLoop, 0 )
gameLoopTimer4 = timer.performWithDelay( bulletSpeed, shootLoop, 0 )
end
end
-- hide()
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
timer.cancel( gameLoopTimer1 )
timer.cancel( gameLoopTimer2 )
timer.cancel( gameLoopTimer3 )
timer.cancel( gameLoopTimer4 )
elseif ( phase == "did" ) then
Runtime:removeEventListener( "collision", collision )
physics.pause()
transition.cancel()
composer.removeScene("playground")
end
end
-- destroy()
function scene:destroy( event )
local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view
end
-- -----------------------------------------------------------------------------------
-- Scene event function listeners
-- -----------------------------------------------------------------------------------
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -----------------------------------------------------------------------------------
return scene