By the end of this task I should know how to... 1) import graphics (background and sprites) into Scratch 2) make sprites move around the stage 3) create a scoring system using a variable.
Creating Computer Games All computer games are created by programming PCs or Games Consoles with lots and lots of computer instructions. Instructions tell the computer system how to draw backgrounds, make characters move, keep scores etc. Initially games were simple enough to be programmed by one person, often working in their own home. As the complexity of the games grew, companies began employing larger and larger teams of programmers and other specialists. To create a game today, often requires input from more than 100 people. Some of their jobs are listed below: Ÿ Scripting Ÿ Graphic Art Ÿ Surround Sound Processing Ÿ Artificial Intelligence Ÿ Game Engine Coding Ÿ Motion Capture Ÿ Full Motion Video Ÿ Marketing
In this unit you will be using Scratch. Scratch was developed (by the USA’s famous MIT university) to teach school pupils how to program. Your task will be to create a simple shark and fish chasing game in four stages. 1. Changing the background (or stage) 2. Inserting characters (or sprites) 3. Making the characters to move and interact 4. Creating a scoring system. You will then be given the opportunity to develop the game further.
1
The Stage Start up a new file in Scratch. If you can’t find the program ask your teacher where it is. Scratch allows you to add sprites (small graphics like the cat below) to a stage and then control them using instructions called scripts. The Scratch window is split into 4 sections: 1. Blocks Palette Blocks for programming your sprites.
1
3
2. Scripts Area Drag blocks from 1 and snap them together to write scripts.
2
3. Stage This is where what you produce will be shown.
4
4. Sprite List Shows thumbnails of all your sprites.
As we wish to start with a blank stage we start by deleting the cat sprite.
Click on
and then click the scissors icon on the cat
.
The background colour of the stage is currently white. This can be altered using the Paint Editor. Click on the Stage icon
and then select backgrounds and import.
Use the window to navigate to the saved backgrounds and select one.
2
Now click on the edit button to open the Paint Editor window.
Use the grow and shrink buttons to make the background fill the stage.
Try - Use the Paint Editor to add your name to the stage. Try - The game will involve a shark chasing a fish around the stage. Add a suitable name for the game to the top of the stage.
Sprites A sprite is a small graphic that is part of a bigger picture. This means that all characters and objects in games are called sprites. Our game is going to have two sprites -
Shark
Fish
Later you will learn how to make the sprites move around the stage and react when they come in contact with each other.
3
Inserting Sprites To add the shark sprite click on the ‘Choose new sprite from file’ button.
Open the ‘Animals’ folder and select shark1-a.
Repeat the above steps to add either :
Use the Shrink Sprite button to make the fish smaller than the shark.
fish2
fish3
or fish4
You should now see two sprites in the sprite list (1 shark and 1 fish). Each time you want to change how a sprite looks, moves or behaves you must select it from the sprite list. Later on, when our shark catches the fish we will want it to open its mouth. To do this we have to add another costume to the shark sprite. Select the shark sprite
and click on costumes.
Click on the Import button and select shark1-b. You should now see a second costume below the first. Try - Click on the Edit button for each costume and use the Paint Editor to change the colour of your shark to one of your choice. You’ll have to do it for both costumes. Try - Use the Edit button to colour your fish as well.
4
Making Sprites Move (Shark) The next part of creating our game is the fun bit. To create a game we have to give our sprites instructions that tell them how to behave when the game is being played. Instructions in Scratch are called scripts and are created using blocks.
Let’s start with the shark. Select the shark from the sprite list.
Click on the Scripts tab in the Scripts Area.
Scratch blocks are grouped in 8 categories. To add blocks we simply select the appropriate category and drag across the blocks we want to use. Using the and create the script shown below.
categories
to
This starts the script when the Go flag is clicked. This is called a loop. The loop will keep repeating the blocks inside it until the Stop button is clicked The block will make the shark glide to a position(x,y) on the screen. The amount of time it takes to get there is set in seconds. We can get the shark to glide to wherever the mouse is on the screen by adding the and blocks to the glide block. Use the
category to add mouse x and mouse y to the glide block. This now reads as: Take 1 second to glide to the x and y coordinates of where the mouse is pointing.
Click the Go flag to start you game. Move the mouse around the stage and watch how your shark now follows the mouse. Try - Experiment by changing the glide time to values between 0.2 and 5 seconds.
5
Making Sprites Move (Fish) Each sprite has its own scripts so before we start creating a script for the fish we must select the fish from the sprite list. Stage Graph The fish will start in the middle of the stage when the flag is clicked. Note from the graph on the right that the middle of the stage are coordinates (0,0).
To make the fish move create the script below. This moves the fish to the centre of the stage when the Go flag is clicked. The fish turns 15 degrees before it starts to move. The repeat loop can be set to repeat the blocks inside it (move & bounce) 100 times. Note that because we have one loop inside another, the repeat loop will have to finish before the script returns to the top of the forever loop Move is used to make a sprite move across the stage. Bounce makes the sprite change direction when it hits the edge of the stage. Click on the Go flag and watch carefully how the fish moves. Try moving the shark to catch the fish and you will see that it is very easy because we can predict where the fish is going to be next. We can use another block in the Add the
category to improve how our fish behaves.
block to the turn block and enter 1 to 359. The fish will now turn a random number of degrees between 1 and 359 each time the forever loop runs.
Try - Try changing the repeat and move blocks to different values. If the game is to work well, the fish should be difficult but not impossible to catch. Keep trying different values until you are happy with how the fish is moving.
6
Interaction and Scoring The last task is to keep a score of the number of times the shark catches (touches) the fish. To keep score the game must be able to store a number. This is done using a variable.
We will create the variable on the stage so click on the stage icon next to the sprite list. Click on the
category and then
.
Create a variable called score.
Now use the variable to create the script shown below.
When the flag the score will be set to 0. Next we have to add something to the score when the shark catches the fish.
Select the shark from the sprite list.
Use the
and
categories to add the following new blocks to the shark.
An block is used to make decisions. This block can be read as: If the shark is Sprite 2 (the fish) then add 1 to the score variable.
7
Changing Costumes The last task is to animate our shark so that it looks like it trying to bite the fish when it catches it. To do this we need to use the second shark costume we added earlier (on page 4).
We already have an IF block that adds one to your score if the shark sprite touches the fish sprite.
If we want to make other things happen when the fish is caught we add more blocks inside IF block.
Use the
and
categories to add three more blocks to the script.
These blocks change the shark costume, pause for 1 second and then change the costume back. This makes it look like the shark is closing his mouth and opening it again.
You have now finished the basic game. Try it!
Intermediate Game Production You can now choose which of the 7 Intermediate tasks you wish to attempt. Bonus Points Adding Speech Two Players Adding Sound
More Fish Game Over Setting Difficulty
Each task will describe how the game could be improved. You will be expected to think of how you would solve each problem and add more variables, blocks and scripts to the game. A solution will be given on each sheet but you should attempt to solve each problem yourself!
8
By the end of each task I should know how to... Task 1 - make a message appear on the screen when two sprites touch each other. Task 2 - use the pick random and if blocks to make a sprite say random messages.
Adding Speech Scratch contains four blocks that make your sprites talk or think.
Notice that there are two versions of the and blocks. One puts the message up for a set amount of time and the other will put the message up until it is replaced by another message or the sprite is hidden.
By combining a few blocks we can generate random messages. First create a variable to store a number. Set the variable to a random number.
Depending which number is selected a different message appears.
You should attempt all of the tasks below in order. Remember - these tasks are designed to make you solve problems yourself. Do not look at the solutions unless you absolutely have to! Task 1 - Use the say block to make the shark say “Chomp�, for half a second, when he catches the fish. Task 2 - Make the fish say a random message, taunting the shark every time it changes direction. There should be five different messages.
9
Adding Speech (solutions) Task 1 Choosing the correct block in this task is fairly simple. Click on the shark sprite. To get the shark to speak at the right time it’s important to put the block in the correct place.
The should go inside the block so that the block is used only when the shark touches the fish.
Note that the say will cause the shark to stop for half a second so there is no need to have the block as well.
Task 2 First create a new
called Taunts.
Click on the Fish sprite. Then use the and number between 1 and 5.
block to store a
You will now need to create 5 blocks, one for each message.
Finally, add all the blocks under the block where the fish changes direction. Make sure the blocks are below each other and not inside each other!
10
By the end of each task I should know how to... Task 1 - make sprites appear and disappear Task 2 - make sprites disappear to random times Task 3 - make sprites appear randomly in different places on the stage.
Bonus Points So far, all the sprites in our game can be seen for the whole time the game is being played. It is common, in games, for sprites to appear on the screen for short spaces of time and then disappear. These sprites could be things like power-ups or items that characters can pick up. To make sprites appear and disappear in Scratch we use the
Here is example of hiding and showing a sprite.
and
blocks.
The script starts when the green Go flag is clicked.
}
The loop repeats the blocks inside it until the game is stopped. The sprite starts by being hidden, waits 10 seconds, is shown, waits 2 seconds and then disappears when the loop starts again.
To make the sprite appear randomly the block could be used to pick a number between 1 and 10.
You should attempt all of the tasks below in order. Remember - these tasks are designed to make you solve problems yourself. Do not look at the solutions unless you absolutely have to! Task 1 - Use the show, hide and random blocks to make a ‘bowl of Cheesy Puffs’ sprite appear, on the stage, randomly every 1 to 10 seconds. If the shark catches the cheesy puffs in time the player gets a 50 point bonus. Task 2 - Use the random block to make the cheesy puffs disappear at random times. Task 3 - Use a combination of the and blocks to make the cheesy puffs change where they appear on the stage. Note - you will have to think of minimum and maximum x and y numbers.
11
Bonus Points (solutions) Task 1 Add the cheesy puffs sprite to the game (you’ll find it in the ‘Things’ folder). Now create the script for the cheesy puffs shown below.
Add the block to make the cheesy puffs appear at random times.
Finally, add an IF block to the shark to add 50 points on to the score if the shark is touching the cheesy puffs.
Task 2 This is an easy one! Simply add another block to the second wait block.
Task 3 To make the cheesy puffs appear in random places on the stage we need to look the stage’s coordinates. Remember the stage ranges from x = -240 up to 240 and y = -180 up to 180
.
-160,70
We can use this in our cheesy puff script. Add a block with random numbers for x and y. Note that the x and y numbers do not go right to the edge of the stage or some of the cheesy puff sprite would disappear off the edge of the stage.
12
By the end of each task I should know how to... Task 1 - end a game using the broadcast block or the timer.
Game Over Most games will have some way of indicating that the game is finished. This may be a message that flashes up or credits that list all the people involved in producing the game. Before our game can stop, we must have a way of sensing when the game is finished. are a few ways of doing this. Two methods are shown below.
There
Method 1 The game could finish when the player has scored a certain score.
Shark
Stage
The IF block is used to sense when the score reaches 20 or more. Broadcast sends a message out to all the other sprites (and the stage). When other blocks hear the message they can then take some sort of action. This example changes to a different background that has the text “Game Over�.
Method 2 The game could last a fixed amount of time.
The reset timer block starts a clock running. This loop keeps this script from going to the next blocks until the clock reaches 60 seconds..
Stage
When the clock reaches 60 the background is switched and all the scripts are stopped.
You should attempt the task below. Remember - these tasks are designed to make you solve problems yourself. Do not look at the solutions unless you absolutely have to! Task 1 - Combine the information given in method 1 and method 2 to make the game stop when the timer passes 40 seconds or when the score is equal to 10.
13
Game Over (solutions) Task 1 The important part of this task is to be able to untangle the logic behind the problem. Read the problem carefully and you will see that the game is to finish when. ( Timer > 40 ) OR ( Score = 10 )
To build this into your script you need three blocks.
Together like this.
they
look
When the Timer and the Score variable are added we get.
This can be used, as you were shown in Method 2, to make the Stage script loop until one or the other statement is true.
Remember, you will have to Import a second background that the game can switch to when it finishes.
14
By the end of each task I should know how to... Task 1 - add another fish sprite that moves differently Task 2 - add a poisonous starfish that the shark should avoid Task 3 - increase the number of steps a fish sprite moves while the game is being played.
More Fish As your games become more complex you will probably find that you add more and more sprites.
Each sprite can be made to behave differently from the others by simply having different scripts.
It is also possible to change the behaviour of sprites while the game is playing. To do this we have to replace fixed numbers with variables. For example, to change the glide speed when the shark catches the fish, we would create a variable to store the Speed. Other blocks can then be used at other places in your scripts to change what is stored in the Speed variable.
This would add 1 second to the glide speed .
You should attempt at least one of the tasks below. Remember - these tasks are designed to make you solve problems yourself. Do not look at the solutions unless you absolutely have to! Task 1 - Add a second smaller fish that is harder to catch. this fish 5 points should be added to the score.
When the shark touches
Task 2 - Add a starfish sprite to the game. Make the star fish move slowly round the screen. If the shark touches the starfish the player should lose 10 points. Task 3 - Each time the shark touches the original fish (sprite 2), the fish should move slightly faster making it harder to catch.
15
More Fish (solutions) Task 1 First Import another fish sprite. Now create a script to control the second fish (note - to make it harder to catch, the fish moves further (move 5 steps) with each step and turns more often (repeat 20). Finally, add another IF block to the shark to add 5 points to the score when the shark catches the fish (sprite 3).
Task 2 Add the starfish sprite to the game. Create a script to make the starfish move slowly around the stage. Remember the way the sprite moves is controlled by the random, repeat and move numbers. Another IF block is added to the shark script to the score by -10 when the shark touches the starfish.
Task 3 To control the speed of the fish during the game replace the number of steps it moves with a variable. I’ve called my variable Fish Speed. The speed can then be set to 2 at the beginning of the game when the green flag is clicked.
Finally (in the shark script), add a variable block to add 3 to the number of steps the fish moves when it is touched by the shark.
16
By the end of each task I should know how to change the difficulty of the game by ... Task 1 - allowing the player to control the speed of the fish Task 2 - allowing the player to control the speed of the shark Task 3 - allowing the player to control how often the fish turns.
Setting Difficulty To change how difficult the game is, we have to control certain aspects of the game. For example, if the fish moves slower or the shark moves faster it’s easier to catch the fish. To allow the player to change the speed of the fish, during the game, we can use a variable.
S Fish l a n i Orig
cript
Create and add a variable to the move block.
c ish S F w Ne
ript
By adding a button to the game we can give the player a way of changing what is stored in the Fish Moves variable. Add a new button sprite and edit it the add the word “easy” over the middle of it. A script on the button will then change the speed of the fish when the player clicks on the button. You should attempt all the tasks below in order. Remember - these tasks are designed to make you solve problems yourself. Do not look at the solutions unless you absolutely have to! Task 1 - Add two button sprites to the game (one “easy” & one “hard”). Copy and change the above script so that each button makes the fish move at different speeds. Task 2 - Create another variable to control how long the shark takes to glide towards the mouse pointer. Add more blocks to each button to change the speed of the shark when they are pressed. Task 3 - Repeat the above to control how far the fish moves before turning.
17
Setting Difficulty (solutions) Task 1 Create a Fish Moves variable. Create and edit an “easy” button and add the script shown. Repeat these steps for a “hard” button. Remember, to set the Fish Moves to different values in the easy and hard buttons.
Task 2 For every number you want to control with the two buttons another variable has to be created. To control the shark’s gliding speed create a . Add the Glide Speed to the shark’s glide block.
Now add another
block to each button.
Try different glide speeds until you are happy with how easy and hard they make the game. Task 3 Once again create another variable. Add the variable to the fish script. Finally add a third buttons.
block to the easy and hard
Remember to experiment with the values.
18
By the end of each task I should know how to... Task 1 - add a simple sound to a script Task 2 - make a sound play when sprites touch Task 3 - add background music to scratch.
Adding Sound Scratch has several ways of playing/adding sounds to a project.
} } } } }
These blocks allow you to record your own sounds using a microphone or Import a saved sound file. Note that Scratch comes with many pre-saved sounds. Drum beats of different pitches can be added. The speed of the beat can be changed or pauses added. The note blocks allow tunes to be added to your game. Look on the Internet for tunes (eg E D# E D# E D C A). You may want a sudden burst of sound in your game. The volume can be changed using these blocks. The tempo is the speed the sound plays at. Slow it down, speed it up or set it to a specific number of beats per minute (bpm).
Where you add the sound will be important. If you want background music add the blocks to the stage. If you want to add a sound to an event (like two sprites touching) think of where the sound blocks should be attached.
You should attempt at least one of the tasks below. Remember - these tasks are designed to make you solve problems yourself. Do not look at the solutions unless you absolutely have to! Task 1 - Add a sound that is only played when the fish changes direction. Task 2 - Add a sound that is played when the shark catches the fish. Task 3 - Find the musical notes for a well known tune and create a backing track for your game. Sources of piano music could be the Internet, your home or the Music Department.
19
Adding Sound (solutions) Task 1 First Import a sound into the fish sprite.
Add a sound under the turn block to make the sound play after the fish has turned. Note - if the block was placed inside the repeat loop it would play all the time. Try moving the block inside the repeat loop to see why this is wrong.
Task 2 First Import a sound into the shark sprite.
Add the sound inside the shark’s IF - touching block.
Task 3 To add a backing track, add the sound blocks to the stage and place them inside a forever loop so that they keep playing all the time the game is being played. The example plays the well known tune FĎ‹r Elise. Note - if you understand sheet music you could alter the length of each note by changing the length of the beats.
20
By the end of each task I should know how to... Task 1 - control a sprite’s movement using the keyboard Task 2 - control a sprite’s movement using the keyboard and store two players’ scores.
Two Players Our game can be adapted easily for two players in a variety of ways. We could have: Ÿ two sharks chasing the fish Ÿ one player playing the fish and another playing the shark Ÿ one player trying to block the shark from getting the fish and so on.... No matter which game we decide to create, we have to use the keyboard to control the fish or shark as we can only have one mouse.
If you look at the category you will see that we can change the position of a sprite on the screen by changing its x and y coordinate.
If we use these with the block we can move a sprite in one direction when a key is pressed.
y gets larger x gets larger
x gets smaller
y gets smaller
You should attempt one of the tasks below. Remember - these tasks are designed to make you solve problems yourself. Do not look at the solutions unless you absolutely have to! Task 1 - Delete the script that makes the fish move randomly and use the information above to make the 4 cursor (arrow) keys control the fish. Task 2 - Add another shark to the game that is controlled by the 4 cursor (arrow) keys. Add another variable to store player two’s score (like the above example) when the second shark catches the fish.
21
Two Players (solutions) Task 1 In this task, we simply have to change some of the script we created in the Basic Game sheets. Change the fish script as shown below. After Before
Note that four IF blocks are needed (one for each cursor key). To change the speed the fish moves at, simply make the x and y coordinates change by a larger amount when each key is pressed.
Task 2 Start by adding another shark sprite to the Game. of the second shark using the edit button in costumes.
You should change the colour
Delete the original Score variable and make two new variables for each player.
On the stage script set both variables to zero when the green flag is clicked.
Now create the script for the second shark. Note, it must be controlled by the keyboard. Finally, change the variable used to keep score in the first shark’s script.
22