From the menu in the ICE Code Editor, select Make a Copy and enter Fruit Hunt!
as the new project name.
To keep score in this game, we’ll need something new—a scoreboard. There
is a fairly nice scoreboard built into the ICE Code Editor, but we have to load
the library first. In Chapter 9, What's All That Other Code?, on page 85, we
looked at the libraries that are loaded with the <script> tags at the very top of
the code. We need to add another one.
Similarly, to make sounds in our game, we need the sounds library. So let’s
add a second <script> tag at the top of the code.
We make these changes by starting a new line after the three <script> tags at
the top of the page with src attributes. The new line should be on line 5. Add
the following <script> tags to pull in the scoreboard and sound libraries:
<scriptsrc="http://gamingJS.com/Scoreboard.js"></script>
<scriptsrc="http://gamingJS.com/Sounds.js"></script>
Since this is just the “getting started” section of our program, those lines
won’t actually change anything in the game. To see the scoreboard, we need
to configure it and turn it on. Let’s do that next.
11.2 Starting a Scoreboard at Zero
The rest of the code in this chapter will go below the START CODING ON THE NEXT
LINE line.
To add a scoreboard to our game, we create a new one using the new keyword.
Then we tell the scoreboard to start a countdown timer, show the score, and
add a help message. To do all of that, we first enter the following code after
the line that introduces the not_allowed variable:
varscoreboard =newScoreboard();
scoreboard.countdown(45);
scoreboard.score();
scoreboard.help(
'Arrow keys to move. '+
'Space bar to jump for fruit. '+
'Watch for shaking trees with fruit.'+
'Get near the tree and jump before the fruit is gone!'
);
These lines add a nifty-looking scoreboard to our screen, complete with the
time remaining in the game (45 seconds), the current score (zero), and a hint
to players that they can press the question mark (?) key to get some help.
The scoreboard should look like the following:
Chapter 11. Project: Fruit Hunt • 100
Prepared exclusively for Michael Powell report erratum • discuss