20.03.2013 Views

Introduction to Computer Science with Scratch and App Inventor

Introduction to Computer Science with Scratch and App Inventor

Introduction to Computer Science with Scratch and App Inventor

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Click on the word procedure <strong>to</strong> change the name of the procedure.<br />

We will call our procedure MoveMole. In programming when you<br />

are naming things, you want <strong>to</strong> make sure you use descriptive<br />

names. This will help you remember how your program is working!<br />

Now we just need <strong>to</strong> fill in the MoveMole procedure <strong>with</strong> the comm<strong>and</strong>s that move the mole the way<br />

we want it <strong>to</strong>. How will we tell the mole where <strong>to</strong> go? Do you remember the X <strong>and</strong> Y properties of the<br />

mole sprite? We can access those properties <strong>and</strong> change them using blocks! We can tell the mole <strong>to</strong> go<br />

<strong>to</strong> a new location by setting (changing) the value of the X <strong>and</strong> Y properties.<br />

If you click on the My Blocks tab, you will see that<br />

Mole is one of the options. If you click on Mole,<br />

you will see a list of blocks that allow you <strong>to</strong><br />

manipulate the Mole sprite using code! Here, we<br />

want <strong>to</strong> change the X <strong>and</strong> Y properties of the mole.<br />

Scroll all the way <strong>to</strong> the bot<strong>to</strong>m of the menu of<br />

Mole blocks. You will see blocks that say set<br />

Mole.X <strong>to</strong> <strong>and</strong> set Mole.Y <strong>to</strong>. Mole.X means “the X<br />

property of the sprite Mole”. So these are the<br />

blocks that we want.<br />

Drag one set Mole.X <strong>to</strong> block <strong>and</strong> one set Mole.Y<br />

<strong>to</strong> in<strong>to</strong> the MoveMole procedure.<br />

Now we need <strong>to</strong> figure out what we want the new<br />

X <strong>and</strong> Y values for the Mole <strong>to</strong> be. We want the<br />

mole <strong>to</strong> reappear at a new r<strong>and</strong>om location on the<br />

screen. Just like <strong>Scratch</strong>, <strong>App</strong> Inven<strong>to</strong>r has some<br />

built in <strong>to</strong>ols that allow us <strong>to</strong> generate r<strong>and</strong>om<br />

numbers. However, it is a little trickier than in<br />

<strong>Scratch</strong> <strong>to</strong> set the parameters for the r<strong>and</strong>om<br />

number.<br />

To generate the r<strong>and</strong>om location on the screen, we will use several different blocks from the Math<br />

menu which is located in the Built-In tab – we will need call r<strong>and</strong>om fraction <strong>and</strong> some of the simple<br />

calculation blocks.<br />

mbrace Game Development Workbook Page 40

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

Saved successfully!

Ooh no, something went wrong!