Wednesday, December 21, 2011

Project: Fruit Basket Mini Game

Recently, I posted a Scratch tutorial on summing or updating scores with sound effects, like in a game show. In today's project I will show how that is applied in a mini time-based game. In this game, players have one minute to catch as many fruits as they can. Each fruit earns the player points:

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 initialization

We 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
All the orange blocks in this script do two things: first, they hide all the variables so that the watchers are hidden. We also set all the variables to zero.

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
When the basket receives the broadcasted message to start the game, it shows itself and for the next 60 seconds, keeps checking whether the left or right arrow keys are pressed. If the right arrow key is pressed it moves 8 steps to the right, and if it is the left arrow key it moves the same distance to the left.

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 fruits

We 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
The Cherry first sets its instrument to #13, which is the Marimba. Next, it shows the variable that counts the number of cherries the player collects (with the show variable cherry block), and appears on stage (with the show block).

Then, for the next 60 seconds, it repeatedly:
  1. Positions itself somewhere at the top of the stage, with the set x to_ and set y to_ blocks.
  2. 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.
  3. 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.
When the minute is up, it disappears from stage with the hide block.

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
All the fruits - Pear, Plum and Pineapple - have the same script as the lemon. They are different in terms of:
  • 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 score

Remember the "end" message that we broadcasted in Step 2? This is where we use it.

Scoring script adapted from the game show scoring template
The Score sprite detects that the minute is up and that the game has ended once it receives the broadcasted "end" message. To this, it shows its first costume, which says "Time's Up!". It waits two seconds, to be sure that the player reads that, and switches to the next costume, which says "Your total score is:"

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.


  1. what do i do ????????????????????????????

  2. ma frends name is juliana xD

  3. i love the fruit game :D x

  4. Good game to get started with. I think there needs to be more levels to make it better, and I will try and make some more. But thanks for making the game, it helps me to start my project off.

    1. Glad you find it helpful. Drop a link here on your project when done, whaddya say? Would be awesome to see it!

  5. I did this with just 2 object falling. One works just fine, but for some reason if I miss the 2nd, it stops reappearing. Do you know what might be wrong?

    1. It is hard to say what might be wrong without looking at your scripts. If you can upload it to MIT Scratch and alert me, I can help take a look. In any case, here are a couple of things that you may want to check:

      1) Are the "hide" and "show" blocks for the 2nd object in the right places?
      2) Are the conditions for the "repeat until" blocks correct?