Scratch Games

23

description

Scratch Games

Transcript of Scratch Games

Page 1: Scratch Games
Page 2: Scratch Games

1

By the end of this task I should know how to...1) import graphics (background and sprites) into Scratch2) make sprites move around the stage3) create a scoring system using a variable.

Creating Computer GamesAll computer games are created by programming PCs or Games Consoles with lots and lots ofcomputer instructions. Instructions tell the computer system how to draw backgrounds, makecharacters move, keep scores etc.

Initially games were simple enough to be programmed by one person, often working in their ownhome. As the complexity of the games grew, companies began employing larger and largerteams of programmers and other specialists. To create a game today, often requires inputfrom 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 MITuniversity) 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 developthe game further.

Page 3: Scratch Games

2

The StageStart 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 controlthem using instructions called scripts.

The Scratch window is split into 4 sections:

1. Blocks PaletteBlocks for programmingyour sprites.

2. Scripts AreaDrag blocks from 1 andsnap them together towrite scripts.

3. StageThis is where what youproduce will be shown.

4. Sprite ListShows thumbnails of allyour 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 PaintEditor.

Click on the Stage icon and then select backgrounds and import.

Use the window to navigate to the saved backgrounds and select one.

12

3

4

Page 4: Scratch Games

3

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 namefor the game to the top of the stage.

SpritesA sprite is a small graphic that is part of a bigger picture. This means that all characters andobjects 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 comein contact with each other.

Page 5: Scratch Games

4

Inserting Sprites

To add the shark sprite click on the ‘Choose new sprite from file’ button.

Open the ‘Animals’ folder andselect shark1-a.

Repeat the above steps to add either :

Use the Shrink Sprite button to makethe fish smaller than the shark.

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 thesprite list.

Later on, when our shark catches the fish we will want it to open its mouth. To do this we haveto add another costume to the shark sprite.

Select the shark sprite and click on costumes.

Click on the Import button andselect shark1-b.

You should now see a secondcostume below the first.

Try - Click on the Edit button for each costume and use the Paint Editor to change thecolour 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.

fish2 fish3 or fish4

Page 6: Scratch Games

5

Making Sprites Move (Shark)The next part of creating our game is the fun bit. To create a game we have to give our spritesinstructions 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 addblocks we simply select the appropriate category anddrag across the blocks we want to use.

Using the and categories tocreate the script shown below.

We can get the shark to glide to wherever the mouse is on the screen by adding theand blocks to the glide block.

Use the category to add mouse x and mouse y to the glide block.

Click the Go flag to start you game. Move the mouse around the stage and watch howyour shark now follows the mouse.

Try - Experiment by changing the glide time to values between 0.2 and 5 seconds.

This starts the script when the Go flag is clicked.

This is called a loop. The loop will keeprepeating the blocks inside it until the Stopbutton is clicked

The block will make the shark glide to aposition(x,y) on the screen. The amount of timeit takes to get there is set in seconds.

This now reads as:

Take 1 second to glide to the x and ycoordinates of where the mouse ispointing.

Page 7: Scratch Games

6

Making Sprites Move (Fish)Each sprite has its own scripts so before we start creating a script for the fish wemust select the fish from the sprite list.

The fish will start in the middle of the stagewhen the flag is clicked. Note from the graphon the right that the middle of the stage arecoordinates (0,0).

To make the fish move create the script below.

Click on the Go flag and watch carefully how the fish moves. Try moving the shark to catchthe fish and you will see that it is very easy because we can predict where the fish is going tobe next.

We can use another block in the category to improve how our fish behaves.

Add the block to the turn block and enter 1 to 359.

Try - Try changing the repeat and move blocks to different values. If the game is towork 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.

This moves the fish to the centre of the stage whenthe Go flag is clicked.

The fish turns 15 degrees before it starts to move.

The repeat loop can be set to repeat the blocks insideit (move & bounce) 100 times. Note that because wehave one loop inside another, the repeat loop will haveto finish before the script returns to the top of theforever loop

Move is used to make a sprite move across the stage.

Bounce makes the sprite change direction when it hitsthe edge of the stage.

Stage Graph

The fish will now turn a random numberof degrees between 1 and 359 each timethe forever loop runs.

Page 8: Scratch Games

7

Interaction and ScoringThe 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 makedecisions.

This block can be read as:

If the shark is Sprite 2(the fish) then add 1 to the scorevariable.

Page 9: Scratch Games

8

Changing CostumesThe last task is to animate our shark so that it looks like it trying tobite the fish when it catches it. To do this we need to use thesecond 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 fishsprite.

If we want to make other things happen when the fish iscaught we add more blocks inside IF block.

Use the and categories to add three more blocks to the script.

You have now finished the basic game. Try it!

Intermediate Game ProductionYou can now choose which of the 7 Intermediate tasks you wish to attempt.

Bonus Points More FishAdding Speech Game OverTwo Players Setting DifficultyAdding Sound

Each task will describe how the game could be improved. You will be expected to think of howyou 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!

These blocks change the sharkcostume, pause for 1 second andthen change the costume back.

This makes it look like the shark isclosing his mouth and opening itagain.

Page 10: Scratch Games

9

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 SpeechScratch contains four blocks that make yoursprites talk or think.

Notice that there are two versions of the and blocks. One puts the message up fora set amount of time and the other will put the message up until it is replaced by anothermessage 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 adifferent message appears.

You should attempt all of the tasks below in order. Remember - these tasks are designed tomake 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 hecatches the fish.

Task 2 - Make the fish say a random message, taunting the shark every time it changesdirection. There should be five different messages.

Page 11: Scratch Games

10

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 sothat the block is used only when theshark touches the fish.

Note that the say will cause the shark to stop for half a second so there is no need to havethe block as well.

Task 2

First create a new called Taunts.

Click on the Fish sprite.

Then use the and block to store anumber between 1 and 5.

You will now need to create 5blocks, one for each message.

Finally, add all the blocks under the block where the fishchanges direction.

Make sure the blocks are below each other and notinside each other!

Page 12: Scratch Games

11

By the end of each task I should know how to...Task 1 - make sprites appear and disappearTask 2 - make sprites disappear to random timesTask 3 - make sprites appear randomly in different places on the

stage.

Bonus PointsSo far, all the sprites in our game can be seen for the whole timethe game is being played.

It is common, in games, for sprites to appear on the screen forshort spaces of time and then disappear. These sprites could bethings like power-ups or items that characters can pick up.

To make sprites appear and disappear in Scratch we use the and blocks.

Here is example ofhiding and showing asprite.

To make the sprite appear randomly theblock 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 tomake 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’ spriteappear, on the stage, randomly every 1 to 10 seconds. If the shark catchesthe 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 tomake the cheesy puffs change where they appear on the stage.Note - you will have to think of minimum and maximum x and y numbers.

The script starts when the green Go flag isclicked.

The loop repeats the blocks inside ituntil the game is stopped.

The sprite starts by being hidden, waits 10seconds, is shown, waits 2 seconds and thendisappears when the loop starts again.

}

Page 13: Scratch Games

12

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.

Finally, add an IF block to the shark to add 50 pointson to the score if the shark is touching the cheesy puffs.

Task 2

This is an easy one! Simply addanother block to thesecond wait block.

Task 3

To make the cheesy puffs appear in random places onthe stage we need to look the stage’s coordinates.

Remember the stage ranges fromx = -240 up to 240 andy = -180 up to 180

We can use this in our cheesy puff script.Add a block with random numbersfor x and y.

Note that the x and y numbers do not goright to the edge of the stage or some ofthe cheesy puff sprite would disappearoff the edge of the stage.

Add the block to makethe cheesy puffs appear at randomtimes.

-160,70.

Page 14: Scratch Games

13

By the end of each task I should know how to...

Task 1 - end a game using the broadcast block or the timer.

Game OverMost games will have some way of indicating that the game is finished. This may be a messagethat 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. Thereare a few ways of doing this. Two methods are shown below.

Method 1

The game could finish when theplayer has scored a certain score.

Method 2

The game could last a fixed amountof time.

You should attempt the task below. Remember - these tasks are designed to make you solveproblems 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 stopwhen the timer passes 40 seconds or when the score is equal to 10.

The IF block is used to sense when the scorereaches 20 or more.

Broadcast sends a message out to all theother sprites (and the stage).

When other blocks hear the message they canthen take some sort of action.

This example changes to adifferent background that hasthe text “Game Over”.

The reset timer block starts a clock running.

This loop keeps this script from going to thenext blocks until the clock reaches 60 seconds..

When the clock reaches 60 the background isswitched and all the scripts are stopped.

Shark

Stage

Stage

Page 15: Scratch Games

14

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 yourscript you need threeblocks.

Together they looklike this.

When the Timer andthe Score variable areadded we get.

This can be used, as you were shown in Method 2, to make the Stage script loop until one orthe other statement is true.

Remember, you will have to Import a second background that the game can switch to when itfinishes.

Page 16: Scratch Games

15

By the end of each task I should know how to...Task 1 - add another fish sprite that moves differentlyTask 2 - add a poisonous starfish that the shark should avoidTask 3 - increase the number of steps a fish sprite moves while

the game is being played.

More FishAs your games become more complex youwill probably find that you add more andmore sprites.

Each sprite can be made to behavedifferently from the others by simplyhaving different scripts.

It is also possible to change the behaviour of sprites while the game is playing.

To do this we have to replace fixednumbers with variables.

For example, to change the glide speedwhen the shark catches the fish, we wouldcreate a variable to store the Speed.

Other blocks can then be usedat other places in your scripts to changewhat is stored in the Speed variable.

You should attempt at least one of the tasks below. Remember - these tasks are designed tomake 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. When the shark touchesthis fish 5 points should be added to the score.

Task 2 - Add a starfish sprite to the game. Make the star fish move slowly round thescreen. 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 moveslightly faster making it harder to catch.

This would add1 second to theglide speed .

Page 17: Scratch Games

16

More Fish (solutions)Task 1

First Import another fish sprite.

Now create a script to controlthe second fish (note - to makeit harder to catch, the fishmoves further (move 5 steps)with each step and turns moreoften (repeat 20).

Finally, add another IF block to the shark to add 5 pointsto 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 slowlyaround the stage. Remember the way the spritemoves is controlled by the random, repeat and movenumbers.

Another IF block is added to the shark script tothe score by -10 when the shark touches the

starfish.

Task 3

To control the speed of the fish during the gamereplace 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 thegame when the green flag is clicked.

Finally (in the shark script), add a variableblock to add 3 to the number of steps the fish moveswhen it is touched by the shark.

Page 18: Scratch Games

17

By the end of each task I should know how to change thedifficulty of the game by ...

Task 1 - allowing the player to control the speed of the fishTask 2 - allowing the player to control the speed of the sharkTask 3 - allowing the player to control how often the fish turns.

Setting DifficultyTo change how difficult the game is, we have to control certain aspects of the game. Forexample, 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.

By adding a button to the game we can give the player a way of changing what is stored in theFish Moves variable.

Add a new button sprite and edit it theadd the word “easy” over the middle of it.A script on the button will then changethe speed of the fish when the playerclicks on the button.

You should attempt all the tasks below in order. Remember - these tasks are designed to makeyou 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 changethe 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 towardsthe mouse pointer. Add more blocks to each button to change the speed of theshark when they are pressed.

Task 3 - Repeat the above to control how far the fish moves before turning.

New Fish Script

Create and add a variableto the move block.

Original Fish Script

Page 19: Scratch Games

18

Setting Difficulty (solutions)Task 1

Create a Fish Moves variable. Create and edit an“easy” button and add the script shown. Repeat thesesteps for a “hard” button.

Remember, to set the Fish Moves to different valuesin the easy and hard buttons.

Task 2

For every number you want to control with the twobuttons another variable has to be created. Tocontrol 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 withhow 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 block to the easy and hardbuttons.

Remember to experiment with the values.

Page 20: Scratch Games

19

By the end of each task I should know how to...Task 1 - add a simple sound to a scriptTask 2 - make a sound play when sprites touchTask 3 - add background music to scratch.

Adding SoundScratch has several ways of playing/adding sounds to a project.

Where you add the sound will be important. If you want background music add the blocks tothe stage. If you want to add a sound to an event (like two sprites touching) think of wherethe sound blocks should be attached.

You should attempt at least one of the tasks below. Remember - these tasks are designed tomake 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 yourgame. Sources of piano music could be the Internet, your home or the MusicDepartment.

These blocks allow you to record your own sounds usinga microphone or Import a saved sound file.Note that Scratch comes with many pre-saved sounds.}

}}}}

Drum beats of different pitches can be added. Thespeed 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 itdown, speed it up or set it to a specific number ofbeats per minute (bpm).

Page 21: Scratch Games

20

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 theblock 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 thestage and place them inside a forever loop so that theykeep 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 alterthe length of each note by changing the length of thebeats.

Page 22: Scratch Games

21

By the end of each task I should know how to...Task 1 - control a sprite’s movement using the keyboardTask 2 - control a sprite’s movement using the keyboard and store

two players’ scores.

Two PlayersOur 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 touse the keyboard to control the fish or shark as we canonly have one mouse.

If you look at the category you will see that wecan change the position of a sprite on the screen by changingits x and y coordinate.

If we use these with theblock we can move a sprite in one direction whena key is pressed.

You should attempt one of the tasks below. Remember - these tasks are designed to make yousolve 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 informationabove 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) whenthe second shark catches the fish.

x gets largerx gets smaller

y gets smaller

y gets larger

Page 23: Scratch Games

22

Two Players (solutions)Task 1

In this task, we simply have to change someof the script we created in the Basic Gamesheets.

Change the fish script as shown below.

Note that four IF blocks areneeded (one for each cursor key).

To change the speed the fish moves at, simply make the x and y coordinates change by a largeramount when each key is pressed.

Task 2

Start by adding another shark sprite to the Game. You should change the colourof the second shark using the edit button in costumes.

Delete the original Score variable and make two new variables for each player.

On the stage script set both variables to zero whenthe 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 inthe first shark’s script.

Before

After