-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
305 lines (241 loc) · 8.45 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
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
298
299
300
301
302
303
304
305
local composer = require( "composer" )
local scene = composer.newScene()
local physics = require "physics"
physics.start()
physics.setGravity( 0, 50 )
--physics.setDrawMode( "hybrid" )
local mydata = require("mydata")
local gameStarted = false
local teto = display.newRect(1, display.actualContentHeight- 800, display.contentWidth*3, 1)
physics.addBody(teto, "static", {friction = 0.1})
-- function preencherVidaPerdida(event)
-- local sceneGroup = scene.view
-- vida1 = display.newImageRect( "Imagens/object.png", 32, 32 )
-- vida2 = display.newImageRect( "Imagens/object.png", 32, 32 )
-- vida3 = display.newImageRect( "Imagens/object.png", 32, 32 )
-- if ( quantErros == 1 ) then
-- vida1.x = 50
-- vida1.y = 50
-- elseif ( quantErros == 2 ) then
-- vida2.x = 100
-- vida2.y = 50
-- elseif (quantErros == 3) then
-- vida3.x = 150
-- vida3.y = 50
-- end
-- end
function backgroundScroller(self,event)
if self.x < (-self.width + (self.speed*2)) then
self.x = self.width
else
self.x = self.x - self.speed
end
end
function flyUp(event)
if event.phase == "began" then
if gameStarted == false then
player.bodyType = "dynamic"
gameStarted = true
score.alpha = 1
adicionarColunasTimer = timer.performWithDelay (1500, adicionarColunas, -1)
andarColunasTimer = timer.performWithDelay (2, andarColunas, -1)
player:applyForce(0, -600, player.x, player.y)
else
player:applyForce(0, -900, player.x, player.y)
end
end
end
function onCollision( event )
if ( event.phase == "began" ) then
composer.gotoScene( "restart" )
end
end
function andarColunas()
for a = elements.numChildren,1,-1 do
if(elements[a].x < player.x - 10) then
if elements[a].scoreAdded == false then
mydata.score = mydata.score + 1
score.text = mydata.score
elements[a].scoreAdded = true
end
end
if (elements[a].x > -100) then
elements[a].x = elements [a].x - groundSpeed
else
elements:remove(elements[a])
end
end
end
function adicionarColunas()
height = math.random (display.contentCenterY -100, display.contentCenterY +100)
topColumn = display.newImageRect('images/cactu.png', 200 , 300)
topColumn.anchorX = 0.5
topColumn.anchorY = 0
topColumn.x = display.contentWidth -1.5
topColumn.y = height - 2
topColumn.scoreAdded = false
physics.addBody(topColumn, "static", {density=1,bounce=0.1, friction=0.2})
elements:insert(topColumn)
downColumn = display.newImageRect('images/cactudown.png', 150 , 250)
downColumn.anchorX = -0.5
downColumn.anchorY = 10
downColumn.x = display.contentWidth + 180
downColumn.y = height - 210
physics.addBody(downColumn, "static", {density=1,bounce=0.1, friction=0.2})
elements:insert(downColumn)
coisa1 = display.newImageRect("images/Skeleton.png", 150 ,100 )
-- coisa1.anchorX = 10
-- coisa1.anchorY = 1
coisa1.x = display.contentWidth + 100
coisa1.y = 500
coisa1.speed = groundSpeed
elements:insert(coisa1)
end
function scene:create( event )
mydata.score = 0
gameStarted = false
local sceneGroup = self.view
skySpeed = .2
backgroundSpeed = 4
groundSpeed = 10
backgroundSize = display.contentWidth
groundSize = display.contentWidth
-- local vida1
-- local vida2
-- local vida3
background1 = display.newImageRect('images/sky_dune001.png', display.contentWidth,550)
background1.anchorX = 0
background1.anchorY = 1
background1.x = 1
background1.y = display.contentHeight - 190
background1.speed = backgroundSpeed
sceneGroup:insert(background1)
background2 = display.newImageRect('images/sky_dune001.png',display.contentWidth ,550)
background2.anchorX = 0
background2.anchorY = 1
background2.x = backgroundSize
background2.y = display.contentHeight - 190
background2.speed = backgroundSpeed
sceneGroup:insert(background2)
elements = display.newGroup()
elements.anchorChildren = true
elements.anchorX = 0
elements.anchorY = 1
elements.x = 0
elements.y = 0
sceneGroup:insert(elements)
-- coisa1 = display.newImageRect("images/Skeleton.png", 100 ,100 )
-- -- coisa1.anchorX = 10
-- -- coisa1.anchorY = 1
-- coisa1.x = 100
-- coisa1.y = 490
-- coisa1.speed = groundSpeed
-- sceneGroup:insert(coisa1)
-- coisa2 = display.newImageRect("images/Skeleton.png", 64, 220 )
-- -- coisa2.anchorX = 100
-- -- coisa2.anchorY = 1
-- coisa2.x = display.contentWidth
-- coisa2.y = 700
-- coisa2.speed = groundSpeed
-- sceneGroup:insert(coisa2)
ground1 = display.newImageRect("images/ground.png", groundSize, 220 )
ground1.anchorX = 0
ground1.anchorY = 1
ground1.x = 0
ground1.y = 750
ground1.speed = groundSpeed
sceneGroup:insert(ground1)
ground2 = display.newImageRect("images/ground.png", groundSize, 220 )
ground2.anchorX = 0
ground2.anchorY = 1
ground2.x = backgroundSize
ground2.y = 750
ground2.speed = groundSpeed
sceneGroup:insert(ground2)
-- vidaDisponivel1 = display.newImageRect( "images/object-vida.png", 64, 64 )
-- vidaDisponivel1.x = 50
-- vidaDisponivel1.y = 50
-- sceneGroup:insert( vidaDisponivel1 )
-- vidaDisponivel2 = display.newImageRect( "images/object-vida.png", 64, 64 )
-- vidaDisponivel2.x = 100
-- vidaDisponivel2.y = 50
-- sceneGroup:insert( vidaDisponivel2 )
-- vidaDisponivel3 = display.newImageRect( "images/object-vida.png", 64, 64 )
-- vidaDisponivel3.x = 150
-- vidaDisponivel3.y = 50
-- sceneGroup:insert( vidaDisponivel3 )
p_options =
{
-- Required params
width = 90,
height = 80,
numFrames = 2,
-- content scaling
sheetContentWidth = 180,
sheetContentHeight = 80,
}
playerSheet = graphics.newImageSheet( "images/pigeon.png", p_options )
player = display.newSprite( playerSheet, { name="player", start=1, count=2, time=150 } )
player.anchorX = 0.5 -- SE MUDAR PARA 0.4 FICA MUITO LOKO
player.anchorY = 0.5 -- SE MUDAR PARA 0.4 FICA MUITO LOKO
player.x = 120
player.y = display.contentCenterY
physics.addBody(player, "static", {density=.1, bounce=0.1, friction=1})
player:play()
sceneGroup:insert(player)
score = display.newText( "Score:" .. mydata.score, 1000, 40, "Comic Sans MS", 80) -- Está no canto superior direito
--(dados.score, 1200, 600, native.systemFont, 100) - Deixa no canto inferior direito
--(dados.score, 100, 600, native.systemFont, 100) - Deixa nno canto inferior Esquerdo
score:setFillColor (0,0,0)
score.alpha = 0
sceneGroup:insert(score)
ground = display.newRect( display.contentCenterX, display.contentHeight -100, backgroundSize, 10 )
ground.alpha = 0
physics.addBody(ground, "static", {density=.1, bounce=0.1, friction=.2})
sceneGroup:insert(ground)
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "did" ) then
composer.removeScene("main")
Runtime:addEventListener("collision", onCollision)
background1.enterFrame = backgroundScroller
Runtime:addEventListener("enterFrame", background1)
background2.enterFrame = backgroundScroller
Runtime:addEventListener("enterFrame", background2)
ground1.enterFrame = backgroundScroller
Runtime:addEventListener("enterFrame", ground1)
ground2.enterFrame = backgroundScroller
Runtime:addEventListener("enterFrame", ground2)
Runtime:addEventListener("enterFrame", elements)
-- Runtime:addEventListener( "enterFrame", preencherVidaPerdida )
Runtime:addEventListener("touch", flyUp)
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
Runtime:removeEventListener("touch", flyUp)
Runtime:removeEventListener("enterFrame", sky1)
Runtime:removeEventListener("enterFrame", sky2)
Runtime:removeEventListener("enterFrame", ground1)
Runtime:removeEventListener("enterFrame", ground2)
Runtime:removeEventListener("enterFrame", background1)
Runtime:removeEventListener("enterFrame", background2)
Runtime:removeEventListener("collision", onCollision)
Runtime:removeEventListener("enterFrame", elements)
-- Runtime:removeEventListener( "enterFrame", preencherVidaPerdida )
timer.cancel(adicionarColunasTimer)
timer.cancel(andarColunasTimer)
end
end
function scene:destroy( event )
local sceneGroup = self.view
end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene