|Cherry||: 1 point|
|Lemon||: 2 points|
|Pear||: 5 points|
|Plum||: 10 points|
|Pineapple||: 20 points|
After one minute, the game ends and the final score is decided based on the number and types of fruits caught. Play to experience it for yourself (the sound quality is horrendous in this online version - you may download and run on your computer for the music notes to play better):
We will program this game in four steps.
Step 1: Do staging and initializationWe have two backgrounds for the stage - one with the game title "Fruit Basket" and another without. This script basically displays the one with the title at the beginning of the program, and only switches to the next background when the player hits the spacebar. Since this game is timed, we also reset the timer before broadcasting a message to start the game.
|Script to change stage background and initialize variables|
We have six variables in this game. We use those named after the fruits to keep track of the number of fruits collected. We will see how to do this in Step 3. In Step 4, we will use the values in these variables to arrive at the final score, which we put in the totalScore variable.
Step 2: Control the basket
|Script to control basket|
Once the 60 seconds is up, it broadcasts "end" to signal that the game has ended. We will make use of this "end" message in Step 4.
Step 3: Program the falling fruitsWe want the fruits to fall in differing speeds and stages. The more points a fruit is worth, the later it will appear in the game, and the faster it will fall. When the player successfully collects a fruit we will also play a note. The instrument that we use for each fruit will also be different.
The Cherry is the first fruit to appear, and it starts falling right immediately when it receives the 'start' signal:
|Script for Cherry|
Then, for the next 60 seconds, it repeatedly:
- Positions itself somewhere at the top of the stage, with the set x to_ and set y to_ blocks.
- Keep falling until either it touches the basket (I use touching color block here, but you could also use the touching sprite block), or if it has reached the bottom of the stage (when y position is less than -170), or if time is up.
- Checks if it touches the basket. If it has, then it adds to the count of cherries collected with the change cherry by 1 block. It also plays the 65th note of the Marimba for 0.01 beats.
The script for Lemon is almost the same as Cherry's. Lemon appears only after 10 seconds into the game, so we insert a wait until (timer > 10) block to ensure this:
|Script for Lemon has the wait until (timer > 10) block|
- instrument used (observed in the set instrument to_ block)
- time to appear (observed in the wait until_ block)
- falling speed (observed in the change y by_ block)
- the variable to update (observed in the change_by_ block)
Step 4: Display final scoreRemember the "end" message that we broadcasted in Step 2? This is where we use it.
|Scoring script adapted from the game show scoring template|
It then proceeds to do a game show flavour of totaling up the scores using the Scratch script template from the game show scoring tutorial. Note that in this game, we increase totalScore by 1, 2, 5, 10 or 20 for each count of cherry, lemon, pear, plum and pineapple respectively.