-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
681 lines (538 loc) · 20.1 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
require ( "physics" )
require( "setup" )
require( "constants" )
require( "cache" )
require( "path_files" )
local ui = require( "ui" )
local composer = require( "composer" )
local scene = composer.newScene()
physics.start()
physics.setGravity( 0, 9.8 * 2)
physics.start()
local images = getImage()
local sounds = getSounds()
local QUESTION_ABOUT_ROOT_SQUARE = 1
local QUESTION_ABOUT_OPERATIONS_4 = 2
local QUESTION_ABOUT_NUMBER_SQUARE = 3
local QUESTION_ABOUT_FATORIAL = 4
local themeFromQuestio = 0
local scores = 0
local endPoints = {}
local maxPoints = 5
local lineThickness = 20
local lineFadeTime = 250
local slashSoundEnabled = true -- sound should be enabled by default on startup
local minTimeBetweenSlashes = 150 -- Minimum amount of time in between each slash sound
local minDistanceForSlashSound = 50 -- Amount of pixels the users finger needs to travel in one frame in order to play a slash sound
local soundCut = audio.loadSound(sounds.PATH_SOUND_SLASH_)
local preExplosion = audio.loadSound(sounds.PATH_SOUND_PRE_EXPLOSION_)
local explosion = audio.loadSound(sounds.PATH_SOUND_EXPLOSION_)
local avalBall = {}
local ballProp = {
density = 1.0,
friction = 0.3,
bounce = 0.2,
filter = {
categoryBits = 2,
maskBits = 1
}
}
local gushProp = {
density = 1.0,
friction = 0.3,
bounce = 0.2,
filter = {
categoryBits = 4,
maskBits = 8
}
}
local minVelocityY = 850
local maxVelocityY = 1100
local minVelocityX = -200
local maxVelocityX = 200
local minAngularVelocity = 100
local maxAngularVelocity = 200
local minAngularVelocityChopped = 100
local maxAngularVelocityChopped = 200
local operatorsSymbol = getOperators()
local operators = {
operatorsSymbol.OPERATOR_MULTIPLICATION_,
operatorsSymbol.OPERATOR_DIVISION_,
operatorsSymbol.OPERATOR_SUBTRACTION_,
operatorsSymbol.OPERATOR_SUM_
}
local minGushRadius = 10
local maxGushRadius = 25
local numOfGushParticles = 15
local gushFadeTime = 500
local gushFadeDelay = 500
local minGushVelocityX = -350
local maxGushVelocityX = 350
local minGushVelocityY = -350
local maxGushVelocityY = 350
local bombTimer
local ballTimer
local responseTimer
local timeRemainingToRespond = 10
local ballShootingInterval = 1000
local bombShootingInterval = 5000
local splashImgs = {}
local splashFadeTime = 2500
local splashFadeDelayTime = 5000
local splashInitAlpha = .5
local splashSlideDistance = 50
local colorBallWithResponse
local listQuestios = {}
local pontuacaoAtual = 0
local drawSlashLine
local soundSuccess = audio.loadSound("sounds/success.wav")
local soundError = audio.loadSound("sounds/success.wav")
local questionToResponse
function scene:create()
local sceneGroup = self.view
local background = setupBackground("images/lousa.png");
sceneGroup:insert(background)
local rootSquare = display.newImage("images/root-square.png", 60, 160)
rootSquare.width = 100
rootSquare.height = 80
rootSquare.isVisible = false
sceneGroup:insert(rootSquare)
themeFromQuestio = composer.getVariable( "operation" )
function createQuestion()
if (themeFromQuestio == QUESTION_ABOUT_ROOT_SQUARE) then
local n1 = math.random(0, 20)
rootSquare.isVisible = true
local value = n1 * n1
return {
question = value .. " = ?",
result = n1
}
elseif (themeFromQuestio == QUESTION_ABOUT_OPERATIONS_4) then
local operator = math.random(1, 4)
local n1 = math.random(0, 9)
local n2 = math.random(0, 9)
local result = 0
local simboloOperator = operators[operator]
if (simboloOperator == operatorsSymbol.OPERATOR_DIVISION_) then
if (n2 == 0) then
n2 = 1
elseif (n1 ~= 0 and math.fmod(n1, n2) ~= 0 ) then
local divisoes = {
{n1 = 9, n2 = 3},
{n1 = 8, n2 = 4},
{n1 = 8, n2 = 2},
{n1 = 6, n2 = 3},
{n1 = 6, n2 = 2},
{n1 = 4, n2 = 2}
}
local divisao = math.random(1, #divisoes)
n1 = divisoes[divisao].n1
n2 = divisoes[divisao].n2
end
result = n1 / n2
elseif (simboloOperator == operatorsSymbol.OPERATOR_MULTIPLICATION_) then
result = n1 * n2
elseif (simboloOperator == operatorsSymbol.OPERATOR_SUBTRACTION_) then
result = n1 - n2
elseif (simboloOperator == operatorsSymbol.OPERATOR_SUM_) then
result = n1 + n2
end
return {
n1 = n1,
n2 = n2,
question = n1 .. " " .. simboloOperator .. " " .. n2 .. " = ?",
simboloOperator = simboloOperator,
result = result
}
elseif (themeFromQuestio == QUESTION_ABOUT_NUMBER_SQUARE) then
local n1 = math.random(0, 20)
return {
question = n1.."² = ?",
result = (n1 * n1),
}
elseif (themeFromQuestio == QUESTION_ABOUT_FATORIAL) then
local n1 = math.random(0, 11)
function fat(n)
if n == 0 then
return 1
else
return n * fat(n - 1)
end
end
return {
question = n1.."! = ?",
result = fat(n1),
}
end
end
local ball = {}
local lifes = {}
local questions = {}
local countLife = 3
function initQuestions()
local LEFT_BALL_ = 170
questions[0] = display.newText("?", LEFT_BALL_, ball[0].y, native.systemFontBold, 40)
questions[1] = display.newText("?", LEFT_BALL_, ball[1].y, native.systemFontBold, 40)
questions[2] = display.newText("?", LEFT_BALL_, ball[2].y, native.systemFontBold, 40)
sceneGroup:insert(questions[0])
sceneGroup:insert(questions[1])
sceneGroup:insert(questions[2])
end
function initLife()
local y = 170
lifes[1] = display.newImage(images.PATH_IMAGE_LIFE_, display.contentWidth - 100, y)
lifes[2] = display.newImage(images.PATH_IMAGE_LIFE_, display.contentWidth - 170, y)
lifes[3] = display.newImage(images.PATH_IMAGE_LIFE_, display.contentWidth - 240, y)
sceneGroup:insert(lifes[1])
sceneGroup:insert(lifes[2])
sceneGroup:insert(lifes[3])
end
function initBall()
local width = 70
local height = 70
local left = 100
ball[0] = display.newImage(images.PATH_IMAGE_BALL_RED_, left, 270)
ball[0].width = width
ball[0].height = height
ball[0].color = "red"
ball[1] = display.newImage(images.PATH_IMAGE_BALL_BLUE_, left, 350)
ball[1].width = width
ball[1].height = height
ball[1].color = "blue"
ball[2] = display.newImage(images.PATH_IMAGE_BALL_YELLOW_, left, 430)
ball[2].width = width
ball[2].height = height
ball[2].color = "yellow"
sceneGroup:insert(ball[0])
sceneGroup:insert(ball[1])
sceneGroup:insert(ball[2])
end
initBall()
initLife()
initQuestions()
questionToResponse = display.newText(" ? = ?", 150, 170, native.systemFontBold, 50)
questionToResponse:setTextColor(255, 255, 255)
sceneGroup:insert(questionToResponse)
local alarm = display.newImage("images/alarm_clock.png", display.contentWidth * 0.45, 170)
alarm.width = 80
alarm.height = 100
sceneGroup:insert(alarm)
local timerTextQuestion = display.newText("10", display.contentWidth * 0.52, 170, native.systemFontBold, 50)
sceneGroup:insert(timerTextQuestion)
pontuacaoAtual = display.newText(scores, display.contentWidth * 0.91, 230, native.systemFontBold, 50)
sceneGroup:insert(pontuacaoAtual)
Runtime:addEventListener("touch", drawSlashLine)
function initBallAndSplash()
local ballRed = {
whole = images.PATH_IMAGE_BALL_RED_,
top = images.PATH_IMAGE_BALL_RED_TOP_,
bottom = images.PATH_IMAGE_BALL_RED_BOTTOM_,
color = "red"
}
local ballBlue = {
whole = images.PATH_IMAGE_BALL_BLUE_,
top = images.PATH_IMAGE_BALL_BLUE_TOP_,
bottom = images.PATH_IMAGE_BALL_BLUE_BOTTOM_,
color = "blue"
}
local ballYellow = {
whole = images.PATH_IMAGE_BALL_YELLOW_,
top = images.PATH_IMAGE_BALL_YELLOW_TOP_,
bottom = images.PATH_IMAGE_BALL_YELLOW_BOTTOM_,
color = "yellow"
}
table.insert(avalBall, ballRed)
table.insert(avalBall, ballBlue)
table.insert(avalBall, ballYellow)
end
function createBallPiece(ball, section)
local ballVelX, ballVelY = ball:getLinearVelocity()
local half = display.newImage(ball[section])
half.x = ball.x - ball.x
local yOffSet = section == "top" and -half.height / 2 or half.height / 2
half.y = ball.y + yOffSet - ball.y
local newPoint = {}
newPoint.x = half.x * math.cos(ball.rotation * (math.pi / 180)) - half.y * math.sin(ball.rotation * (math.pi / 180))
newPoint.y = half.x * math.sin(ball.rotation * (math.pi / 180)) + half.y * math.cos(ball.rotation * (math.pi / 180))
half.x = newPoint.x + ball.x
half.y = newPoint.y + ball.y
half.width = 120
half.height = 60
sceneGroup:insert(half)
half.rotation = ball.rotation
ballProp.radius = half.width / 2
physics.addBody(half, "dynamic", ballProp)
local velocity = math.sqrt(math.pow(ballVelX, 2) + math.pow(ballVelY, 2))
local xDirection = section == "top" and -1 or 1
local velocityX = math.cos((ball.rotation + 90) * (math.pi / 180)) * velocity * xDirection
local velocityY = math.sin((ball.rotation + 90) * (math.pi / 180)) * velocity
half:setLinearVelocity(velocityX, velocityY)
local minAngularVelocity = getRandomValue(minAngularVelocityChopped, maxAngularVelocityChopped)
local direction = (math.random() < .5) and -1 or 1
half.angularVelocity = minAngularVelocity * direction
end
function gameOver ()
Runtime:removeEventListener("touch", drawSlashLine)
timer.cancel(responseTimer)
timer.cancel(ballTimer)
timer.cancel(bombTimer)
composer.removeScene( "game" )
composer.gotoScene( "gameover", {time=800, effect="crossFade"} )
end
function loseLife()
if (countLife > 0 and countLife < 4) then
lifes[countLife].fill.effect = "filter.monotone"
lifes[countLife].fill.effect.r = 0.5
lifes[countLife].fill.effect.g = 0.1
lifes[countLife].fill.effect.b = 0.2
lifes[countLife].fill.effect.a = 1
end
countLife = countLife - 1
if (countLife == 0) then
gameOver()
end
end
function chopBall(ball)
if (colorBallWithResponse == ball.color) then
transition.fadeOut( questionToResponse, { time = 1000 } )
audio.play(soundSuccess, {
duration = 1000,
onComplete = function()
createOtherQuestion()
end
})
scores = scores + 1
pontuacaoAtual.text = scores
composer.setVariable( "scores", scores )
else
loseLife()
end
createBallPiece(ball, "top")
createBallPiece(ball, "bottom")
ball:removeSelf()
end
function getRandomValue(min, max)
return min + math.abs(((max - min) * math.random()))
end
function getRandomBall()
local ballProp
local ball
if (timeRemainingToRespond < 2) then
local i
for i = 1, #avalBall do
if (avalBall[i].color == colorBallWithResponse) then
ballProp = avalBall[i]
ball = display.newImage(ballProp.whole)
end
end
else
ballProp = avalBall[math.random(1, #avalBall)]
ball = display.newImage(ballProp.whole)
end
ball.width = 90
ball.height = 90
ball.whole = ballProp.whole
ball.top = ballProp.top
ball.bottom = ballProp.bottom
ball.color = ballProp.color
return ball
end
function getBomb()
local bomb = display.newImage( "images/bomb.png")
bomb.width = 140
bomb.height = 140
return bomb
end
function blankOutScreen(bomb, linesGroup)
local circle = display.newCircle( bomb.x, bomb.y, 5 )
local circleGrowthTime = 300
local dissolveDuration = 1000
local dissolve = function(event)
transition.to(circle, {
alpha = 0,
time = dissolveDuration,
delay = 0,
onComplete=gameOver()
});
end
circle.alpha = 0
transition.to(circle, {
time=circleGrowthTime,
alpha = 1,
width = display.contentWidth * 3,
height = display.contentWidth * 3,
onComplete = dissolve
})
system.vibrate()
bomb:removeSelf()
linesGroup:removeSelf()
end
function explodeBomb(bomb, listener)
bomb:removeEventListener("touch", listener)
Runtime:removeEventListener("touch", drawSlashLine)
bomb.bodyType = "kinematic"
bomb:setLinearVelocity(0, 0)
bomb.angularVelocity = 0
local stage = display.getCurrentStage()
local moveRightFunction
local moveLeftFunction
local rightTrans
local leftTrans
local shakeTime = 50
local shakeRange = {min = 1, max = 25}
moveRightFunction = function(event) rightTrans = transition.to(stage, {x = math.random(shakeRange.min,shakeRange.max), y = math.random(shakeRange.min, shakeRange.max), time = shakeTime, onComplete=moveLeftFunction}); end
moveLeftFunction = function(event) leftTrans = transition.to(stage, {x = math.random(shakeRange.min,shakeRange.max) * -1, y = math.random(shakeRange.min,shakeRange.max) * -1, time = shakeTime, onComplete=moveRightFunction}); end
moveRightFunction()
local linesGroup = display.newGroup()
local drawLine = function(event)
local line = display.newLine(bomb.x, bomb.y, display.contentWidth * 2, display.contentHeight * 2)
line.rotation = math.random(1,360)
line.strokeWidth = math.random(15, 25)
linesGroup:insert(line)
end
local lineTimer = timer.performWithDelay(100, drawLine, 0)
local explode = function(event)
audio.play(explosion)
blankOutScreen(bomb, linesGroup);
timer.cancel(lineTimer)
stage.x = 0
stage.y = 0
transition.cancel(leftTrans)
transition.cancel(rightTrans)
end
audio.play(preExplosion, {onComplete = explode})
timer.cancel(ballTimer)
timer.cancel(bombTimer)
end
function shootObject(type)
local object = type == "ball" and getRandomBall() or getBomb()
sceneGroup:insert(object)
object.x = display.contentWidth / 2
object.y = display.contentHeight + object.height * 2
ballProp.radius = object.height / 2
physics.addBody(object, "dynamic", ballProp)
if (type == "ball") then
object:addEventListener("touch", function(event) chopBall(object) end)
else
local bombTouchFunction
bombTouchFunction = function(event) explodeBomb(object, bombTouchFunction); end
object:addEventListener("touch", bombTouchFunction)
end
local yVelocity = getRandomValue(minVelocityY, maxVelocityY) * -1
local xVelocity = getRandomValue(minVelocityX, maxVelocityX)
object:setLinearVelocity(xVelocity, yVelocity)
local minAngularVelocity = getRandomValue(minAngularVelocity, maxAngularVelocity)
local direction = (math.random() < .5) and -1 or 1
minAngularVelocity = minAngularVelocity * direction
object.angularVelocity = minAngularVelocity
end
initBallAndSplash()
shootObject("ball")
function createOtherQuestion()
local dataQuestion = createQuestion()
questions[0].text = dataQuestion.result + 1
questions[1].text = dataQuestion.result - 1
questions[2].text = dataQuestion.result - 2
local position = math.random(0, 2)
questions[position].text = dataQuestion.result
table.insert(listQuestios, dataQuestion)
questionToResponse.text = dataQuestion.question
transition.fadeIn( questionToResponse, { time=1000 } )
colorBallWithResponse = ball[position].color
timeRemainingToRespond = 10
timerTextQuestion.text = timeRemainingToRespond
if (responseTimer) then
timer.cancel(responseTimer)
end
responseTimer = timer.performWithDelay(1000, startTimeRemainingToRespond, 0)
local size = string.len(questions[0].text)
local sizeX = 170
if (size == 0) then
rootSquare.x = 130
elseif (size == 1) then
rootSquare.x = 75
else
rootSquare.x = 65
end
if (size > 6) then
sizeX = size * 20 + 80
elseif (size == 3) then
sizeX = 180
elseif (size == 4) then
sizeX = 190
elseif (size == 5) then
sizeX = 200
elseif (size == 6) then
sizeX = 210
end
questions[0].x = sizeX
questions[1].x = sizeX
questions[2].x = sizeX
end
function startTimeRemainingToRespond()
timeRemainingToRespond = timeRemainingToRespond - 1
timerTextQuestion.text = timeRemainingToRespond
if (timeRemainingToRespond == 0) then
timer.cancel(responseTimer)
loseLife()
if (countLife > 0) then
createOtherQuestion()
end
end
end
local parar = display.newImage("images/parar5.png", display.contentWidth * 0.92, display.contentHeight * 0.75)
parar.width = 90
parar.height = 90
sceneGroup:insert(parar)
parar:addEventListener("tap", function()
gameOver();
end)
createOtherQuestion()
bombTimer = timer.performWithDelay(bombShootingInterval, function(event) shootObject("bomb") end, 0)
ballTimer = timer.performWithDelay(ballShootingInterval, function(event) shootObject("ball") end, 0)
end
drawSlashLine = function (event)
if(endPoints ~= nil and endPoints[1] ~= nil) then
local distance = math.sqrt(math.pow(event.x - endPoints[1].x, 2) + math.pow(event.y - endPoints[1].y, 2))
if (distance > minDistanceForSlashSound and slashSoundEnabled == true) then
audio.play(soundCut)
slashSoundEnabled = false
timer.performWithDelay(minTimeBetweenSlashes,
function(event)
slashSoundEnabled = true
end
)
end
end
table.insert(endPoints, 1, {x = event.x, y = event.y, line= nil})
if (#endPoints > maxPoints) then
table.remove(endPoints)
end
for i,v in ipairs(endPoints) do
local line = display.newLine(v.x, v.y, event.x, event.y)
line.strokeWidth = lineThickness
transition.to(line, {
time = lineFadeTime,
alpha = 0,
strokeWidth = 0,
onComplete = function(event)
line:removeSelf()
end
})
end
if (event.phase == "ended") then
while(#endPoints > 0) do
table.remove(endPoints)
end
end
end
function scene:show( )
composer.setVariable( "scores", 0 )
end
function scene:hide( ) end
function scene:destroy( ) end
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
return scene