L5-build-cookie-stacker.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // set up
  2. scene.setBackgroundColor(5)
  3. game.showLongText("Stack cookies then geuss how many you stacked!", DialogLayout.Bottom)
  4. //variable set up
  5. let chocCookieY = 100
  6. let sugarCookieY = 100
  7. let chocCookieStack = 0
  8. let sugarCookieStack = 0
  9. let canStack = true //init to true
  10. info.startCountdown(10)
  11. //plate sprite code
  12. let plate1 = sprites.create(img`
  13. ...............bbbbbbbbbbbbbbbbbbb...............
  14. ...........bbbbdd111111111111111ddbbbb...........
  15. ........bbbd1111111111111111111111111dbbb........
  16. ......bbd11111111dddddddddddddd111111111dbb......
  17. ....bbd1111111ddd11111111111111dddd1111111dbb....
  18. ...bd111111ddd111111111111111111111ddd111111db...
  19. ..bd11111ddd111ddddddddddddddddddd111ddd11111db..
  20. .bd11111dd111dddd111111111111111dddd111dd11111db.
  21. .b11111d111ddd111111111111111111111ddd111d11111b.
  22. bd11111d1ddd1111111111111111111111111ddd1111111db
  23. b11111d1ddd111111111111111111111111111ddd1d11111b
  24. b11111ddddd111111111111111111111111111ddddd11111b
  25. b11111ddddd111111111111111111111111111dddbd11111b
  26. b111111dddd111111111111111111111111111dddb111111b
  27. bd111111dddd1111111111111111111111111dddbd11111db
  28. .b1111111dddd11111111111111111111111dddbd111111b.
  29. .bd1111111dbbdd1111111111111111111dddbbd111111db.
  30. ..bd11111111dbbdd111111111111111dddbbd1111111db..
  31. ...bd111111111dbbbbbbdddddddddddddd111111111db...
  32. ....bbd11111111111dbbbbbbbbbddd11111111111dbb....
  33. ......bbdd11111111111111111111111111111ddbb......
  34. ........bbbdd11111111111111111111111ddbbb........
  35. ...........bbbbbddd11111111111dddbbbbb...........
  36. ................bbbbbbbbbbbbbbbbb................
  37. `, SpriteKind.Player)
  38. plate1.setPosition(50, 100)
  39. let plate2 = sprites.create(img`
  40. ...............bbbbbbbbbbbbbbbbbbb...............
  41. ...........bbbbdd111111111111111ddbbbb...........
  42. ........bbbd1111111111111111111111111dbbb........
  43. ......bbd11111111dddddddddddddd111111111dbb......
  44. ....bbd1111111ddd11111111111111dddd1111111dbb....
  45. ...bd111111ddd111111111111111111111ddd111111db...
  46. ..bd11111ddd111ddddddddddddddddddd111ddd11111db..
  47. .bd11111dd111dddd111111111111111dddd111dd11111db.
  48. .b11111d111ddd111111111111111111111ddd111d11111b.
  49. bd11111d1ddd1111111111111111111111111ddd1111111db
  50. b11111d1ddd111111111111111111111111111ddd1d11111b
  51. b11111ddddd111111111111111111111111111ddddd11111b
  52. b11111ddddd111111111111111111111111111dddbd11111b
  53. b111111dddd111111111111111111111111111dddb111111b
  54. bd111111dddd1111111111111111111111111dddbd11111db
  55. .b1111111dddd11111111111111111111111dddbd111111b.
  56. .bd1111111dbbdd1111111111111111111dddbbd111111db.
  57. ..bd11111111dbbdd111111111111111dddbbd1111111db..
  58. ...bd111111111dbbbbbbdddddddddddddd111111111db...
  59. ....bbd11111111111dbbbbbbbbbddd11111111111dbb....
  60. ......bbdd11111111111111111111111111111ddbb......
  61. ........bbbdd11111111111111111111111ddbbb........
  62. ...........bbbbbddd11111111111dddbbbbb...........
  63. ................bbbbbbbbbbbbbbbbb................
  64. `, SpriteKind.Player)
  65. plate2.setPosition(110, 100)
  66. //on A button pressed
  67. controller.A.onEvent(ControllerButtonEvent.Pressed, function () {
  68. if (canStack == true) {
  69. let chocCookie = sprites.create(assets.image`myImage`, SpriteKind.Player)
  70. chocCookie.setPosition(50, chocCookieY)
  71. chocCookieY -=2
  72. chocCookieStack += 1
  73. }
  74. })
  75. //on B button pressed
  76. controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
  77. if (canStack == true) {
  78. let sugarCookie = sprites.create(assets.image`myImage0`, SpriteKind.Player)
  79. sugarCookie.setPosition(110, sugarCookieY)
  80. sugarCookieY -=2 // changed from 0-2
  81. sugarCookieStack += 1
  82. }
  83. })
  84. //on countdown end
  85. info.onCountdownEnd(function () {
  86. canStack = false
  87. pause(1000)
  88. music.play(music.melodyPlayable(music.powerUp), music.PlaybackMode.UntilDone)
  89. //cleaned up playerGuess
  90. let playerGuess = game.askForNumber("How manyt cookies do you think you stacked?", 2)
  91. let cookieStackTotal = chocCookieStack + sugarCookieStack
  92. game.showLongText("Wow! You stacked " + chocCookieStack + " chocolate chip cookies and " + sugarCookieStack + " sugar cookies!", DialogLayout.Full)
  93. //cleaned up if
  94. if (playerGuess == cookieStackTotal) {
  95. game.showLongText("You guessed correctly - you're one smart cookie! " + cookieStackTotal + " cookies were stacked!", DialogLayout.Full)
  96. } else if (playerGuess != cookieStackTotal) {
  97. game.showLongText("You guessed "+ playerGuess + " cookies, but you stacked " + cookieStackTotal + " cookies instead!", DialogLayout.Full)
  98. }
  99. })