Zombie Game: Implementation of the Zombie Clones & Message Broadcasting
Code Yourself! An Introduction to Programming
Have you ever wished you knew how to program, but had no idea where to start from? This course will teach you how to program in Scratch, an easy to use visual programming language. More importantly, it will introduce you to the fundamental principles of computing and it will help you think like a software engineer.
Think Like a Software Engineer
In this unit you’ll follow all the steps of software development to create a fun computer game. You’ll also learn about message broadcasting and concurrency.
We'll do this following our code design for the zombie clones.
So we'll introduce a, when I start as a clone block, followed by a show block,
as we specified earlier, that the actual sprite is hidden.
We'll also indicate that the zombie clone points to the left, so
as to move towards the left later on.
And that it is located on the right side of the screen at a random height.
So we'll set x to be 240.
And y to take a random value between -150 and 150.
We'll also modify the zombie's color randomly,
to make it look more cute, say, between 1 and 200.
How does this program continue?
Inside a forever block, we specify that it should move a few steps,
the amount of which is indicated by the speed variable.
Then, we check if it's touching the broom, in which case a sound is played,
the score increases, and the zombie returns.
We'll create a new block called return, which we'll detail later on.
Then, we check if it is touching the flower, in which case,
the zombie halts for a second.
In the case that it reaches the house, that is its x position is a very low
value, that corresponds to the left of the screen, the game is over.
What does it mean that the game is over?
The different game elements will react to the situation accordingly.
For example, when the game is over, the stage
should switch to the game over backdrop and all scripts should stop execution.
But how can we tell all those elements that the game is over?
In Scratch, we can do this with the use of messages,
to which different program elements react accordingly.
Let's explain this through an example from everyday life.
A football referee blows the whistle to signal the opening kickoff.
The signal can be seen as a message that is broadcasted to everyone
in the football stadium, but only particular people will react to it,
among which, the player that kicks the ball first.
Communication between different objects in Scratch works a bit like this.
Scratch lets us broadcast messages and we can specify for different objects
to have a certain behavior upon the receipt of those messages.
This way, we can also coordinate the behavior of these objects.
In our case, we want the zombie clone to communicate with the rest of the program
objects, to let them know that the game is over.
So when the zombie reaches the left side of the screen,
it will broadcast the message which we'll call "game over".
The reaction of other objects upon the receipt of this message
will be taken care of later on.
But first,
we want to specify the body of the return block that is currently pending.
Returning involves moving to the right,
in the same speed that the zombie was moving previously.
So, we'll specify that it points to the right, and then, we'll introduce a repeat
until block, with a condition that the x position is closer to the right.
For example, greater than 220.
Inside this loop, we'll have the zombie move with the same speed.
Once we exit this loop, the clone will be deleted.
It's a good idea to add a comment that clarifies what this procedure does.
In the next video, we'll program the stage and