1.4 Platform Game in Scratch: Introduction To Coding
1.4 Platform Game in Scratch: Introduction To Coding
You can have a look at our project Evil Daddy now. You’ll find it on the Nerdsville Scratch page or follow this
link. Have a go – try to sneak the boy past Evil Daddy and get to the TV remote control before Daddy catches
him and makes him do his homework.
http://scratch.mit.edu/projects/26107515/
After playing the game look inside to see the code. You’ll see that Evil Daddy has one backdrop and four
sprites.
When you are ready to create your own version Sign in to Scratch and click on Create. Make sure you give your
new project a name.
BACKDROP
The backdrop for Evil Daddy is based on photographs of rooms in my house – you can create your own
backdrop based on an image from the Scratch library, your own painted backdrop or an uploaded image.
Notice the fluorescent green borders, floors and stairs – these were added using the Scratch paint editor. The
scripts in this project rely on the detection of collisions between the sprites and this green colour.
The Evil Daddy backdrop –The borders, floors and stairs are all painted the
same fluorescent green colour. This makes it easy to create code that detects
when our sprites are touching these objects.
The Boy sprite is from the library of Scratch 1.4 (an older version of Scratch).
Create your main character sprite – paint it, choose from the library or upload a file
Now create your character’s main script. Here’s the Boy sprite main script…
Look carefully at the script. Make sure you know what each block
does.
During the initial setup we create three variables: Boy x, Boy y and
Lives. These variables are used to set Boy’s x and y coordinates for
the start of the game and keep count of how many lives he has left.
Lives is initially set to 3.
This section simulates gravity – every time the forever loop repeats,
Boy y is changed by -1 – making him ‘fall’ down the screen. However
if he’s touching the green floor, Boy y is increased by 1 so he doesn’t
fall through floors.
Two if statements are used to detect key presses and change the Boy
x variables accordingly.
(We could have used the change x by_ and change y by_ blocks instead of creating variables for Boy’s
coordinates, however it’s good for you to know that there are often different ways of achieving the same
result. As long as your code works and isn’t buggy then any method is fine!)
Create a script to handle what happens to the main character sprite when Gotcha! is broadcast…
In Boy’s main script the Gotcha! message is broadcast when he touches the Daddy sprite. This small script
makes the boy spin around when the message is received.
The when I receive_ block is used to create a new script that will run when a
particular message is received. The drop-down box allows you to choose the
message. In this script the attached blocks cause the boy to spin, then point in
direction 90 again.
There is a second when I receive Gotcha! script for the Daddy sprite that plays the “Do your homework!”
sound and handles what happens to the Lives variable and what happens when Lives = 0.
You can create a number of scripts for different sprites that will all run at the same time when a message is
broadcast. The broadcast_ and wait block can be used to trigger a number of other scripts, then wait until
they have finished before continuing.
The Red Hang Gang theme plays when the boy succeeds in turning on the TV –
along with the show’s image sprite appearing on the TV screen. Both of these
events are triggered by the Win message being broadcast.
The Daddy sprite is from the library of Scratch 1.4 (an older version of Scratch).
Create your ‘baddie’ sprite – paint it, choose from the library or upload a file
Now create the sprite’s main script. Here’s the Daddy sprite script…
After setting the size, start position and direction of the
sprite a forever loop is set up that contains Daddy’s
movement script…
Daddy’s second script is triggered by the broadcast of the Gotcha! message when the Boy sprite touches the
Daddy sprite (see the Boy sprite main script above).
Create a script handles what happens to the ‘baddie’ sprite when Gotcha! is broadcast…
On receiving the Gotcha! broadcast, this script plays the Homework
sound (‘Do your homework!’ – recorded by me doing a funny voice
close to the computer’s microphone) and changes the Lives variable by -
1.
The TV screen sprite has two small scripts. When you design your own game you can choose to end it however
you like – perhaps choose your own sound or tune and create a ‘Game Over’ message sprite or make your
sprites spin around or disappear into the distance.
Create scripts that handle what happens when it’s ‘Game Over’…
The first small script sets the position of my TV
screen sprite and then hides it when the game
starts.
The remote control sprite simply sits there waiting to be touched by the Boy
sprite in order to win the game (the code for this is in Boy’s main script).
http://scratch.mit.edu/projects/23593836/
It’s not a complete game, but simply a basic template for a platform game with jumping. We’ve used the
Nerdsville logo, Cubit as our only sprite and drawn a background with a floor and platforms of the same orange
colour.
This is the main script – it should all look quite familiar and is similar
to Boy’s main script in Evil Daddy.
After setting size and initial position, a forever loop sets up gravity
and monitors for the left and right arrows being pressed to change
the sprite’s horizontal position.
This second script uses event handling to wait for the space bar to be
pressed and trigger a script that makes Cubit jump. Event handling is
commonly used in computer languages where code needs to respond
to a certain event – like a key press or a mouse click.
When space is pressed the variable jump is given the value 14, then a
loop is set up in which the sprite’s y value is changed by jump, and
jump’s value decreased by 1. This has the effect of making Cubit
jump, decelerate and then accelerate downwards again.
You should now be able to experiment with creating your own platform game with a jumping main character
sprite and ‘baddies’ that move randomly. Have fun!
You should now be quite good at reading somebody else’s code and figuring out how it
works – it’s a handy skill and you’ve had a lot of practice
How to include gravity in your games and make a character jump
The broadcast blocks are very useful for coordinating and synchronising sprites
Event handling allows you to write scripts that run in response to particular events
Now might be a good time to explore some of the six-million plus projects that other Scratch users
have shared – you can look inside and see how their projects work - this is a great way to learn.
Make sure you ask your teacher for permission if you’d like to do this in class.
Playing Scratch games might be fun, but make sure you also figure out how the code works!
You can remix projects or ‘borrow’ scripts and graphics from them using your backpack.
Make sure you include credits on the project page if you use other people’s material.
QUIZ