04-02-hungry-hungry-ninja.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. Ninjas use conditionals and if statements to keep score and move an object when
  3. a key is pressed.
  4. Reinforced Vocabulary and Concepts
  5. * Comparison Operators
  6. * Functions and Parameters
  7. * GDP isKeyPressed() Function
  8. * GDP Keys Object
  9. * GPD .x() and .y() Functions
  10. * if() Statements
  11. * Object Names
  12. * Pseudocode
  13. Sensei Notes
  14. * This game introduces the use of the x and y functions to set the position of
  15. an object. Previously they were used to get the position of an object.
  16. * This game will give the Ninja the last pieces of knowledge they need to
  17. complete the final activity in the White Belt.
  18. * The Ninja will need to write the code to keep score.
  19. * This is the most code that a Ninja has written in a single game, but it is a
  20. series of similar if statements.
  21. Solution Steps
  22. 1 Make sure the Ninja opens the correct scene.
  23. 2 The Ninja should play the game to see what functionality needs to be added.
  24. 3 Have the Ninja copy the provided pseudocode down as a reference.
  25. 4 Have the Ninja copy the provided pseudocode down as a reference.
  26. 5 Have the Ninja write the pseudocode for pressing the down and right arrows
  27. based on the pseudocode provided in steps 3 and 4.
  28. 6 Make sure the Ninja is in the scene's Update Every Frame event.
  29. if() {
  30. }
  31. 7 Make sure the Ninja places the function call inside the if statement's
  32. parentheses. Ask them to explain how the GDP uses the isKeyPressed function to
  33. communicate when a key is being pressed by the user. Ask them to explain that
  34. the Keys object is an easy way to ask the isKeyPressed() function about a
  35. specific key on the keyboard.
  36. if(isKeyPressed(Keys.upArrow)) {
  37. }
  38. 8 Make sure the Ninja types the new line of code inside the body of the if
  39. statement. The name of the GDP object is ninja and we want to set its x position
  40. to exactly 400.
  41. if(isKeyPressed(Keys.upArrow)) {
  42. ninja.x(400);
  43. }
  44. 9 The Ninja should play the game and investigate what happens when the up arrow
  45. is pressed. The ninja object will go to 400 on the x axis, so it will be stuck
  46. in the hole.
  47. 10 Make sure the Ninja types the new line of code inside the body of the if
  48. statement and sets they value to exactly 240.
  49. if(isKeyPressed(Keys.upArrow)) {
  50. ninja.x(400);
  51. ninja.y(240);
  52. }
  53. 11 Have the Ninja play the game to see what happens when the up key is pressed.
  54. The Ninja should move to the top conveyer belt, but it will not be able to move
  55. somewhere else.
  56. 12 Make sure the Ninja writes the new if statement after the first if statement.
  57. Make sure they correctly place the isKeyPressed function and the Keys.leftArrow
  58. object.
  59. if(isKeyPressed(Keys.leftArrow)) {
  60. }
  61. 13 Make sure the Ninja sets the x value to exactly 340.
  62. if(isKeyPressed(Keys.leftArrow)) {
  63. ninja.x(340);
  64. }
  65. 14 Make sure the Ninja types the new line of code inside the body of the if statement and sets they value to
  66. exactly 300.
  67. if(isKeyPressed(Keys.leftArrow)) {
  68. ninja.x(340);
  69. ninja.y(300);
  70. }
  71. 15 The ninja can now move to the top and to the left conveyer belts.
  72. 16 Help the Ninja write a new if statement based off their pseudocode statement
  73. from step 5. They should be able to use the first two if statements to help them.
  74. This code is explicitly provided in the manual in step 18.
  75. if(isKeyPressed(Keys.downArrow)) {
  76. }
  77. 17 Help the Ninja write the code that sets the ninja's position to exactly
  78. 400, 360.
  79. if(isKeyPressed(Keys.downArrow)) {
  80. ninja.x(400);
  81. ninja.y(360);
  82. }
  83. 18 Verify that the Ninja's code matches and that the ninja in the game moves to
  84. the correct place when the down arrow is pressed.
  85. 19 Help the Ninja write the final piece of movement code.
  86. if(isKeyPressed(Keys.rightArrow)) {
  87. ninja.x(460);
  88. ninja.y(300);
  89. }
  90. 20 The game's movement code is now complete. The ninja can collect the food, but
  91. the score is not updating.
  92. 21 Help the Ninja write the final if statement. Explain that the ninja object
  93. knows a function called ateFood() that will return true when the ninja collects
  94. a piece of food off a conveyer belt.
  95. if(ninja.ateFood()) {
  96. }
  97. 22 The ninja object keeps track of the game's score through the ninja.score
  98. property. This line of code takes the current score, adds one, and updates the
  99. value.
  100. if(ninja.ateFood()) {
  101. ninja.score += 1;
  102. }
  103. 23 The Ninja should notice that the game now gets harder, but the score number
  104. in the UI does not update.
  105. Explain that we are keeping track of the score, but not yet displaying it to the
  106. user.
  107. 24 Explain that the GDP uses labels to display text to the user. In this game,
  108. we need to set the scorelabel's text by passing the score to the text function.
  109. if(ninja.ateFood()) {
  110. ninja.score += 1;
  111. scoreLabel.text(ninja.score);
  112. }
  113. 25 Before the Ninja submits the game, verify that the score is updating.
  114. */
  115. if(isKeyPressed(Keys.upArrow)) {
  116. ninja.x(400);
  117. ninja.y(240);
  118. }
  119. if(isKeyPressed(Keys.leftArrow)) {
  120. ninja.x(340);
  121. ninja.y(300);
  122. }
  123. if(isKeyPressed(Keys.downArrow)) {
  124. ninja.x(400);
  125. ninja.y(360);
  126. }
  127. if(isKeyPressed(Keys.rightArrow)) {
  128. ninja.x(460);
  129. ninja.y(300);
  130. }
  131. if(ninja.ateFood()) {
  132. ninja.score += 1;
  133. scoreLabel.text(ninja.score);
  134. }
  135. /**
  136. Sensei Stops
  137. 5 Read over your pseudocode with a Sensei. Talk obout whot you need to change to
  138. convert it into real JavaScript code.
  139. The Ninja should be able to explain how to convert the pseudocode into
  140. JavaScript code. If they are struggling, you can use Coconut Chaos as a
  141. reference point.
  142. 10 Before you play your game, tell a Sensei what you think will happen to your
  143. ninja. Using the words horizontal, vertical, and coordinate, explain the
  144. difference between the ninja's x and y values.
  145. Answers will vary, but Ninjas should be able to pair horizontal with x and
  146. vertical with y. They should know that x starts at O on the left and increases
  147. in value to the right. They should know that y starts at O at the top and
  148. increases up in value down.
  149. 15 Tell a Sensei what code you still need to write to make your ninja move to
  150. the bottom and the right conveyer belts.
  151. The Ninja should be able to explain that they need two more if statements that
  152. respond to the down and right arrows and move the ninja to the appropriate
  153. location.
  154. 20 Tell a Sensei how you planned, coded, ond tested moving the ninja down ond
  155. right. What parts were easy? What parts were hard?
  156. Answers will vary. This is a time for the Ninja to reflect on the process of
  157. creating pseudocode, programming, and testing.
  158. 25 Explain the difference between ninja.score and scoreLabel to a Sensei.
  159. Why do we need both?
  160. We are keeping track of the value of score with ninja.score and we display the
  161. score in the scorelabel GDP object. Explain that the ninja is keeping track of
  162. how many pieces of food they ate, and they need to tell the user since we can't
  163. read the ninja's mind.
  164. */