-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel1.lua
executable file
·546 lines (451 loc) · 16.9 KB
/
level1.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
local composer = require( "composer" )
local widget = require( "widget" )
local sheetInfo = require( "sheet" )
local scene = composer.newScene()
-- -----------------------------------------------------------------------------------------------------------------
-- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called.
-- -----------------------------------------------------------------------------------------------------------------
-- local forward references should go here
-- characters
local alex, parplin
local level, attempt = 1, 1
-- wigetbuttons
local btnGo, btnRock, btnPaper, btnScissor, btnWin, btnLose, messageBox, textMessage, btnQuit
---------- ALEX KIDD -------------------------------------
local options =
{
frames = {
{ x = 1, y = 2, width = 16, height = 25}, --frame 1
{ x = 18, y = 2, width = 16, height = 25}, --frame 2
{ x = 35, y = 2, width = 16, height = 25}, --frame 3
{ x = 52, y = 2, width = 16, height = 25}, --frame 4
{ x = 1, y = 54, width = 16, height = 24}, --ready1
{ x = 19, y = 54, width = 16, height = 24}, --ready2
{ x = 37, y = 54, width = 29, height = 24}, -- rock
{ x = 67, y = 54, width = 33, height = 24}, -- scissor
{ x = 101, y = 54, width = 33, height = 24}, -- paper
}
}
local sheet = graphics.newImageSheet( "res/kidd.png", options )
local seqData = {
{name = "normal", start=1 , count = 4, time = 800},
{name = "faster", frames={1,2,3,4}, time = 400},
{name = "shake", frames={5,6}, time = 500},
{name = "rock", frames={7}},
{name = "paper", frames={9}},
{name = "scissor", frames={8}},
}
---------- PARPLIN ---------------------------------------
local baddieSheet = graphics.newImageSheet( "res/chars.png", sheetInfo:getSheet() )
-- Create animation sequence parplin
local seqDataParplin = {
{name = "taunt", frames={15,16}, time = 500},
{name = "shake", frames={12,13}, time = 500},
{name = "set", frames={11}, time = 10, loopCount=1},
{name = "rock", frames={10}, time = 5000},
{name = "paper", frames={9}, time = 5000},
{name = "scissor", frames={14}, time = 5000},
}
---------- BUTTONS ---------------------------------------
local btnOpt =
{
frames = {
{ x = 3, y = 2, width=70, height = 22}, --frame 1
{ x = 78, y = 2, width=70, height = 22}, --frame 2
}
}
local buttonSheet = graphics.newImageSheet( "res/button.png", btnOpt )
---------- DELETE ---------------------------------------
-- this will make deleting things much easier.
local function delete(obj)
if obj then obj:removeSelf(); obj = nil end
end
--================== Buttons to Change Scenes ===============--
-- delete all the message stuff and go to level 2
local function level2BtnListener( event )
delete( messageBox )
delete( textMessage )
delete( btnWin )
delete( btnLose )
delete( btnQuit )
composer.gotoScene( "level2", "fade", 800)
end
-- delete all the message stuff and go to level 1
local function level1BtnListener( event )
delete( messageBox )
delete( textMessage )
delete( btnWin )
delete( btnLose )
delete( btnQuit )
composer.gotoScene( "level1", "fade", 800)
end
-- delete all the message stuff and go to start screen
local function startScreenBtnListener( event )
delete( messageBox )
delete( textMessage )
delete( btnWin )
delete( btnLose )
delete( btnQuit )
composer.gotoScene( "startScreen", "fade", 800)
end
-- display a message at the end of the screen.
local function nextLevelMessage()
attempt = attempt + 1
messageBox = display.newRoundedRect( display.contentCenterX, display.contentCenterY, 200, 100, 5 )
messageBox:setFillColor(.2,.2,.2,.4)
textMessage = display.newText( "Hello, World", display.contentCenterX, display.contentCenterY, 100, 50, arial ,20 )
-- you win
if alex.win > alex.lose then
textMessage.text = "You Win"
btnWin = widget.newButton( -- contiune to next level
{
x = display.contentCenterX+50,
y = display.contentCenterY+35,
id = "btnWin",
label = "Continue",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = level2BtnListener,
isEnabled = true,
}
)
btnQuit = widget.newButton( -- quit and exit to start screen
{
x = display.contentCenterX-50,
y = display.contentCenterY+35,
id = "btnLose",
label = "Quit",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = startScreenBtnListener,
isEnabled = true,
}
)
else
textMessage.text = "You Lose"
btnQuit = widget.newButton( -- quit and exit to start screen
{
x = display.contentCenterX-50,
y = display.contentCenterY+35,
id = "btnLose",
label = "Quit",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = startScreenBtnListener,
isEnabled = true,
}
)
btnLose = widget.newButton( -- retry the current level.
{
x = display.contentCenterX+50,
y = display.contentCenterY+35,
id = "btnLose",
label = "Retry",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = level1BtnListener,
isEnabled = true,
}
)
end
end
--^^^^^^^^^^^^^^^^^ Buttons to Change Scenes ^^^^^^^^^^^^^^^--
--==================== Parplin Transitions ===================--
-- some forward declarations of the object assosiated with parplin
local playMove, playPreview, moveTable, playShake, playCount
playCount = 1; moveTable = {}
alexPlay = nil
local parplinPlayedHand
-- user played rock
local function alexPlayedRock( event )
alexPlay = true
alex.choice = 1
end
-- user played paper
local function alexPlayedPaper( event )
alexPlay = true
alex.choice = 2
end
-- user played scissor
local function alexPlayedScissor( event )
alexPlay = true
alex.choice = 3
end
-- display the user choice if clicked in time, to register.
function playMove()
parplinPlayedHand.alpha = 1
parplinPlayedHand = display.newSprite (baddieSheet, seqDataParplin)
parplinPlayedHand.x = display.contentCenterX+52
parplinPlayedHand.y = display.contentCenterY+42
parplin.anchorX = 1
parplin.anchorY = 1
if moveTable[playCount] == 1 and playCount <= 3 then
-- show parplin move and set the sequence
transition.to(parplin, {time = 1500, onComplete=playShake})
parplin:setSequence( "set" )
parplin:play()
parplinPlayedHand:setSequence( "rock" )
parplinPlayedHand:play()
elseif moveTable[playCount] == 2 and playCount <= 3 then
-- show parplin move and set the sequence
transition.to(parplin, {time = 1500, onComplete=playShake})
parplin:setSequence( "set" )
parplin:play()
parplinPlayedHand:setSequence( "paper" )
parplinPlayedHand:play()
elseif moveTable[playCount] == 3 and playCount <= 3 then
-- show parplin move and set the sequence
transition.to(parplin, {time = 1500, onComplete=playShake})
parplin:setSequence( "set" )
parplin:play()
parplinPlayedHand:setSequence( "scissor" )
parplinPlayedHand:play()
end
--display alex kidd move, and calculate the win/lose
--this implementation will also count a tie as a win for alex kidd....
-- because he is the hero.
if alexPlay then
alexPlay = false
if alex.choice == 1 then
alex:setSequence("rock")
alex:play()
if moveTable[playCount] == 2 then alex.lose = alex.lose + 1 else alex.win = alex.win + 1 end
elseif alex.choice == 2 then
alex:setSequence("paper")
alex:play()
if moveTable[playCount] == 3 then alex.lose = alex.lose + 1 else alex.win = alex.win + 1 end
elseif alex.choice == 3 then
alex:setSequence("scissor")
alex:play()
if moveTable[playCount] == 1 then alex.lose = alex.lose + 1 else alex.win = alex.win + 1 end
end
else
-- if the user didnt play in time, play the loseing hand for alex kidd.
alexPlay = false
if moveTable[playCount] == 1 then
alex:setSequence("scissor")
alex:play()
elseif moveTable[playCount] == 2 then
alex:setSequence("rock")
alex:play()
else
alex:setSequence("paper")
alex:play()
end
alex.lose = alex.lose + 1 -- inc the lose
end
playCount = playCount + 1
end
function playShake()
-- remove the last played hand from parplin
if parplinPlayedHand then delete(parplinPlayedHand) end
-- total number of hand to play is three
if playCount > 3 then
parplin:setSequence("taunt") -- set parplin to taunt
parplin:play()
alex:setSequence("normal")
alex:pause() -- pause alex kidd
alex:setFrame(2) -- setup for replay and stop alex animation
playCount = 1 -- setup for replay set playcount to 1
transition.fadeOut( btnRock, { time=1000, onComplete=delete } )
transition.fadeOut( btnPaper, { time=1000, onComplete=delete} )
transition.fadeOut( btnScissor, { time=1000, onComplete=delete} )
nextLevelMessage()
--add what happens on a win or lose
else
-- continue the play sequence and go to the next hand.
parplin:setSequence("shake") -- shake for 1500ms
parplin:play()
alex:setSequence("shake") -- shake for 1500ms
alex:play()
transition.to(parplin, {time = 1500, onComplete=playMove})
end
end
function playPreview()
-- create ai moves
for i=1,3 do
moveTable[i] = math.random(1,3)
-- add the hand to the screen as a preview above parplins head.
local parplinHand = display.newSprite (baddieSheet, seqDataParplin)
parplinHand.x = display.contentCenterX+42 + 16*(i-1)
parplinHand.y = display.contentCenterY+15
if (moveTable[i] == 1) then -- show rock
parplinHand:setSequence( "rock" )
parplinHand:play()
elseif (moveTable[i] == 2) then -- show paper
parplinHand:setSequence( "paper" )
parplinHand:play()
elseif (moveTable[i] == 3) then -- show scissor
parplinHand:setSequence( "scissor" )
parplinHand:play()
end
-- after 5s remove and delete the preview.
local handTimer = timer.performWithDelay(5000, function()
delete(parplinHand)
end, 1)
end
-- move to shake and start the play loop
transition.to(parplin, {time = 5000, onComplete=playShake})
end
--==================== Parplin Transitions ===================--
-- play the walking animation
local function startWalking( event )
alex:play()
transition.fadeOut( btnGo, { time=1000, onComplete=delete } )
end
-- stop the waling animation and show preview
local function stopWalking( event )
alex:pause()
alex:setFrame(2)
playPreview() -- show the preview
end
-- listener from the widget
local function go( event )
transition.to(alex, {time = 2500, x=135, onStart=startWalking, onComplete=stopWalking})
end
-- -------------------------------------------------------------------------------------
-- "scene:create()"
function scene:create( event )
local sceneGroup = self.view
-- Initialize the scene here.
local bg = display.newImage( sceneGroup, "res/bg.png", display.contentWidth/2, display.contentHeight/2 )
bg.width = display.contentWidth
bg.height = display.contentHeight
alex = display.newSprite (sheet, seqData)
alex:setFrame(2)
alex.x = display.contentCenterX-95
alex.y = display.contentCenterY+58
alex.lose = 0 -- set the win/lose to 0
alex.win = 0
alex.anchorX = 0
alex.anchorY = 1
parplin = display.newSprite (baddieSheet, seqDataParplin)
parplin.x = display.contentCenterX+75
parplin.y = display.contentCenterY+58
parplin.anchorX = 1
parplin.anchorY = 1
-- Example: add display objects to "sceneGroup", add touch listeners, etc.
sceneGroup:insert( alex )
sceneGroup:insert( parplin )
end
-- "scene:show()"
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is still off screen (but is about to come on screen).
btnGo = widget.newButton(
{
x = 200,
y = 20,
id = "btnGo",
label = "Go!",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
alpha = 0,
onPress = go,
}
)
btnGo.param = sceneGroup
btnRock = widget.newButton(
{
x = 80,
y = 20,
id = "btnRock",
label = "Rock",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = alexPlayedRock,
isEnabled = true,
}
)
btnPaper = widget.newButton(
{
x = 80,
y = 50,
id = "btnPaper",
label = "Paper",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = alexPlayedPaper,
isEnabled = true,
}
)
btnScissor = widget.newButton(
{
x = 80,
y = 80,
id = "btnScissor",
label = "Scissor",
labelColor = { default={ 0, 0, 0 }, over={ 0, 0, 0 } },
sheet = buttonSheet,
defaultFrame = 1,
overFrame = 2,
onPress = alexPlayedScissor,
isEnabled = true,
}
)
-- add parplin to the sceneGroup and set alpha to 0
parplinPlayedHand = display.newSprite (baddieSheet, seqDataParplin)
parplinPlayedHand.alpha = 0
-- reset alex kidds posistion back to start
alex.x = display.contentCenterX-95
alex.win = 0 --reset
alex.lose = 0
--insert everything
sceneGroup:insert( btnGo )
sceneGroup:insert( btnPaper )
sceneGroup:insert( btnRock )
sceneGroup:insert( btnScissor )
sceneGroup:insert( parplinPlayedHand )
elseif ( phase == "did" ) then
parplin:play()
local text = "level "..level.." Attempt "..attempt
local title = display.newText( sceneGroup, text, display.contentCenterX, 15, 100, 30, monospaced, 5 )
title:setFillColor( 0,0,0 )
-- Called when the scene is now on screen.
-- Insert code here to make the scene come alive.
-- Example: start timers, begin animation, play audio, etc.
end
end
-- "scene:hide()"
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == "will" ) then
-- Called when the scene is on screen (but is about to go off screen).
-- Insert code here to "pause" the scene.
-- Example: stop timers, stop animation, stop audio, etc.
elseif ( phase == "did" ) then
-- Called immediately after scene goes off screen.
end
end
-- "scene:destroy()"
function scene:destroy( event )
local sceneGroup = self.view
-- Called prior to the removal of scene's view ("sceneGroup").
-- Insert code here to clean up the scene.
-- Example: remove display objects, save state, etc.
end
-- -------------------------------------------------------------------------------
-- Listener setup
scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )
-- -------------------------------------------------------------------------------
return scene