/** Ninjas use conditionals and if statements to keep score and move an object when a key is pressed. Reinforced Vocabulary and Concepts * Comparison Operators * Functions and Parameters * GDP isKeyPressed() Function * GDP Keys Object * GPD .x() and .y() Functions * if() Statements * Object Names * Pseudocode Sensei Notes * This game introduces the use of the x and y functions to set the position of an object. Previously they were used to get the position of an object. * This game will give the Ninja the last pieces of knowledge they need to complete the final activity in the White Belt. * The Ninja will need to write the code to keep score. * This is the most code that a Ninja has written in a single game, but it is a series of similar if statements. Solution Steps 1 Make sure the Ninja opens the correct scene. 2 The Ninja should play the game to see what functionality needs to be added. 3 Have the Ninja copy the provided pseudocode down as a reference. 4 Have the Ninja copy the provided pseudocode down as a reference. 5 Have the Ninja write the pseudocode for pressing the down and right arrows based on the pseudocode provided in steps 3 and 4. 6 Make sure the Ninja is in the scene's Update Every Frame event. if() { } 7 Make sure the Ninja places the function call inside the if statement's parentheses. Ask them to explain how the GDP uses the isKeyPressed function to communicate when a key is being pressed by the user. Ask them to explain that the Keys object is an easy way to ask the isKeyPressed() function about a specific key on the keyboard. if(isKeyPressed(Keys.upArrow)) { } 8 Make sure the Ninja types the new line of code inside the body of the if statement. The name of the GDP object is ninja and we want to set its x position to exactly 400. if(isKeyPressed(Keys.upArrow)) { ninja.x(400); } 9 The Ninja should play the game and investigate what happens when the up arrow is pressed. The ninja object will go to 400 on the x axis, so it will be stuck in the hole. 10 Make sure the Ninja types the new line of code inside the body of the if statement and sets they value to exactly 240. if(isKeyPressed(Keys.upArrow)) { ninja.x(400); ninja.y(240); } 11 Have the Ninja play the game to see what happens when the up key is pressed. The Ninja should move to the top conveyer belt, but it will not be able to move somewhere else. 12 Make sure the Ninja writes the new if statement after the first if statement. Make sure they correctly place the isKeyPressed function and the Keys.leftArrow object. if(isKeyPressed(Keys.leftArrow)) { } 13 Make sure the Ninja sets the x value to exactly 340. if(isKeyPressed(Keys.leftArrow)) { ninja.x(340); } 14 Make sure the Ninja types the new line of code inside the body of the if statement and sets they value to exactly 300. if(isKeyPressed(Keys.leftArrow)) { ninja.x(340); ninja.y(300); } 15 The ninja can now move to the top and to the left conveyer belts. 16 Help the Ninja write a new if statement based off their pseudocode statement from step 5. They should be able to use the first two if statements to help them. This code is explicitly provided in the manual in step 18. if(isKeyPressed(Keys.downArrow)) { } 17 Help the Ninja write the code that sets the ninja's position to exactly 400, 360. if(isKeyPressed(Keys.downArrow)) { ninja.x(400); ninja.y(360); } 18 Verify that the Ninja's code matches and that the ninja in the game moves to the correct place when the down arrow is pressed. 19 Help the Ninja write the final piece of movement code. if(isKeyPressed(Keys.rightArrow)) { ninja.x(460); ninja.y(300); } 20 The game's movement code is now complete. The ninja can collect the food, but the score is not updating. 21 Help the Ninja write the final if statement. Explain that the ninja object knows a function called ateFood() that will return true when the ninja collects a piece of food off a conveyer belt. if(ninja.ateFood()) { } 22 The ninja object keeps track of the game's score through the ninja.score property. This line of code takes the current score, adds one, and updates the value. if(ninja.ateFood()) { ninja.score += 1; } 23 The Ninja should notice that the game now gets harder, but the score number in the UI does not update. Explain that we are keeping track of the score, but not yet displaying it to the user. 24 Explain that the GDP uses labels to display text to the user. In this game, we need to set the scorelabel's text by passing the score to the text function. if(ninja.ateFood()) { ninja.score += 1; scoreLabel.text(ninja.score); } 25 Before the Ninja submits the game, verify that the score is updating. */ if(isKeyPressed(Keys.upArrow)) { ninja.x(400); ninja.y(240); } if(isKeyPressed(Keys.leftArrow)) { ninja.x(340); ninja.y(300); } if(isKeyPressed(Keys.downArrow)) { ninja.x(400); ninja.y(360); } if(isKeyPressed(Keys.rightArrow)) { ninja.x(460); ninja.y(300); } if(ninja.ateFood()) { ninja.score += 1; scoreLabel.text(ninja.score); } /** Sensei Stops 5 Read over your pseudocode with a Sensei. Talk obout whot you need to change to convert it into real JavaScript code. The Ninja should be able to explain how to convert the pseudocode into JavaScript code. If they are struggling, you can use Coconut Chaos as a reference point. 10 Before you play your game, tell a Sensei what you think will happen to your ninja. Using the words horizontal, vertical, and coordinate, explain the difference between the ninja's x and y values. Answers will vary, but Ninjas should be able to pair horizontal with x and vertical with y. They should know that x starts at O on the left and increases in value to the right. They should know that y starts at O at the top and increases up in value down. 15 Tell a Sensei what code you still need to write to make your ninja move to the bottom and the right conveyer belts. The Ninja should be able to explain that they need two more if statements that respond to the down and right arrows and move the ninja to the appropriate location. 20 Tell a Sensei how you planned, coded, ond tested moving the ninja down ond right. What parts were easy? What parts were hard? Answers will vary. This is a time for the Ninja to reflect on the process of creating pseudocode, programming, and testing. 25 Explain the difference between ninja.score and scoreLabel to a Sensei. Why do we need both? We are keeping track of the value of score with ninja.score and we display the score in the scorelabel GDP object. Explain that the ninja is keeping track of how many pieces of food they ate, and they need to tell the user since we can't read the ninja's mind. */