A. creating clones 1. clone Bubbles - Scratch From...

9
80 It’s important to delete the clone inside a forever loop otherwise too many clones will be generated and the programme will crash. Skills Training 5 In many projects you see multiple copies of a single sprite: bubbles in a fish tank, clouds of smoke, rockets, bullets, flocks of birds or of sheep, players on a soccer team, etc. In Scratch 2 you can create a single sprite and then generate many clones of the sprite. You might have a very busy stage but when you stop the script, all you see is your single sprite. The image made by the stamp block cannot move or be moved. It doesn't have a unique identity or name and cannot respond to a message or to user interaction the way a sprite can. The stamp block instructs the sprite to create a picture of itself at the current location of the sprite. A clone is a copy of a parent sprite. It inherits the parent's scripts, costumes, sounds, and properties, and it can be moved, modified and made to behave in a different manner from the original. However it is not a sprite and can't respond to user interaction as it doesn't have a unique identity or name. All clones share the same scripts. The three clone blocks are used to create and programme clones of a sprite. A. creating clones the only hat block not in the Events palette. The blocks are in the Control palette. 1. clone Bubbles www.scratch.mit.edu/projects/19857330 5_01 clone_Bubbles To do: Create an underwater animation. One fish sprite creates a clone of itself so that two fish are seen swimming about. The fish sprite also creates a clone of another sprite (a bubble). The clones just keep bubbling up from the mouth of the fish. fish script 1 of 2 … the clone of this fish forever loop creates a clone of the bubble in each pass of the loop. animates this sprite (the fish) moves this sprite changes the y-position of this sprite “I am the Fish” - the little green guy is only my clone changes clone’s direction, position, colour and size defines the animation and movement characteristics of my clone fish script 2 of 2 Question: When the programme is stopped the small fish and the bubbles disappear. Why is this? fish sprite ... or of another sprite The clone does not have a script Bubble scripts Hide parent bubble so that it’s not visible on the stage. Use the show block to make the clones visible. When each bubble clone starts it goes to the stage position of the Fish. To help make this happen, in the Paint Editor set costume centre of the fish over its mouth.

Transcript of A. creating clones 1. clone Bubbles - Scratch From...

80

It’s important to delete the cloneinside a forever loop otherwise

too many clones will be generated and the programmewill crash.

Skills Training 5

In many projects you see multiple copies of a singlesprite: bubbles in a fish tank, clouds of smoke,rockets, bullets, flocks of birds or of sheep, playerson a soccer team, etc. In Scratch 2 you can create asingle sprite and then generate many clones of thesprite. You might have a very busy stage but whenyou stop the script, all you see is your single sprite.The image made by the stamp block cannot move orbe moved. It doesn't have a unique identity or nameand cannot respond to a message or to userinteraction the way a sprite can. The stamp blockinstructs the sprite to create a picture of itself at thecurrent location of the sprite.A clone is a copy of a parent sprite. It inherits theparent's scripts, costumes, sounds, and properties,and it can be moved, modified and made to behavein a different manner from the original. However it isnot a sprite and can't respond to user interaction asit doesn't have a unique identity or name. All clonesshare the same scripts.The three clone blocks are used to create andprogramme clones of a sprite.

A. creating clones

the only hat block notin the Events palette.

The blocks are in the Control palette.

1. clone Bubbleswww.scratch.mit.edu/projects/19857330

5_01 clone_Bubbles

To do: Create an underwater animation. One fishsprite creates a clone of itself so that two fish areseen swimming about. The fish sprite also createsa clone of another sprite (a bubble). The clonesjust keep bubbling up from the mouth of the fish.

fish script 1 of 2

… the clone of this fish

forever loop creates a clone ofthe bubble in each pass of the loop.

animates this sprite (the fish)

moves this sprite

changes the y-position of this sprite

“I am the Fish” - the littlegreen guy is only my clone

changes clone’s direction,position, colour and size

defines the animation andmovement characteristics ofmy clone

fish script 2 of 2

Question: When the programme isstopped the small fish and the bubblesdisappear. Why is this?

fishsprite

... or of another sprite

The clone does not have a script

Bubble scripts

Hide parent bubble sothat it’s not visible onthe stage. Use theshow block to makethe clones visible.

When each bubble clone starts it goes to the stageposition of the Fish. To help make this happen,in the Paint Editor set costume centre of the fishover its mouth.

81

Skills Training 5

2. clone Bulletswww.scratch.mit.edu/projects/22498561

5_02 shootAliens

To do: This Spacewars game works well but it isincomplete. There is only one alien ship. There isno Score script and it needs a Timing script also.Can you make it more exciting and perhaps addsound and other features?

(a) scripts on alien sprite

3 costumes on each alienalienShip1alienShip1ashellBlast

(b) Laser sprite scripts(yellow)

(c) script on gun sprite

(d) scripts on rotating barrell sprite

(1) The repeat until loop makes the animated Alien ship move down the stage until it either touches the bottom edge or else it collides with a laser bullet.(2) If touching the bottom of the stage, it makes the alien ship disappear for up to 2 seconds, after which it appears again at the top of the stage.(3) if touching the laser bullet it sends out the broadcast

'hit', switches to shellBlast costume, fades away due to the ghost effect in the repeat 10 loop and it returns to the top of the stage to fight again.(4) The laser bullet sprite responds to the space key which creates a clone of itself each time it's pressed. The bullet takes up position under the barrel of the gun and points in the same direction as the barrel. The repeat until (touching edge) loop makes it move at 20 steps until it is deleted at the edge of the stage. The delete this clone block removes it if it hits the alien ship.(5) Player controls the gun barrel with keyboard. Initially it points upwards and the go to front block ensures that the barrel is to the front with the bullet hidden beneath it.

NOTES:

(1)

(2)

(3)

(4)(5)

82

Skills Training 5

B. new blocks: how to Define a procedure

This procedure draws the curve of an arc which isa quarter of the circumference of a circle. To getthis precise curve the repeat value x degrees mustequal 90º. (repeat 15x6º = 90º). The value of themove steps only affects the size of the shape.The point in direction combination initialises thedirection of the pen before drawing the secondcurve in the shape (shown with the dashed line).

This procedure clearsprevious drawings andit initialises the penstate and settings. Italso set the initialdirection and positionof the sprite.

www.scratch.mit.edu/projects/22531677

5_03 flowerDraw

To do: Draw a 5-petal flower using procedures toinitialise the pen and draw the flower components.

Draw the fullshape 5 timeswith a 72º turnin each pass ofthe loop.

(5 x 72º = 360º)

x2

Draws flower

To try:Define aprocedurecalled flowerand script adesign thatdraws manyflowers.

3. draw Flowers

4. draw Treeswww.scratch.mit.edu/projects/22652317

5_04 drawTrees

the random numberscreate the tree effect,by varying the lengthof each branch.

Note: The tree has 18 radialbranches. 18 x 20º = 360The line thickness(pen size) makes a 20º turnadequate. Experiment withpen size and amount ofturning.To do: Draw a tree using

the technique for drawinga circle from centre onpage 31.

To try: Adapt this to drawflowers using the sametechnique.

mainscript

Introduced page 48

83

Skills Training 5

5_05 jumpCartwheel

In the More Blocks palette click ‘Make a Block’, give it aname and click OK. You have a new block in the paletteand a special definition block in the Scripts area. The scriptunder the definition block defines what the new block does.

This project has two new blocks, one will make the ballerinasprite do a cartwheel, the other will make her do a triplejump. There is an option to add various types of inputs (orparameters). The Jump block definition has two numberparameters, the height (number of ‘steps’) and the wait(number of seconds). Finally, to run the procedures, createa main script which contains the new blocks.

NOTE 1: The ballerinasprite comes with 4costumes but youcan edit it to add yourown extra costumes.

(c) cartwheel definition

my cartwheel

simple definition block(a) the main script

new blockwith parameters

down1

(b) jump definition my triple jump

NOTE 2:A block definitioncannot be deleted untilall instances of its definedblock are first deletedfrom the scripts area.

NOTE 3:Notice howparameter blockse.g. wait and heightcan be addedto operators.

5. Jumps and Cartwheelswww.scratch.mit.edu/projects/23016400

To do:Make the Ballerina do gymnastics. See page 25for previous use of ballerina sprite graphic.

84

Skills Training 5

5_06 Fireworks

costume 2flash 1

… hides sprite at start… clicking the stage starts the firework show

… randomises number of seconds before ignition (See note above)

… hides firework after completing previous loop

… sets y at bottom of stage… randomises x… runs new ignite block (see definition block below)

… it’s a continuous show

… returns sprite to normal size after previous loop… clears graphic effects from previous loop

… runs new explode block (see definition block on opposite page)… changes backdrop to starry night sky

NOTE: Randomise other sprites to have different wait intervals e.g. 1 to 4 secs, 2 to 5 secs etc.

costume 1rocket

costume 3flash 2

… loop makes rocket rise into sky reaching random heights of between

10x10, 11x10, 12x10 etc to 25x10 pixels (100 to 250 px)

… makes the rocket visible

… makes and defines ignite block

… sets random colour of rocket and explosion… firework starts as a rocket

ignition block definition

Only 1 sprite is necessary but you can make twoduplicates as pictured.

Each spritehas 3 costumes.

sprite 1exploding

sprite 2exploding

sprite 3taking off as rocket

6. fireworks Displaywww.scratch.mit.edu/projects/22538818

To do: Create a simulation of a fireworks display.

Create the sprite and its costumes in the PaintEditor of Scratch 2.0. Alternatively, you could useexternal software such as DrawPlus - see page 41for hints and ideas.

(b)

(a)

(c)

the main script

script (c)script (d)

85

Skills Training 5

Click the stage to start the animation

… makes and defines explode block

… switches rocket costume to flash1 to simulate explosion step 1… grows between 50% and 150%

… sound of firework (imported from free sound effects web site)… switches to explosion step 2 (extra flashes)

… makes explosion expand

… brightens sky

… gives increasing sparkle effect to explosion

… makes explosion gradually disappear (vanishing at 100%)

explode block definition

5_07 cloneSwarm

7. clone Swarmwww.scratch.mit.edu/projects/22542618

To do: Script a single sprite ( a larget dot) tocreate a clone of itself each time the scoreincrements to a multiple of 10. All the scripts areon the one sprite.

(a)

(b)

(c)

(d)

(a) Create two variables score and I'm a Clone. The point in direction (pick random 1 to 360) combination is an alternative to x, y coordinates to use the whole stage.(b) The mod operator was used already on pages 76,77. Here it's used to create a new clone at every 10th score.(c) .. If I'm not a Clone create a clone of myself(d) Change Boolean value to true. Change colour so that the parent sprite stands out from the crowd.

A Boolean value can only betrue or false. In (d) I'm aClone is set to true becausewhen I start as a clone thevariable's value is true. . .In (a) I'm a Clone is not aclone yet, so it's false.

. . . See a similar use of 0and 1 as Boolean valueson pages 76, 77.

This animationillustrates how clonesdon't haveindividuality in theway a sprite does.The clones all takethe same instructionand all behave in thesame way. They canact differently fromthe parent but all willact diffently together.

When the programmeis stopped all you seeis the parent sprite.

(d)

86

Skills Training 5

A rabbit sits on the near side of an 8-lane motorway. Carsare approaching from 4 lanes on the right and from 4 laneson the left. The rabbit wants to cross the motorway tocollect carrots from the field opposite.

In the game, the player must make the rabbit hop acrossthe motorway without getting run over by a car. The rabbitmust collect a carrot and bring it back to the near side.Every hop is worth a score of 1 and every carrot he collectsgives him a 10 points bonus. The rabbit returns to hisstarting position when he is run over by a car. If he is inpossession of a carrot, when he is run over, the carrot isreturned to its starting position also.

The game ends after 2 minutes is reached or if the rabbitloses all his five lives.

Hop Rabbit Hop

1. Background: Draw a background consisting of 10 rectangles 480 x 36 pixels. Colour the rectangles as shown, the top rectangleshould resemble a field for the carrots and the bottom rectangle represents a green grassy verge.

Draw the top-view of a car in the Scratch Paint Editor or in an externaldrawing environment. Although it appears that there are eight cars, thereare only four cars and each has one clone. See (c).

a. When you set color to 0, you set it to its default colour. In this case, red.

b. Car 1 of 4 starts off the stage in the top lane moving right to left. See (g)

c. Each car appears as two cars on the stage, the original car and a clone of itself.

e. The red car, car 1 creates a clone of itself (block c). The clone appears as the green car in the lane just below the red car. Set color to 80 to change the clone into a green car.

d. This is a custom made block which has a definition block below.

f. The cloned car starts in on the stage and in the lane below its origin sprite.

same as (d).

g. Each time the custom block Move car is used, the sprite follows this script. The block can be usedin other sprites, but it must be redefined. This allows for variations. See i.

Custom block definition:

h. Always start with show, when hide block is used in a script.

i. When the Move car block is redefined for Cars 2, 3 and 4 you can set a different speed for each.

j. The If loop runs the wrapping and pausing feature that make there-appearance of the cars less predictable.The wait algorithm produces values 0, 0.2, 0.4 . . . 1.6, 1.8. 2.0 secs

To do: Create and script the cars 2, 3 and 4 and their vlones

2. Variables:

Create the variable scripts on the stage. Click the Stage icon on the left of the sprite thumbnails.

for Number, Position offset, Score, Lives, Timer. Variable

5_08 hopRabbit

8. hop Rabbitwww.scratch.mit.edu/projects/22546814

To do: Create a game in which a rabbit has tocross a busy motorway to collect carrots.

scripts are not shown here. Go online and click See inside.

3. the Cars: Car 1 of 4and its clone

87

Skills Training 5

Draw a rabbit sprite or importsprite from internet images orfrom this project. Create 3costumes, rabbit1, rabbit2 andrabbit3. At start up, go to frontensures the rabbit is not hiddenbehind a car or carrot. Costumes2 and 3 are used when the rabbit is run over by a car. When that happens, the rabbit broadcasts a message to the carrot sprites and returns to its starting position.

Loop ensures therabbit doesn’t goabove top of stage.

Loop ensures therabbit doesn’t gobelow bottom ofstage. Resets rabbitto start position.

5. The Rabbit 3 costumes: 5 scripts

Scripts to move the rabbit using arrow keys on keyboard

Make changes for down arrow withscore for each ‘hop’.

x 2Make changes for right arrow.No score for sideways ‘hops’.

The broadcast instructs the carrot to go back to its start position whenthe rabbit is run over while in possession of a carrot. (See carrot script 2)

carrot script 1 of 2

x 5

x 2

Logic: The Carrot go Back message is received by the carrot when the rabbitcollides with a car. If the Y position of this carrot is the same as the rabbit’sY position and it’s not at the green grassy verge it means the rabbit is in possessionof the carrot. Then it should go back to start resetting number of carrots to 5.

4. Carrots This is carrot script 2 of 2: Collision script

Logic:If the numberof carrots = 5and the rabbittouches thiscarrot then

they should bothhave same x, y untilthey reach the greengrassy verge. Thenthe number ofcarrots is reduced by1. Bonus points arescored. The carrotx,y is set to greenverge.

Programmethe Rabbitto carrya carrot

To do:

88

Skills Training 5

C. create a list

Immediately all the List blocks (a) to (j) appear and alist monitor pops up on the stage. You can typedirectly into the list monitor. Let’s suppose youhave given the list the name questions. You havenot added any items to questions yet so the list isempty, with length 0. To add items to questions,click the plus + button in the bottom left of the listmonitor.

New List

CancelOK

List name: questions

For all sprites For this sprite onlyYou must havea sprite, butwhen creatinga list, it maysuit better tohide it.

5_09 tenQuestions

9. Ten Questionswww.scratch.mit.edu/projects/22663222

To do: Create two list (i) 10 questions (ii) 10 answers

fox

Resize the list monitor bydragging from bottom rightempty list monitor

If you want to add ten questions to questions clickthe + ten times and you will get ten empty inputboxes. As you fill in the items, a scroll bar appearson the right of the monitor.

OR: Alternatively, you can add things to the list withthe add to block. Type into the small input windowand click the block to add each item.OR: There is a third, and very easy method to additems to a list. Create your list in Word or Notepadand save as a plain .txt file. When creating your list,right-click on the list monitor and import the txt filelist you have already prepared. Here, we will fill inthe list directly, using the list monitor.

(c)

(g)

(j)

(a)

(b)

(d)

(e)

(f)

(h)

1.

foxharehen

dog

cowsheeppighorsegoosecat

Now, create two lists: One is a list of words thatwill form the questions. The other list of words willform the answers. It is important that both lists bethe exact same length and that the index number ofeach should correspond.

A list of adultanmals andanother listof their young.

cubleveretchicken

pup

calflambpigletfoalgoslingkitten

The numberin front ofeach item isknown as itsindex number.

Each list has alength of 10.

Both lists mustbe the samelength.

Variables and Lists are in the samepalette, because lists are like variables.A variable can hold only one value at

a time. A list can hold several values, all at thesame time. When you click the Make a List buttonthe next thing you do is to give your list a nameand click OK.

Make a List

If you like the bookand would like

to continue beyondthis point

please buy the bookor contact me at

[email protected].

ClickBUY A BOOK

in main menu