2. Other Projects‎ > ‎Scratch‎ > ‎

Pumpkin Invaders

posted 18 Oct 2015, 04:37 by Andy Lakin-Hall   [ updated 4 Dec 2016, 13:25 ]
As it's that time of year, I thought I'd write about making a pumpkin themed game - Pumpkin Invaders!

Lets start with drawing a pumpkin. This is really easy in Scratch. I'll be describing Scratch 1.4 for this game, but it should also work in a similar way in Scratch 2

Start Scratch 1.4 and delete the cat sprite.

Draw a Pumpkin

Choose Paint new sprite .

Choose the Ellipse Outline and draw a pair of overlapping brown circles .

Fill these with orange 

Add a little stalk 

Make a COPY of this costume, just in case you make a mistake.

Off to the side of the pumpkin, draw an eye 

Use the Stamp tool to make a copy of the eye 

Use the selection tool and the Flip Horizontal tool   to flip the right eye over 

Use the selection tool to move the eyes into position 

Now make a COPY of this costume before you draw on a mouth. You need to have two pumpkin costumes; one with a zig-zaggy mouth, one with a spit mouth.


Now you are done drawing these two costumes, you need to ROTATE both costume 90° to the right, so the stalk aligns with the blue sprite direction line, which defaults to point right. If you forget to do this step, later when you try to get the pumpkin to point towards the mouse pointer it will be 90° off.

In Costume Edit, click the Rotate Clockwise control three times to rotate both the pumpkin costumes 90° to the right.

You could have tried drawing the pumpkin pointing to the right in the first place, but I found that very hard to get right, and it was easier to draw it upright and then rotate it.

Name the sprite pumpkin and the costumes, blank, pumpkin and pumpkin spit.

Draw a seed

Your pumpkin needs a seed to fire. Choose Paint new sprite  and draw a small ellipse . Draw it sideways, so it aligns to the blue sprite direction line. Name the sprite seed.

Pumpkin Basic Script

The following script for the pumpkin makes it always point towards the mouse pointer. It also handles the seed firing, switching pumpkin costumes and broadcasting the command spit seed.

Seed Script

Here are the scripts for the seed. The seed is hidden at the start. When it receives the broadcast spit seed from the pumpkin, the sound effect is triggered, then the seed sprite goes to the pumpkin and points in the same direction. Then the seed is shown and it starts moving forward until it touches the edge of the screen. At the edge of the screen, the seed hides once again.

The third script handles what happens after a seed hits a ghost (we haven't made them yet). After hitting a ghost, we need the seed to vanish. But we don't want to do that until after the ghost has detected the sprite collision, so this waits until the hit ghost broadcasts that it has been hit, and then hides the seed.

The Ghost

Click Choose A New Sprite from File  and inside the Fantasy folder find the ghost2-a sprite 

Name the sprite ghost1 and name this first costume Happy Ghost.

Copy the happy ghost costume and change the name of the copy to Sad Ghost. Edit it this costume and paint over the smiling mouth, replacing it what a sad face.

Make a variable for this sprite only called speed, and then build this script for movement.

Ghost sprite collision routine

The Ghost sprite collision routine is quite complicated, but bear with me and I'll try and explain all the steps.

First set the ghost to be hidden.

Next we set the ghost's appearance; the fisheye effect is set to 0, the costume is switched to Happy Ghost and the ghost effect is set to 50, to make the ghost appear ghostly and transparent.

The variable speed is set to a random pick between 2 and 5.

The ghost's Y-coordinate is set to a random pick between 0 and 150.

The ghosts's size is set to a random number between 25 and 50%

The ghost plays the ghostly moan sound effect.

The script waits until the ghost sprite is touching a seed sprite.

Set the speed to zero, to stop the ghost sprite moving.

Switch the costume to sad ghost. The ghost is sad, because it's been hit by a seed.

Broadcast hit ghost. This will tell the seed it can disappear now.

Wait a second.

Then follow the repeat 10 loop, changing the ghost and fisheye effects to make the ghost appear to squish up and fade away.

After the loop, set the ghost sprite to hide.

Now the ghost is vanished, wait a random pick number of between 2 and 3 seconds, and we get to the end of the loop and go around again.

Add a background

Click the stage and change import the background from the nature folder called woods.

Test the game

The game can be run like this. Just duplicate a few extra ghosts and click the green flag for some ghost shooting action.

Add Score

We need to add a scoring system next. While on the stage, add a variable called score.

Add the following script

Because we've already using that hit ghost broadcast every time a ghost is hit, we can use it to detect that event and increment the score.

We will need to reset the score to zero at the start of every game, and you can do that by adding a When Green Flag Clicked, Set score to 0 script.

Game Timer

The game needs a timer, so create a variable called Time and build this script;

This starts by setting the score to zero, then setting the Time variable to 30.

The repeat 30 loop counts down thirty repeats of Wait 1 second, and then does Stop All, which will stop everything.

The Countdown Spider

Rather than use an orange variable number to show the game time countdown, I want to use a graphic - a countdown spider.

This spider has been drawn in Scratch, mainly with the ellipse tool. The legs are straight lines.

When the spider is complete, make a copy.

Use the Text Tool to position a number on the spider's tummy. Start from 10, then copy that costume, and then edit the text to say 9, and so on, down to zero. In all you'll need twelve spider costumes. The first blank. The second with 10 and then counting down to zero.

Here's the script for the Counting Spider;

The first script handles the position and size of the spider, and sets up the blank costume for the start of the game.

The second script starts when the Start Counting broadcast is made. We haven't made that yet - don't worry.

The repeat loop makes the spider jiggle and then switch to the next sequential costume. The two delays; Wait 1.5 seconds, mean that over ten repeats the script will take a total of thirty seconds, which is the same duration as the game timer on the stage.

Front End

In the final step we want the game to run over and over again, without having to click the green flag. For this we need to make a front end.

Using the Nature - Woods background as a starting point I have created this background image to display on the stage when the game begins;

ghostly moan.mp3
Andy Lakin-Hall,
18 Oct 2015, 12:07
pumpkin invaders.sb
Andy Lakin-Hall,
6 Dec 2016, 05:55
Andy Lakin-Hall,
18 Oct 2015, 12:05