Posted on

Scratch Coding – Fruit Slicer

We are going to make a game called fruit slicer, you might have played a similar fruit cutting game. The idea is that fruit is thrown into the air and you must cut it with your sharp mouse cursor. The player must be very careful because fruit doesn’t always get thrown into the air, we also throw objects that could break our sharp mouse cursor.

Sprites

I have selected a few fruit sprites that we can slice up, I’ve used a lump of rocks as the object to avoid.

We also need to create a sprite costume for the fruit in its sliced state.

To do this I changed the sprite from a vector image to a bitmap by selecting the convert to bitmap button:

Then I could use the select tool to select half of the object and move it away from the other half.

Backdrops

We are going to have two backdrops, one is the normal game play backdrop, the second is the one that is shown when the game is lost:

I put the words you lost on mine.

Flying Fruit

The first part of our game is to get the fruit flying into the air. Let’s select our apple fruit to start with.

We are going to have the apples fly up when our game starts, so we start with our green flag hat block. We also want our apple hidden until it is ready to be shown on the screen.

We want our apple to fly across the screen with some up and down motion. We are going to need the computer to remember the across and up down speeds.

For this we are going to use two variables: UpDownSpeed and AcrossSpeed.

Let’s create the variables:

We need to make sure that the ‘For this sprite only’ checkbox is selected on all of our variables. Otherwise all of our sprites will rise and fall at the same time.

We setup our code so that we set our UpDownSpeed and AcrossSpeed to some random numbers. Experiment later to find some good numbers.

We then create a clone of our sprite which we will animate across the screen.

We find a random location on the screen and then we use the Go to X Y block to put the sprite in its place. We then show the sprite.

We want the sprite to be alive until it hits the bottom of the screen, so we can create a test that will keep our sprite moving until it hits the bottom.

When our sprite hits the Y position of less than -170 we know it has gone close to the bottom of the screen.

To make our sprite move:

We change the UpDownSpeed by -1 each time the loop runs. This number starts as a large positive number and slowly becomes a negative number which makes it fall. We also add a little rotation to make it look like the object is truly flying.

Test this out, we should see some flying apples.

Slice that fruit

Now that our fruit is flying, we can add a test to see if the fruit is touched by our mouse cursor.

We have two options for our game, we can either make it so that the fruit gets sliced when the mouse is down, or just when the mouse moves over the fruit.

To test for the mouse button being pressed:

To test for just the mouse touching the sprite:

What do you think works better?

Keeping Score

We need to create a couple of variables to keep track of how many items have been hit and missed.

My variables are called ‘Hits’ and ‘Misses’, these are created with the ‘for all sprites’ option set.

Now we can add our scoring:

We add a short delay before we remove the sprite from the screen.

The miss needs to be add the end of our code block:

Now we can copy these scripts over to our other sprites, be sure to change the costume references in each sprite, when we copy them over, they won’t be set correctly.

The Hard Rock

The rock needs a few changes to make our game complete:

We need to switch to our ‘you lost’ backdrop when the rock is hit, then we stop the game. We also need to remove the costume changes.