-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgame.lua
305 lines (220 loc) · 6.32 KB
/
game.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
display.setStatusBar(display.HiddenStatusBar)
local physics = require('physics')
physics.start()
physics.setGravity(0,0)
-- Graphics
-- [Background]
local background
-- [Game Menu View]
local title
local startBtn;
local creditsBtn;
-- [MenunView Group]
local menuView
-- [Score&Lives]
local live
local livesTF
local lives = 3
local scoreTF
local score = 0
local alertScore
-- [Blocks group,Player]
local blocks
local player
-- [CreditsView]
local credits
-- [GameView Group]
local gameView
-- Variables
local moveSpeed = 2
local blockTimer
local liveTimer
-- Functions Declaration
local Main = {}
local addGameMenuView = {}
local initialListeners = {}
local showCredits = {}
local hideCredits = {}
local destroyCredits = {}
local gameView = {}
local addInitialBlocks = {}
local addPlayer = {}
local movePlayer = {}
local addBlock = {}
local addLive = {}
local gameListeners = {}
local update = {}
local collisionHandler = {}
local showAlert = {}
function startGame()
addGameMenuView()
end
function addGameMenuView()
background = display.newImage('bg.png')
title = display.newImage('titleBg.png')
startBtn = display.newImage('startBtn.png')
startBtn.x = display.contentCenterX
startBtn.y = display.contentCenterY
startBtn.name = 'startBtn'
creditsBtn = display.newImage('creditsBtn.png')
creditsBtn.x = display.contentCenterX
creditsBtn.y = display.contentCenterY + 60
creditsBtn.name = 'creditsBtn'
menuView = display.newGroup()
menuView:insert(title)
menuView:insert(startBtn)
menuView:insert(creditsBtn)
initialListeners('add')
end
function initialListeners(action)
if(action == 'add') then
startBtn:addEventListener('tap',gameView)
creditsBtn:addEventListener('tap',showCredits)
else
startBtn:removeEventListener('tap',gameView)
creditsBtn:removeEventListener('tap',showCredits)
end
end
function showCredits()
credits = display.newImage('creditsView.png')
transition.from(credits,{time = 400,x = display.contentWidth*2,transition = easing.outExpo})
credits:addEventListener('tap',hideCredits)
startBtn.isVisible = false
credotsBtn.isVisible = false
end
function hideCredits()
startBtn.isVisible = true
creditsBtn.isVisible = true
transition.to(credits,{time = 600,x = display.contentWidth*2,transition = easing.outExpo,onComplete = destroyCredits})
end
function destroyCredits()
credits:removeEventListener('tap',hideCredits)
display.remove(credits)
credits = nil
end
function gameView()
initialListeners('rmv')
transition.to(menuView,{time = 500,x = -menuView.height,onComplete = function()display.remove(menuView)menuView = nil addInitialBlocks(3)end})
--Score Text
scoreTF = display.newText('0',303,22,system.nativeFont,12)
scoreTF:setTextColor(68,68,68)
--Lives Text
livesTF = display.newText('x3',289,56,system.nativeFont,12)
livesTF:setTextColor(245,248,248)
end
function addInitialBlocks(n)
blocks = display.newGroup()
for i = 1, n do
local block = display.newImage('block.png')
block.x = math.floor(math.random()*(display.contentWidth - block.width))
block.y = (display.contentHeight*0.5) + math.floor(math.random()*(display.contentHeight*0.5))
physics.addBody(block,{density = 1,bounce = 0})
block.bodyType = 'static'
blocks:insert(block)
end
addPlayer()
end
function addPlayer()
player = display.newImage('player.png')
player.x = (display.contentWidth*0.5)
player.y = display.height
physics.addBody(player,{density = 1,friction = 0,bounce = 0})
player.isFixedRotation = true
gameListeners('add')
end
function movePlayer:accelerometer(e)
-- Accelerometer Movement
player.x = display.contentCenterX + (display.contentCenterX * (e.xGravity*3))
-- Borders
if((player.x - player.width * 0.5) < 0) then
player.x = player.width * 0.5
elseif((player.x + player.width * 0.5) > display.contentWidth) then
player.x = display.contentWidth - player.width * 0.5
end
end
function addBlock()
local r = math.floor(math.random()*4)
if(r ~= 0) then
local block = display.newImage('block.png')
block.x = math.random()*(display.contentWidth - (block.width * 0.5))
block.y = display.contentHeight + block.height
physics.addBody(block ,{density = 1, bounce = 0})
blocks:insert(block)
else
local badBlock = display.newImage('badBlock.png')
badBlock.name = 'bad'
physics.addBody(badBlock,{density = 1 ,bounce = 0})
badBlock.bodyType = 'static'
badBlock.x = math.random() * (display.contentWidth - (badBlock.width * 0.5))
badBlock.y = display.contentHeight + badBlock.height
blocks:insert(badBlock)
end
end
function addLive()
live = display.newImage('live.png')
live.name = 'live'
live.x = blocks[blocks.numChildren - 1].x
live.y = blocks[blocks.numChildren - 1].y - live.height
physics.addBody(live,{density = 1,friction = 0,bounce = 0})
end
function gameListeners(action)
if(action == 'add') then
Runtime:addEventListener('accelerometer',movePlayer)
Runtime:addEventListener('enterFrame',update)
blockTimer = timer.performWithDelay(800,addBlock,0)
liveTimer = timer.performWithDelay(8000,addLive,0)
player:addEventListener('collision',collisionHandler)
else
Runtime.removeEventListener('accelerometer',movePlayer)
Runtime.removeEventListener('enterFrame',update)
timer.cancel(blockTimer)
timer.cancel(liveTimer)
blockTimer = nil
liveTimer = nil
player:removeEventListener('collision',collisionHandler)
end
end
function update(e)
--Player Movement
player.y = player.y + moveSpeed
--Score
score = score + 1
scoreTF.text = score
--Lose lives
if(player.y > display.contentHeight or player.y < -5) then
player.x = blocks[blocks.numChildren - 1].x
player.y = blocks[blocks.numChildren - 1].y - player.height
lives = lives - 1
livesTF.text = 'x' .. lives
end
--Check for game over
if(lives < 0) then
showAlert()
end
--Levels
if(score > 500 and score < 502) then
moveSpeed = 3
end
end
function collisionHandler(e)
--Grab Lives
if(e.other.name == 'live') then
display.remove(e.other)
e.other = nil
lives = lives + 1
livesTF.text = 'x' .. lives
end
--Bad Blocks
if(e.other.name == 'bad') then
lives = lives - 1
livesTF.text = 'x' .. lives
end
end
function showAlert()
gameListeners('rmv')
local alert = display.newImage('alert.png',70,190)
alertScore = display.newText(scoreTF.text .. '!',134,240,native.systemFontBold,30)
livesTF.text = ''
transition.from(alert,{time = 200,xScale = 0.8})
end
startGame()