22.02.2017 Views

-ORIENTED

MagPi54

MagPi54

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

UNDERSTAND OBJECT-<strong>ORIENTED</strong> PROGRAMMING<br />

Feature<br />

>STEP-05<br />

Compare scores<br />

We’ve only got one sprite so far. But we’re about to<br />

add another and compare one sprite’s total with the<br />

other. Choose the Sensing selection of blocks and<br />

look for one marked x-position of e. Change<br />

‘x-position’ to total. Drag the block into the correct<br />

side of the greater-than block.<br />

>STEP-03<br />

Throw the dice<br />

Click on Events and drag a when green clicked<br />

block to the scripts area. Below this you need to<br />

add three set diceN to pick nd 1 to 6 blocks.<br />

Below these blocks, add set to dice1 + dice2<br />

+ dice3 (you need to drag one () + () block inside<br />

another to add up three blocks.<br />

>STEP-04<br />

Speak<br />

Now drag a for block and attach it to the end of<br />

the code. Change it to for 1 secs. Below<br />

that, drag an if block. Inside, add a [] > [] (greaterthan)<br />

block. Drag the variable to the left side of<br />

the greater-than block. Drag a block inside the if<br />

block and change ‘Hello!’ to ‘I win!’.<br />

>STEP-06<br />

Player 2<br />

Our Player 1 is ready. Now we’re going to clone the sprite<br />

to create a Player 2. Right-click the sprite and choose<br />

Duplicate. The new cat sprite will be automatically called<br />

‘Player2’. Click on Player1 in the Sprites window. Change<br />

the of e block to of e (as shown<br />

below). Now Player1 compares their total to Player2 (and<br />

Player2 is comparing theirs to Player1). Click the green<br />

g to run the progrm nd ee whih pyer win.<br />

SCOPE<br />

he concet of coe i imortant in oectoriente rogramming n Scratch, it<br />

o imle that o ma not een notice it t or to laer each hae their on<br />

dice1, dice2, an dice3 ariale, l a total ariale hee ariale are local in<br />

coe hen Plaer annonce total, it their total f oth rite accee the<br />

ame total, it ol e gloal in coe ariale in oect are local in coe<br />

raspberrypi.org/magpi February 2017 69

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!