Introduction to the scratch interface_1_ by pptfiles

VIEWS: 30 PAGES: 26

									Using MIT Scratch for Programming and Control Exercise 4 – Cat and Dog game
Year 9 ICT Autumn Term 2007

What you will learn:
• • • • • • • • • • • Revision – making the sprite walk Revision – controlling sprite with arrow keys Walking with keyboard control Importing a new sprite and costumes Creating automatic motion Creating interaction between sprites Setting a countdown using variables Setting a hit count using variables Scoring using variables Importing sounds Importing backgrounds

Revision – Getting the sprite to walk (using costumes)
• Before starting this exercise, dump any existing script into the left had side of the screen (this will delete it) • Click on the costumes tab • Rename the sprite as Cat • Rename the costumes Catwalk1 and Catwalk2

Revision – Getting the sprite to walk (using costumes)
• Create a new script and drag in the move 10 steps instruction • Drag in the switch to costume and select Catwalk2 from the drop down list • Drag in a wait and change the value from 1 sec to 0.1 secs • Repeat the move, switch costume and wait steps to go back to Catwalk1 • Add an if on edge, bounce instruction

Revision – Driving the mouse with the arrow keys
• We will use the change x by and change y by instructions to order to ensure the correct up and down vertically or side to side horizontally movement of the Sprite • Before completing this exercise dump your existing script into the left hand side of the screen (this will delete it)

Revision – Driving the mouse with the arrow keys
• Start a new script with the when green flag pressed and forever instructions • Choose the if condition instruction and drag in the key pressed instruction from the sensing topics. Set the key pressed to down arrow • Add the point in direction and change y by set instructions Set point in direction to 180 and change y by to -10

Revision – Driving the mouse with the arrow keys
• We will now copy the main part of the script then edit the sub script for each movement of the cat for each of the four arrow keys • Click on the copy (stamp symbol) tool then click on the word if in the script. This will create a copy of that part of the script.

Revision – Driving the mouse with the arrow keys
• Repeat this process twice more in order to create the scripts for each different movement of the sprite • Drop the three new copied sets of instructions inside the forever instruction

Revision – Driving the mouse with the arrow keys
In the second instruction set • Set the key pressed to up arrow • Set the point in direction to 0 • Set the change y by to 10 In the third instruction set • Set the key pressed to right arrow • Set the point in direction to 90 • Set the change y by to 10 In the forth instruction set • Set the key pressed to left arrow • Set the point in direction to -90 • Set the change y by to -10

Revision – Driving the mouse with the arrow keys
• Click the green flag button. • It should now be possible to drive your cat sprite around the screen using the arrow keys

Cat walking with keyboard control
• Looking back over the previous exercises it should be possible to make a script combining what you have already learned that allows you to walk the Cat sprite around the screen using the arrow keys • Note that two scripts have been created here as an expedience for display purposes – it is possible to combine all of this into one script • (Keep this script – it will be a part of the game)

Importing a new sprite and importing costumes
• Working in the exercise already started, click the centre button below the stage. This allows you to import a new sprite onto the stage • Browse the Animals folder and select dog2-b

Importing a new sprite and importing costumes
• The dog only comes with one costume and we need two to make it walk. With the dog sprite selected click the costumes tab and click import • Select dog2-c

Importing a new sprite and importing costumes
• Rename the dog sprite as Dog • Rename the costumes as Dogwalk1 and Dogwalk2

Importing a new sprite and importing costumes
• In the game the dog will walk around the screen automatically. • Create the script illustrated • The instruction from the motion topic will make the wander round but not in a straight path • The instruction from the sensing topic will cause the dog to interact whenever it touches the cat

Setting the countdown for the game (using variables)
• You need to create a variable in order to make a counter which counts down from 15. • You can create this script with one or other of the sprites selected or with the stage selected • Click the variables topic, click Make a variable. Name the variable time.

Setting the countdown for the game (using variables)
• Create the script illustrated

• The time should be displayed in the corner of the stage

Counting the number of times the dog hits the cat
• You can create this script with one or other of the sprites selected or with the stage selected • Create two more variables called hit and toggle (toggle is needed so that the hit count does not keep on going all the time that the dog and cat are in contact

Counting the number of times the dog hits the cat
• This script sets the toggle to a value of 0 whenever the sprites are not in contact

Counting the number of times the dog hits the cat
• This script changes the value of hits by one if the cat is touching the dog and the value of toggle is 0. • As soon as the instruction is carried out toggle is set to a value of 1 and the hit count will then not go up until the sprites have separated and then touch again

Scoring feedback
• This script gives alternative messages from the cat sprite, giving feedback to the user of the game according to how well you have avoided the dog and giving instructions for starting a new game • Since this script determines the action of the cat, create this script with the Cat sprite selected • The dialogues are all found in the purple looks topic

Importing sounds
• As this action changes things to do with the whole game, change actions on the stage. • Click the stage button to the left of the sprite symbols below the stage. • Click the Sounds tab and import the sound DrumSet1 • Click the Scripts tab and create the simple script shown

Importing a background
• Click the Backgrounds tab and browse for a new background.

Developing the game
• In this example two dogs have been created which react with each other and chase the cat! • (Simply import another dog as before but change its colour) • Notice that the scripts have been slightly amended so that the dogs and cat now go to centre positions in the stage at the beginning of the game.

Developing the game

Developing the game


								
To top