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

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

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

<strong>Introduction</strong> <strong>to</strong> <strong>Computer</strong><br />

<strong>Science</strong> <strong>with</strong> <strong>Scratch</strong> <strong>and</strong><br />

<strong>App</strong> Inven<strong>to</strong>r<br />

Name:<br />

Produced by the ITCD Department at California State<br />

University, Monterey Bay <strong>and</strong> mbrace, the Monterey Bay<br />

Regional Academy of Computing Education. For more<br />

information, please visit http://mbrace.csumb.edu/


<strong>Introduction</strong> <strong>to</strong> <strong>Scratch</strong><br />

What is <strong>Scratch</strong>?<br />

<strong>Scratch</strong> is a new programming language that makes it easy <strong>to</strong> create your own interactive s<strong>to</strong>ries, games,<br />

<strong>and</strong> animations – <strong>and</strong> share your creations <strong>with</strong> others on the web.<br />

<strong>Scratch</strong> is developed by the Lifelong Kindergarten research group at the MIT Media Lab<br />

Where can I get <strong>Scratch</strong>?<br />

You can download <strong>Scratch</strong> for free from the MIT website http://scratch.mit.edu. <strong>Scratch</strong> is available for<br />

Mac, Windows <strong>and</strong> Linux. From the <strong>Scratch</strong> website, you can also create an account that allows you <strong>to</strong><br />

upload <strong>and</strong> share the scratch projects you create.<br />

To download <strong>Scratch</strong><br />

at home, start here<br />

How do I learn <strong>Scratch</strong>?<br />

This workbook <strong>and</strong> the <strong>Scratch</strong> workshop will give you an introduction <strong>to</strong> some of the <strong>Scratch</strong> basics. To<br />

learn more, you can use the resources on the <strong>Scratch</strong> website, or Google for more information on<br />

<strong>Scratch</strong> – there are a lot of great resources on the web – including some great tu<strong>to</strong>rials on You Tube.<br />

Probably the best way <strong>to</strong> learn <strong>Scratch</strong> (or any programming language) is <strong>to</strong> play!! Start up <strong>Scratch</strong> <strong>and</strong><br />

try out different things. Remember, you can’t break or hurt your computer using <strong>Scratch</strong>. Have fun!<br />

mbrace Game Development Workbook Page 2


LESSON 1: Getting Started <strong>with</strong> <strong>Scratch</strong><br />

How <strong>Scratch</strong> Works<br />

In <strong>Scratch</strong>, we will write programs called SCRIPTS. Each SCRIPT controls an object called a SPRITE. The<br />

orange cat that you see when you start up <strong>Scratch</strong> is an example of a Sprite. Below you can also see an<br />

example of what a script looks like:<br />

Sample Script<br />

The <strong>Scratch</strong> Interface<br />

Before we can write a script, we need <strong>to</strong> know a little bit about the <strong>Scratch</strong> interface.<br />

BLOCKS PALATTE CURRENT SPRITE INFO<br />

TABS<br />

SCRIPTS AREA<br />

A Sprite<br />

STAGE<br />

SPRITE LIST<br />

The BLOCKS PALATTE contains the blocks that we can use <strong>to</strong> make our scripts. Each block represents<br />

one instruction in our program.<br />

We build our scripts by dragging blocks from the BLOCKS PALATTE <strong>to</strong> the SCRIPTS AREA.<br />

mbrace Game Development Workbook Page 3


Above the SCRIPTS AREA is the CURRENT SPRITE INFO. This is where we can see all of the information<br />

about the sprite we are programming.<br />

In the CURRENT SPRITE INFO panel, we also have a set of TABS which allow us <strong>to</strong> switch between<br />

different aspects of our sprite.<br />

The SPRITE LIST shows all of the sprites that are part of our program.<br />

The STAGE is the background that our program runs on.<br />

Sprites<br />

Each object in our program is a sprite. The default sprite is the orange cat, but you can pick from a<br />

library of different sprites, or even make your own!<br />

Let’s pick or make a new sprite. To pick a new sprite, we’ll use the but<strong>to</strong>ns right under the stage. The<br />

first but<strong>to</strong>n let’s you draw your own sprite, the second but<strong>to</strong>n let’s you pick a sprite from a library <strong>and</strong><br />

the third but<strong>to</strong>n picks a r<strong>and</strong>om sprite for you.<br />

Draw your<br />

own sprite<br />

Pick a<br />

sprite<br />

R<strong>and</strong>om<br />

sprite<br />

Let’s pick a sprite! Click the middle but<strong>to</strong>n <strong>to</strong> open the library of sprites.<br />

You can pick a sprite from any of the<br />

folders in the library. To go up a level in<br />

the library click the arrow but<strong>to</strong>n at the<br />

<strong>to</strong>p. When you find a sprite that you<br />

would like <strong>to</strong> use, click OK.<br />

mbrace Game Development Workbook Page 4


I decided <strong>to</strong> pick a dinosaur. You can see that my dinosaur is on <strong>to</strong>p of the cat. To move the sprites<br />

around on the stage, you can just click on them <strong>with</strong> the mouse <strong>and</strong> drag them <strong>to</strong> a new location.<br />

Now, let’s get rid of the cat sprite. If you right-click on any sprite, it will pop up a menu of options:<br />

Right-click <strong>to</strong><br />

get this menu<br />

<strong>to</strong> appear<br />

Choose the delete option <strong>to</strong> remove<br />

the cat. You can see that two of the<br />

other options are resize this sprite<br />

which allows you <strong>to</strong> make the sprite<br />

bigger or smaller <strong>and</strong> rotate this<br />

sprite which allows you <strong>to</strong> change the<br />

direction the sprite is pointing.<br />

mbrace Game Development Workbook Page 5


Moving Your Sprite<br />

Everyone should have a sprite now. The next step is <strong>to</strong> write a script that will make our sprite move on<br />

its own! We write scripts by dragging blocks from the block palate on<strong>to</strong> the scripts area. Since we want<br />

our sprite <strong>to</strong> move, we will use blocks from the motion set (the blue blocks). We’ll first make our sprite<br />

move forward 10 steps, so we’ll drag a block that says move 10 steps on<strong>to</strong> the scripts area.<br />

If you click on the block, you can see the sprite move on the stage.<br />

Click on<br />

block here<br />

Sprite will<br />

move here<br />

If you want the sprite <strong>to</strong> move further, you can make the number of steps bigger. Try making the<br />

number 100 <strong>and</strong> see what happens.<br />

To make the sprite move backwards, you can make the number of steps negative by putting a minus<br />

sign in front of the number. Try making the number of steps -100 <strong>and</strong> see what happens.<br />

The size of the steps that a sprite makes is based on a coordinate system. Every spot on the stage can<br />

be identified by an x, y coordinate. The x coordinates go from left <strong>to</strong> write <strong>and</strong> have values -240 <strong>to</strong> 240.<br />

The y coordinates go from bot<strong>to</strong>m <strong>to</strong> <strong>to</strong>p <strong>and</strong> have values from -180 <strong>to</strong> 180.<br />

mbrace Game Development Workbook Page 6


The direction that your sprite moves in the coordinate system is based on the direction that it is pointing.<br />

You can always see the location of your sprite <strong>and</strong> its direction by looking at the current sprite info.<br />

Sprite’s current<br />

location<br />

The direction that a sprite is pointing is based on the degrees around a circle.<br />

-90 = left<br />

0 = up<br />

180 = down<br />

90 = right<br />

Sprite’s current<br />

direction<br />

Many of the motion blocks change the direction that the sprite is pointing, or move it around the stage’s<br />

coordinate system:<br />

mbrace Game Development Workbook Page 7


Point your sprite in the direction you want it <strong>to</strong> move. You can<br />

specify an absolute direction or another object on the stage<br />

Move your sprite <strong>to</strong> a specific location on the stage. You can<br />

give coordinates or another object on the stage. Glide can be<br />

used <strong>to</strong> make the sprite move more slowly.<br />

Change x <strong>and</strong> y are great comm<strong>and</strong>s <strong>to</strong> use for moving your<br />

sprite around the screen. We will use these comm<strong>and</strong>s in a<br />

later lesson.<br />

By combining these blocks, we can make our sprite perform more complex behaviors. Let’s make our<br />

sprite “dance” by putting <strong>to</strong>gether multiple actions.<br />

To build complex behavior, snap blocks <strong>to</strong>gether like<br />

Legos.<br />

To see what your new dance looks like, click once on<br />

the group of blocks <strong>and</strong> watch what happens on the<br />

stage<br />

When your script runs, the actions on the blocks<br />

happened one after the other in the order you have<br />

them connected, this is called sequential execution<br />

It might be a little hard <strong>to</strong> see each action individually, because the actions happen very quickly. We can<br />

slow things down a little bit using some of the control blocks. These are on the yellow menu:<br />

mbrace Game Development Workbook Page 8


Let’s add some yellow wait blocks <strong>and</strong> see what<br />

happens when we run our new script (remember,<br />

click on the group of blocks <strong>to</strong> run the script).<br />

You should see the sprite wait between parts of its<br />

new dance. You can change where you put the wait<br />

<strong>and</strong> you can also change how long you wait by<br />

changing the number in the block. [HINT: for a<br />

shorter wait you can use a fraction of a second, 0.5 is<br />

half a second]<br />

Every time we click the group of blocks, our sprite will<br />

do his dance. We can use another control block <strong>to</strong><br />

make the sprite keep dancing.<br />

Drag a forever block in<strong>to</strong> your script. The forever<br />

block can go around a group of other blocks, <strong>and</strong> we<br />

want <strong>to</strong> arrange it so that it goes around our entire<br />

dance script.<br />

Now click on the big group of blocks <strong>and</strong> see what<br />

happens – your sprite should keep dancing.<br />

This is called LOOPING. The forever block is a loop<br />

that repeats a group of blocks over <strong>and</strong> over again<br />

until we tell it <strong>to</strong> s<strong>to</strong>p.<br />

Inside the loop, the individual actions will still<br />

happen sequentially in the order they appear. When<br />

the last block finishes, the loop will start all over <strong>with</strong><br />

the first one again.<br />

Sometimes, we don’t want <strong>to</strong> loop forever, but we do<br />

want <strong>to</strong> do a set of actions more than once. <strong>Scratch</strong><br />

has another kind of loop that lets us repeat a set of<br />

blocks a specific number of times. It is up <strong>to</strong> you <strong>to</strong><br />

pick the kind of loop that will work best in your<br />

program. Play around <strong>with</strong> both the forever loop <strong>and</strong><br />

the counter loop <strong>to</strong> see how they work.<br />

mbrace Game Development Workbook Page 9


To s<strong>to</strong>p a forever block, click the red s<strong>to</strong>p sign above<br />

the stage.<br />

Next <strong>to</strong> the s<strong>to</strong>p sign, there is a green flag but<strong>to</strong>n.<br />

You can have your program start by clicking the<br />

green flag rather than clicking on the blocks in your<br />

script. Once we learn how <strong>to</strong> do this, we will use it<br />

for most of our scripts.<br />

Drag a when (Green Flag) clicked block on<strong>to</strong><br />

the <strong>to</strong>p of your forever loop. Notice that the<br />

green flag block is shaped differently from the<br />

other blocks we have been using. Blocks <strong>with</strong><br />

a curved <strong>to</strong>p don’t allow other blocks <strong>to</strong><br />

attach <strong>to</strong> the <strong>to</strong>p. These blocks are starting<br />

points for our programs. Later on we’ll learn<br />

about some of the other curved blocks as<br />

well.<br />

Now, you can click the green flag <strong>to</strong> start<br />

running your script <strong>and</strong> click the s<strong>to</strong>p sign <strong>to</strong><br />

s<strong>to</strong>p.<br />

Why do we want <strong>to</strong> use the green flag? It might<br />

not see very exciting right now.<br />

If you click the full screen icon in the upper right<br />

h<strong>and</strong> corner of your screen, your stage will take<br />

up the whole screen. In this mode, you don’t<br />

have access <strong>to</strong> the blocks <strong>to</strong> click on, but you will<br />

have the green flag <strong>and</strong> the red s<strong>to</strong>p sign.<br />

Also, as we add more things <strong>to</strong> our projects, you<br />

will see how the green flag allows us <strong>to</strong><br />

coordinate more complicated programs.<br />

mbrace Game Development Workbook Page 10


Changing the Background on the Stage<br />

Now that everyone has a dancing sprite, let’s make the background nicer than plain old white. We can<br />

change things about the stage just like we can change our sprite.<br />

1<br />

Click Here<br />

2<br />

Check out full screen mode <strong>and</strong> watch your<br />

sprite dance across the screen! Use the green<br />

flag <strong>and</strong> red s<strong>to</strong>p sign <strong>to</strong> start <strong>and</strong> s<strong>to</strong>p your<br />

program!<br />

So far we have used blocks from two different<br />

menus: the motion menu (blue blocks) <strong>and</strong> the<br />

control menu (yellow blocks). Whenever you use<br />

a new menu, it is always a good idea <strong>to</strong> read<br />

through the blocks available. You never know<br />

when a new comm<strong>and</strong> will come in h<strong>and</strong>y!<br />

You can only modify one sprite or the background at a<br />

time. To make changes <strong>to</strong> the stage, click on the picture<br />

of the stage in the sprite list underneath the stage.<br />

The sprite information panel at the <strong>to</strong>p will change <strong>to</strong><br />

show that we are working <strong>with</strong> the stage now instead of<br />

our sprite.<br />

We can pick a new background from a library just like we<br />

picked a new sprite.<br />

First, click on the Backgrounds tab<br />

Next, click on the Import but<strong>to</strong>n<br />

mbrace Game Development Workbook Page 11


User Controlled Sprites <strong>and</strong> Branching<br />

When you click the import but<strong>to</strong>n, a box will<br />

open that lets you navigate through the<br />

library of backgrounds.<br />

Pick one that you like <strong>and</strong> click OK <strong>to</strong> make it<br />

the new background on your stage.<br />

To go back <strong>to</strong> working on your script for your<br />

sprite, you just click on the sprite in the sprite<br />

list.<br />

Click here <strong>to</strong> go<br />

back <strong>to</strong> your<br />

sprite<br />

For both sprites <strong>and</strong> backgrounds, you aren’t limited <strong>to</strong> the options in the <strong>Scratch</strong> library. You can also draw<br />

your own sprites, modify existing sprites, or import drawing/pho<strong>to</strong> files from your computer. We will see<br />

how <strong>to</strong> do this in later lessons.<br />

Up until now, all of our sprite’s actions have been controlled by move blocks. However, in a game, we’ll<br />

want the user <strong>to</strong> be able <strong>to</strong> move the sprite around using the keyboard. In order <strong>to</strong> be able <strong>to</strong> do this,<br />

we need <strong>to</strong> learn a new control block first:<br />

The if block implements a BRANCHING control structure.<br />

Branching lets us choose <strong>to</strong> do something, only if certain<br />

conditions are met. We do this all of the time in real life.<br />

For example, we might say if it is raining, take an umbrella<br />

outside. This means that when the condition “it is raining”<br />

is true, we should take an umbrella outside <strong>with</strong> us. We<br />

will use branching in programming in much the same way.<br />

mbrace Game Development Workbook Page 12


To use an if block <strong>to</strong> control the movement of the sprite, we first have <strong>to</strong> think about what conditions<br />

we want <strong>to</strong> test. Let’s use the st<strong>and</strong>ard arrow keys as our conditions. This means we want <strong>to</strong> test the<br />

following conditions:<br />

If the up arrow is pressed, we want the sprite <strong>to</strong> move up the stage (this is a positive change in<br />

the y value of the sprite’s location)<br />

If the down arrow is pressed, we want the sprite <strong>to</strong> move down the stage (this is a negative<br />

change in the y value of the sprite’s location)<br />

If the left arrow is pressed, we want the sprite <strong>to</strong> left on the stage (this is a negative change in<br />

the x value of the sprite’s location)<br />

If the right arrow is pressed, we want the sprite <strong>to</strong> right on the stage (this is a positive change in<br />

the x value of the sprite’s location)<br />

Let’s look at our if block again. Next <strong>to</strong> the word “if” you’ll see a diamond shaped opening. This is<br />

where the condition will go. The action <strong>to</strong> do if the condition is true goes inside the block.<br />

condition <strong>to</strong> check goes here<br />

action <strong>to</strong> do if condition is true goes here<br />

Where do we get the conditions? There are several different types of blocks that can go in<strong>to</strong> the<br />

condition spot. All blocks that can go in a condition spot will be diamond-shaped, just like the opening<br />

in the if block. One place <strong>to</strong> get condition blocks is the sensing menu. Blocks on the sensing menu let us<br />

check for events like:<br />

Mouse events: when the user clicks the mouse (you can also get the location of the mouse<br />

arrow on the screen)<br />

Keyboard events: when the user pushes a but<strong>to</strong>n on the keyboard (here we are interested in<br />

capturing specific keys – the arrow keys – but you can capture any key you want)<br />

When two objects on the screen are <strong>to</strong>uching each other<br />

We will use the sensing menu throughout our game, so it is useful <strong>to</strong> become familiar <strong>with</strong> the available<br />

blocks.<br />

mbrace Game Development Workbook Page 13


The sensing menu contains many blocks that test different<br />

conditions. The one that we are going <strong>to</strong> use <strong>to</strong> help control our<br />

sprite checks <strong>to</strong> see if a certain key is pressed. The default key is<br />

the space bar, but we can change it <strong>to</strong> select any key we want by<br />

clicking on the arrow <strong>to</strong> bring up a drop down menu<br />

Here the arrow keys are<br />

the ones that we want <strong>to</strong><br />

use in our condition<br />

since they will tell our<br />

sprite which direction <strong>to</strong><br />

move on the stage.<br />

Now that we know where <strong>to</strong> find the condition blocks, let’s use if, conditions, <strong>and</strong> movement blocks <strong>to</strong><br />

build a user controlled sprite. What do you think our script will look like?<br />

Here is one example of a script that will work for<br />

creating a user-controlled dinosaur. You can write<br />

a script that does the exact same thing that looks<br />

very different – in programming there are often<br />

lots of different ways <strong>to</strong> do the same thing – that is<br />

OK. The different ways <strong>to</strong> approach the same<br />

problem are called algorithms. Coming up <strong>with</strong><br />

good algorithms <strong>to</strong> solve a problem is a big part of<br />

what programmers do.<br />

mbrace Game Development Workbook Page 14


Costumes <strong>and</strong> Animation<br />

Now that you have your sprite moving around the screen, let’s make our program a little fancier. Let’s<br />

have the sprite change the direction he is pointing so that when the user pushes the right arrow or the<br />

left arrow, the sprite will face the same direction he moves. We will use costumes <strong>to</strong> do this.<br />

Costumes give you a way <strong>to</strong> change the way a given<br />

sprite looks. You can see the costumes available <strong>to</strong><br />

your sprite by clicking on the costumes tab in the<br />

current sprite info pane in the middle of the screen.<br />

Right now you can see that our dinosaur (named<br />

Sprite1) had only one costume available. This costume<br />

is named dinosaur1.<br />

To add more costumes for a sprite, you can copy existing costumes, import new costumes from the<br />

library (if available), make your own costume using drawing <strong>to</strong>ols or a pho<strong>to</strong>, or edit existing costumes.<br />

Here, we want <strong>to</strong> provide a new costume for our sprite that is exactly the same as dinosaur1, only we<br />

want it <strong>to</strong> be facing the opposite direction. To do this we will first copy <strong>and</strong> then edit the existing<br />

costume.<br />

On the costumes tab:<br />

1. Click the copy but<strong>to</strong>n <strong>to</strong> make a copy of the dinosaur1 costume<br />

2. On the new copy of the costume (dinosaur2), click the edit but<strong>to</strong>n <strong>to</strong> bring up the paint edi<strong>to</strong>r<br />

3. In the paint edi<strong>to</strong>r, click the flip but<strong>to</strong>n <strong>to</strong> flip the costume <strong>to</strong> face the other direction<br />

Now you should have two costumes for your sprite – one that faces <strong>to</strong> the left <strong>and</strong> one that faces <strong>to</strong> the<br />

right. Look at the names of the costumes <strong>to</strong> figure out which is which, in my example:<br />

dinosaur1 faces <strong>to</strong> the right <strong>and</strong><br />

dinosaur2 faces <strong>to</strong> the left<br />

You can always rename your costumes (or your sprites) <strong>to</strong> something that makes more sense. Here I am<br />

going <strong>to</strong> rename the dinosaur1 costume <strong>to</strong> be rightDino <strong>and</strong> the dinosaur2 costume <strong>to</strong> be leftDino<br />

mbrace Game Development Workbook Page 15


Now I can modify my script so that my dinosaur always faces the same direction he is moving. I will do<br />

this using the switch <strong>to</strong> costume block from the looks menu. The looks menu (purple blocks) contains<br />

actions that let us control how the appearance of our sprite changes during a program.<br />

Changing Your Sprite’s Looks<br />

To change the costume of our sprite, we used a block from the looks menu. The looks menu contains<br />

blocks that change things like the color <strong>and</strong> size of your sprite.<br />

Try out different looks blocks <strong>and</strong> see<br />

what happens. This simple example<br />

shows a change color effect block. Now<br />

the dinosaur will cycle through color<br />

changes every second.<br />

Now, when the right arrow is pressed, our sprite<br />

will face <strong>to</strong> the right <strong>and</strong> move <strong>to</strong> the right<br />

When the left arrow is pressed, the sprite will face<br />

<strong>to</strong> the left <strong>and</strong> move <strong>to</strong> the left.<br />

The looks blocks are the dark purple blocks.<br />

mbrace Game Development Workbook Page 16


Other useful blocks are the say <strong>and</strong> think blocks. These blocks display words in either thought bubbles<br />

or dialog bubbles.<br />

Using a combination of say <strong>and</strong> wait blocks, you<br />

can create the appearance that multiple sprites are<br />

having a conversation. We will see how this could<br />

work when we add more sprites <strong>to</strong> our game!<br />

Other Actions<br />

Just like we moved our sprite around <strong>with</strong> the arrow keys, you can have your sprite respond <strong>to</strong> other<br />

keys <strong>with</strong> other actions. See if you can make your sprite jump or fly when another key is pressed!<br />

In this script, I’ve added a new<br />

action that will happen when the<br />

space bar is pressed? What do you<br />

think this action is? Try it out <strong>and</strong><br />

see.<br />

mbrace Game Development Workbook Page 17


Interacting <strong>with</strong> Other Sprites<br />

So far we have used the if block <strong>to</strong> let the user control the actions of our sprite, but we can also use the<br />

if block <strong>to</strong> make different sprites interact <strong>with</strong> each other. First I need <strong>to</strong> add a second sprite <strong>to</strong> my<br />

program. Scripts are sprite-specific. Every new sprite will need its own script(s) <strong>to</strong> tell it how <strong>to</strong><br />

behave.<br />

Now I have two sprites – a dinosaur <strong>and</strong> a bowl of<br />

cheesy poofs.<br />

I can control my dinosaur using the arrow keys on<br />

the keyboard using the script I wrote before.<br />

Let’s try <strong>to</strong> make the dinosaur eat the cheesy poofs!<br />

First, we need <strong>to</strong> move the dinosaur <strong>to</strong> the cheesy<br />

poofs – that is easy <strong>to</strong> do using the arrow keys<br />

The second step is <strong>to</strong> write a script so that we know<br />

when the dinosaur arrives at the cheesy poofs.<br />

When the dinosaur gets <strong>to</strong> the cheesy poofs, we<br />

want <strong>to</strong> make them disappear so that it looks like<br />

they’ve been gobbled up.<br />

Let’s write a script for the cheesy poofs that makes it look like the dinosaur is eating them. First, <strong>to</strong><br />

detect when the dinosaur gets <strong>to</strong> the cheesy poofs we can use an if block. What should the condition<br />

be? Luckily, one of the sensing blocks will tell us when two sprites are <strong>to</strong>uching each other. Here, we<br />

want <strong>to</strong> pick the condition “if <strong>to</strong>uching sprite1” since the dinosaur is sprite1.<br />

Check down here <strong>to</strong> see what each sprite is named.<br />

You can change the name of your sprites if you like.<br />

mbrace Game Development Workbook Page 18


?<br />

Now we know how <strong>to</strong> tell when the dinosaur is <strong>to</strong>uching the<br />

bowl of cheesy poofs. What action do we want <strong>to</strong> happen<br />

when that condition is true? We want the cheesy poofs <strong>to</strong><br />

disappear so that it looks like the dinosaur ate them.<br />

We can use the looks block hide <strong>to</strong> make the cheesy poofs<br />

disappear. Try <strong>to</strong> run the program now. Have the dinosaur<br />

eat the cheesy poofs <strong>and</strong> see what happens.<br />

When we move the dinosaur over the cheesy poofs, the<br />

poofs disappear; making it look like the dinosaur ate<br />

them!<br />

When we s<strong>to</strong>p <strong>and</strong> restart our program, the poofs are<br />

still gone. Why?<br />

Once we hide the poofs, they don’t reappear until we<br />

explicitly tell them <strong>to</strong> using a show block.<br />

Let’s change our script so that every time we restart the<br />

program, the poofs reappear. Where should we put the<br />

show block?<br />

If we put the show block right after the green flag block,<br />

every time we restart our program, the poofs will<br />

reappear. Try it out <strong>to</strong> see what happens.<br />

Our game is still kind of lame – the poofs always<br />

reappear at the same spot, let’s change that. Let’s<br />

make it so that every time we restart our game, the<br />

poofs reappear at a r<strong>and</strong>om spot on the screen.<br />

Since show always makes the poofs reappear at the<br />

same spot they were when they were hidden, we need<br />

<strong>to</strong> move the poofs <strong>to</strong> a new spot (a r<strong>and</strong>om spot)<br />

before we make them reappear.<br />

mbrace Game Development Workbook Page 19


To get a r<strong>and</strong>om number, we use the pick r<strong>and</strong>om block from the<br />

Opera<strong>to</strong>rs menu. The Opera<strong>to</strong>rs menu (bright green blocks)<br />

contains many mathematical <strong>and</strong> logical operations <strong>and</strong><br />

comparisons that will be very useful in programming.<br />

To use the pick r<strong>and</strong>om block, you specify two numbers <strong>and</strong> then<br />

<strong>Scratch</strong> will r<strong>and</strong>omly pick a number between those two numbers.<br />

Since we want <strong>to</strong> r<strong>and</strong>omly position our poofs on the stage, we<br />

want <strong>to</strong> r<strong>and</strong>omly pick an x value <strong>and</strong> a y value <strong>to</strong> move the poofs<br />

<strong>to</strong>. What should the range of values be for r<strong>and</strong>omly picking an x<br />

<strong>and</strong> r<strong>and</strong>omly pick a y?<br />

Remember the coordinate system underlying the stage?<br />

The x values on the stage range from -240 <strong>to</strong> 240<br />

The y values on the stage range from -180 <strong>to</strong> 180<br />

*HINT* When you are using new blocks in <strong>Scratch</strong>, remember <strong>to</strong> notice the shape of the block, this will<br />

give you a clue as <strong>to</strong> where the blocks can be used.<br />

If we add a go <strong>to</strong> x: y: block <strong>with</strong> our<br />

pick r<strong>and</strong>om blocks included, our cheesy<br />

poofs will show up at a new location<br />

every time we start our program over.<br />

Give it a try <strong>and</strong> see how it works.<br />

Remember, you still need <strong>to</strong> s<strong>to</strong>p (click<br />

the red s<strong>to</strong>p sign) the program <strong>and</strong><br />

restart (click the green flag) the program<br />

<strong>to</strong> see the cheesy poofs repositioned on<br />

the screen.<br />

What would make the game more fun? It would be lots more fun if we didn’t have <strong>to</strong> restart the<br />

program every time we wanted the poofs <strong>to</strong> reappear. How could we change our program so that every<br />

time the dinosaur eats the poofs, they reappear at a new r<strong>and</strong>om location on the screen?<br />

mbrace Game Development Workbook Page 20


Here is one script that works. To make the<br />

poofs reappear each time the dinosaur eats<br />

them.<br />

Now, let’s add one more thing <strong>to</strong> our game. Let’s make it harder for the dinosaur <strong>to</strong> catch the cheesy<br />

poofs by making the poofs move around the screen.<br />

You already know how <strong>to</strong> do this – you need a script that moves the poofs around the screen <strong>with</strong>out<br />

user interaction like the very first script we wrote for the dinosaur. You can be creative <strong>with</strong> how you<br />

want the poofs <strong>to</strong> move around the screen – create the behavior you want by using different<br />

combinations of blocks from the motion menu.<br />

There are all different ways you can make your poofs move around the screen:<br />

Use a combination of r<strong>and</strong>om, show <strong>and</strong> hide <strong>to</strong> make it look like the poofs are falling from the<br />

sky<br />

Make the poofs move back <strong>and</strong> forth across the stage<br />

Have the poofs bounce r<strong>and</strong>omly around the screen<br />

Things <strong>to</strong> think about when creating poof motion are:<br />

Where should your poofs start when the game starts? Do you need <strong>to</strong> have a specific location?<br />

Keep in mind that at the <strong>to</strong>p of the screen, the y axis value is 180 <strong>and</strong> at the bot<strong>to</strong>m of the<br />

screen the y axis value is -180<br />

There is a bounce on edge block that you might find useful<br />

Once you have the poofs moving around the screen, you have the start of a fun game!<br />

mbrace Game Development Workbook Page 21


Variables <strong>and</strong> Keeping Score<br />

Now that we have the start of a fun game, we want <strong>to</strong> keep score by letting our dinosaur earn a point<br />

for every bowl of cheesy poofs that he eats. In order <strong>to</strong> do this, we are going <strong>to</strong> use VARIABLES.<br />

A program can have many variables, each keeping track of a different value. In order <strong>to</strong> be able <strong>to</strong> tell<br />

our variables apart, we give each variable a name.<br />

Points:<br />

23<br />

To create a variable in <strong>Scratch</strong>,<br />

go <strong>to</strong> the red Variables menu.<br />

Then click on the Make a<br />

variable but<strong>to</strong>n.<br />

This will open a new box where<br />

we can type the name of our<br />

variable. Then click ok.<br />

You can think of a variable like a chalkboard<br />

that can s<strong>to</strong>re one number on it. We can read<br />

the number off the chalkboard or write a new<br />

number on the chalkboard, <strong>and</strong> it will always<br />

keep track of the last thing we wrote down.<br />

You always want <strong>to</strong> give your variable a<br />

descriptive name that explains what<br />

value it represents. Since we want a<br />

variable that will hold the number of<br />

points our dinosaur has, we’ll call our<br />

variable “points”<br />

mbrace Game Development Workbook Page 22


After you create a variable, you will see more blocks appear in<br />

the variables menu.<br />

One block allows you <strong>to</strong> set (or write) a new value <strong>to</strong> your points<br />

variable.<br />

You can also change the value of points (you can add or subtract<br />

from the current value).<br />

Show <strong>and</strong> hide control whether the current value of the variable<br />

will be shown on the stage. By default, the value of the variable<br />

is shown.<br />

We want <strong>to</strong> add one <strong>to</strong> points every<br />

time our dinosaur eats a bowl of cheesy<br />

poofs. So, we look at our program <strong>and</strong><br />

find the spot where a bowl of cheesy<br />

poofs gets eaten. We insert a block that<br />

adds one <strong>to</strong> points.<br />

Now, every time the dinosaur eats<br />

another bowl of cheesy poofs, our<br />

variable get incremented (one gets<br />

added <strong>to</strong> the current value)<br />

Our variable points always keeps track<br />

of how many points the dinosaur has.<br />

mbrace Game Development Workbook Page 23


Look out for the duck!<br />

You might notice that if you s<strong>to</strong>p the<br />

game <strong>and</strong> then restart it, the variable<br />

points still has the same value. We<br />

probably want points <strong>to</strong> start over every<br />

time we restart our game. Giving a<br />

variable a starting value is called<br />

initialization.<br />

We can easily initialize our variable by<br />

adding code that sets it back <strong>to</strong> zero<br />

every time the green flag is clicked.<br />

So far, our dinosaur earns points by eating bowls of cheesy poofs. Let’s make things a little harder for<br />

the dinosaur. Our dinosaur is afraid of ducks, so let’s add a duck sprite that moves r<strong>and</strong>omly around<br />

the stage. The dinosaur must try <strong>to</strong> avoid the duck while he eats cheesy poofs. If the duck catches the<br />

dinosaur, the dinosaur loses two points.<br />

When we want <strong>to</strong> add a lot <strong>to</strong> our program all at once, it helps <strong>to</strong> break it down in<strong>to</strong> smaller steps.<br />

Then we can do one step at a time <strong>to</strong> make the programming easier. For example:<br />

1. Add a duck sprite<br />

2. Make the duck sprite move r<strong>and</strong>omly around the screen<br />

3. Have the duck sprite recognize when it is <strong>to</strong>uching the dinosaur<br />

4. When the duck <strong>to</strong>uches the dinosaur subtract two points from the dinosaur<br />

There are many different scripts you can write <strong>to</strong> create this program. Often in programming there is<br />

more than one way <strong>to</strong> do things. Also, you will notice that many of the steps <strong>to</strong> program our duck are<br />

very similar <strong>to</strong> steps we <strong>to</strong>ok <strong>to</strong> program the dinosaur <strong>and</strong> the poofs. This is also a characteristic of<br />

programming: lots of times you will only have <strong>to</strong> solve a problem once <strong>and</strong> you can reuse the solution.<br />

So, you can tell if the dinosaur is <strong>to</strong>uching the duck the same way you checked <strong>to</strong> see if the dinosaur was<br />

<strong>to</strong>uching the cheesy poofs!<br />

Here is a very simple duck program. Feel free <strong>to</strong> get creative!<br />

This part of the code makes the duck move<br />

around the screen<br />

This part of the code h<strong>and</strong>les what happens<br />

when the duck catches the dinosaur<br />

mbrace Game Development Workbook Page 24


Winning, Losing, <strong>and</strong> Game Play<br />

Our game right now starts when we click the green flag <strong>and</strong> s<strong>to</strong>ps when we click the s<strong>to</strong>p sign. To make<br />

it more realistic (AND FUN!) we need <strong>to</strong> decide how someone wins (or loses) our game. When a player<br />

wins the game, we’ll display a new screen that says “Congratulations – you win!”, when a player loses<br />

our game, we will display a different screen that says “I’m sorry, you lose, game over”.<br />

You can decide how someone should win or lose your game. There are lots of options for winning. You<br />

could have winning be dependent on completing a specific task, or on getting a certain number of points.<br />

For this example, I am going <strong>to</strong> say that a player wins my game if the dinosaur gets 10 points.<br />

We know that the number of points the dinosaur has is held in the points variable. We also know that if<br />

we want <strong>to</strong> do something only when a certain condition is met, that we can use an if block <strong>to</strong> test that<br />

condition. What condition do we want <strong>to</strong> test? We want <strong>to</strong> test if the value of the points variable is<br />

equal <strong>to</strong> 10. In order <strong>to</strong> do this, we will need <strong>to</strong> use a new block from the Opera<strong>to</strong>rs (green) menu. In<br />

the opera<strong>to</strong>rs menu, there are blocks that let us test equal <strong>to</strong>, greater than, <strong>and</strong> less than.<br />

To test if the variable points is equal <strong>to</strong> 10<br />

1. Choose an if or forever if block<br />

2. From the opera<strong>to</strong>rs menu, pick the equal<br />

<strong>to</strong> block <strong>and</strong> drag it <strong>to</strong> the condition<br />

location in your if block<br />

3. From the variables menu, pick the name<br />

of the variable <strong>and</strong> drag it <strong>to</strong> one side of<br />

the equal sign<br />

4. Fill in the other side <strong>with</strong> the value we<br />

want <strong>to</strong> test (10)<br />

Where we put this block, will depend on what we want <strong>to</strong> do when the win condition is met. I want the<br />

background of my game <strong>to</strong> change <strong>to</strong> display a message that the player won the game. Therefore, I am<br />

going <strong>to</strong> put my block in the script area for my stage.<br />

Remember that <strong>to</strong> switch from a sprite<br />

<strong>to</strong> the stage, you click on the small<br />

picture of the stage at the bot<strong>to</strong>m of<br />

the screen.<br />

Then, click on the scripts tab <strong>to</strong> build a<br />

script for the stage. You can write<br />

scripts for the stage just like you would<br />

for any other sprite however your block<br />

options are different. For example,<br />

there are no motion blocks available for<br />

the stage because it wouldn’t make<br />

sense for the stage <strong>to</strong> move around!<br />

mbrace Game Development Workbook Page 25


Now we can think about what we want <strong>to</strong> happen when our win condition is met. I want <strong>to</strong> display a<br />

new background that says “you win”. That means I first have <strong>to</strong> create the background that I want.<br />

To create a new background that<br />

shows my win message I:<br />

1) Switch <strong>to</strong> the backgrounds<br />

tab<br />

2) Make a copy of my existing<br />

background<br />

3) Click the edit but<strong>to</strong>n on the<br />

copy <strong>to</strong> open the paint<br />

edi<strong>to</strong>r<br />

(I hope your win screen looks<br />

better than mine! )<br />

4) Inside the paint edi<strong>to</strong>r, I add my win message <strong>to</strong><br />

the new background<br />

Now I have two backgrounds, one that displays the<br />

general background for my game, <strong>and</strong> another that<br />

displays the “you win” message.<br />

You can create whatever win message you like,<br />

using the <strong>Scratch</strong> library of backgrounds, or a<br />

background you create yourself.<br />

The last step is <strong>to</strong> actually have my background<br />

change when the dinosaur gets 10 points. I can<br />

do this by choosing switch <strong>to</strong> background blocks<br />

from the looks menu.<br />

Notice that I also set up my background so that<br />

when I start the game, I always start <strong>with</strong> the<br />

plain background.<br />

mbrace Game Development Workbook Page 26


You might have noticed that when the dinosaur gets 10 points, the background switches <strong>to</strong> display the<br />

win message, but everything else keeps going (for example, the duck keeps moving). I want <strong>to</strong> change it<br />

so that everything s<strong>to</strong>ps when the player wins <strong>and</strong> all the sprites disappear. I could check if points = 10<br />

for every sprite, but there is a better way <strong>to</strong> coordinate all of my sprites. I can broadcast a message<br />

telling all of my sprites that the game is over.<br />

To broadcast a message, I pick the broadcast<br />

block from the control menu.<br />

I am going <strong>to</strong> broadcast my message from<br />

my stage since that is where I am checking<br />

for the game ending condition.<br />

To send a message, I first need <strong>to</strong> create it,<br />

so I click on the arrow in the broadcast block<br />

<strong>and</strong> choose new <strong>to</strong> create a new message.<br />

This will pop open a new window.<br />

In the window that opens, I give my new message a<br />

name, I am going <strong>to</strong> call my message “Game Over”<br />

because that is what it will signal <strong>to</strong> my sprites.<br />

The last step is <strong>to</strong> tell my sprites what <strong>to</strong> do when they receive<br />

the game over message.<br />

For each sprite, I choose a when I receive block from the<br />

control menu <strong>and</strong> then a hide block from the looks menu <strong>to</strong><br />

have the sprite disappear when the player wins the game.<br />

Since I am hiding all of my sprites when the game ends, I may<br />

also need <strong>to</strong> go back <strong>and</strong> add a show block when the green<br />

flag is clicked so that the sprite reappears when I restart the<br />

game.<br />

Notice that the block for receiving the Game Over message will be the same for all of my sprites. If you<br />

have code that you want <strong>to</strong> duplicate exactly for multiple sprites, you can drag the block of code over<br />

each of the sprites in the sprite list. Viola! You only had <strong>to</strong> write the code once, <strong>and</strong> all of your sprites<br />

can use it!<br />

mbrace Game Development Workbook Page 27


Now I have my winning condition set. How about a losing condition? I want my player <strong>to</strong> lose when<br />

their score is negative points (less than zero). I can set up the losing condition exactly the same way I set<br />

up the winning condition:<br />

1) Create a new background that displays a losing message<br />

2) Use an if block <strong>to</strong> check for a score that is less than zero<br />

3) When the score is less than zero, display my losing background <strong>and</strong> broadcast the game over<br />

message.<br />

This is just one example of how a player can win or lose a game. Be creative <strong>and</strong> think of what works<br />

best for your game. For example:<br />

You could have the win condition be based on capturing a specific item, or completing a specific<br />

task.<br />

You could use two different variables: a points variable <strong>to</strong> track points <strong>and</strong> determine if the<br />

player wins <strong>and</strong> a health variable that decreases <strong>and</strong> is used <strong>to</strong> determine if a player loses (when<br />

their health variable is zero)<br />

Another option is <strong>to</strong> use a timer. Let’s say that I only want my player <strong>to</strong> win if they can score 10 points<br />

in less than 100 seconds. I can use <strong>Scratch</strong>’s built in time <strong>to</strong> check for this.<br />

The timer is located in the sensing blocks. To use the timer:<br />

1) Click the checkbox next <strong>to</strong> timer <strong>to</strong> show the timer value on the stage<br />

2) Choose when <strong>to</strong> start timing. I want <strong>to</strong> start my timer when the green flag is clicked (as soon as<br />

the game starts). Since I am h<strong>and</strong>ling winning <strong>and</strong> losing in the scripts for my stage, I will control<br />

the timer there as well.<br />

3) Drag a reset timer block <strong>to</strong> where you want <strong>to</strong> start the timer<br />

mbrace Game Development Workbook Page 28


Click the<br />

checkbox next<br />

<strong>to</strong> timer <strong>to</strong><br />

make it show<br />

up<br />

Add a reset timer<br />

block where you want<br />

<strong>to</strong> start the timer<br />

Timer will show up<br />

here on the stage.<br />

If you play the game now, you will see that the timer will count the seconds that the game is being<br />

played. Now we need <strong>to</strong> add a check based on the timer. We are updating our win conditions like this:<br />

The player WINS if: They score 10 points AND they have been playing for less than 100 seconds<br />

The player LOSES if: They have negative points OR they have been playing for more than 100<br />

seconds<br />

AND <strong>and</strong> OR are called Boolean expressions in programming <strong>and</strong> are used a lot when we have more<br />

than one thing that we want <strong>to</strong> check.<br />

Use an AND when you have multiple conditions <strong>and</strong> ALL of them must be true<br />

o This is like the case for winning since it must be true that the user has 10 points <strong>and</strong> it<br />

must also be true that they have been playing for less than 100 seconds<br />

Use an OR when you have multiple conditions <strong>and</strong> only ONE of them must be true<br />

o This is like the case for losing since either the player must have a negative points value<br />

or they must have been playing for more than 100 seconds, but both do not have <strong>to</strong> be<br />

true.<br />

<strong>and</strong> <strong>and</strong> or blocks are available from the opera<strong>to</strong>rs menu. Both<br />

have two slots for the conditions involved.<br />

mbrace Game Development Workbook Page 29


Now that we know about <strong>and</strong> <strong>and</strong> or blocks, we can update our win <strong>and</strong> lose conditions for our player.<br />

You can use <strong>and</strong> <strong>and</strong> or blocks throughout your program any time you want <strong>to</strong> check multiple conditions!<br />

You can also use the same blocks we have talked about <strong>to</strong>day <strong>to</strong> create more challenges for your games.<br />

For example, you can:<br />

Create multiple levels – advancing <strong>to</strong> a new level can be dependent on reaching a certain<br />

number of points, or any other condition<br />

Make your game more challenging as players accumulate more points, for example, I could<br />

make the duck move faster once the dinosaur has 5 points<br />

Be creative! There is no limit <strong>to</strong> the kinds of games you can create using <strong>Scratch</strong>!<br />

Adding Sound <strong>to</strong> Your Program<br />

You can also add sounds <strong>to</strong> your program. The sound blocks are bright purple.<br />

If you add sounds <strong>to</strong> your game, please be<br />

respectful of the other students in the lab <strong>and</strong><br />

keep the volume on your computer low!<br />

To play a sound, you must first add it <strong>to</strong> your<br />

sprite.<br />

There are two ways <strong>to</strong> add a sound <strong>to</strong> your<br />

sprite. You can either record a sound using a<br />

microphone, or you can import a sound. If you<br />

import a sound, you can use the built in sounds<br />

from the scratch library, or upload your own<br />

sound/music file.<br />

All of the options for sounds are on the sounds<br />

tab in the current sprite pane.<br />

mbrace Game Development Workbook Page 30


<strong>Scratch</strong> Review<br />

Now you have built your first computer game using <strong>Scratch</strong>! For our next activity, we will be using the<br />

Google <strong>App</strong> Inven<strong>to</strong>r <strong>to</strong> build applications for Android phones. Many of the principles you learned using<br />

<strong>Scratch</strong> will be directly applicable <strong>to</strong> <strong>App</strong> Inven<strong>to</strong>r <strong>and</strong> beyond. Once you learn the basics of<br />

programming in one environment, <strong>to</strong> learn a new environment you just have <strong>to</strong> learn the new syntax for<br />

the environment or language. In programming the syntax of a language refers <strong>to</strong> the specific comm<strong>and</strong>s<br />

(blocks in <strong>Scratch</strong>/<strong>App</strong> Inven<strong>to</strong>r) that you use <strong>to</strong> implement the logic of your program.<br />

Here are some of the programming basics that we learned in <strong>Scratch</strong> that apply <strong>to</strong> programming in<br />

general.<br />

Execution order<br />

o Sequential<br />

o Branching<br />

o Looping<br />

Variables<br />

o Creating variables<br />

o Giving variables initial values<br />

o Changing the value of a variable<br />

Using r<strong>and</strong>om numbers<br />

Logical expressions<br />

o And<br />

o Or<br />

o Checking equality<br />

Using timers<br />

Capturing user events (e.g. key presses)<br />

Using libraries of comm<strong>and</strong>s<br />

Importing/using media files (sound, pictures, etc)<br />

DEBUGGING!<br />

These are among the principles of computing that we teach in introduc<strong>to</strong>ry programming courses at<br />

CSUMB – they are core <strong>to</strong> underst<strong>and</strong>ing programming <strong>and</strong> computer science! If you have any<br />

questions about any of these concepts, make sure you ask.<br />

mbrace Game Development Workbook Page 31


<strong>Introduction</strong> <strong>to</strong> <strong>App</strong> Inven<strong>to</strong>r<br />

These materials are based on the <strong>App</strong> Inven<strong>to</strong>r tu<strong>to</strong>rials available online: http://appinven<strong>to</strong>r.googlelabs.com/learn/tu<strong>to</strong>rials/<br />

What is <strong>App</strong> Inven<strong>to</strong>r?<br />

<strong>App</strong> Inven<strong>to</strong>r is free software available from Google that allows you <strong>to</strong> build apps for Android phones<br />

using a drag-<strong>and</strong>-drop interface similar <strong>to</strong> that of <strong>Scratch</strong>.<br />

Where can I get <strong>App</strong> Inven<strong>to</strong>r?<br />

<strong>App</strong> Inven<strong>to</strong>r is available for free download at: http://appinven<strong>to</strong>r.googlelabs.com/about/ Follow the<br />

directions <strong>to</strong> install it at home<br />

Do I need an Android Phone?<br />

If you have an Android based smart phone, you can put the apps you create on your phone. However, if<br />

you don’t have an Android phone, you can still build apps <strong>with</strong> <strong>App</strong> Inven<strong>to</strong>r. The Inven<strong>to</strong>r contains an<br />

emula<strong>to</strong>r that lets you simulate a phone on your computer.<br />

Example of the Android emula<strong>to</strong>r that you can use<br />

<strong>to</strong> test your <strong>App</strong> Inven<strong>to</strong>r programs.<br />

What do I need <strong>to</strong> run <strong>App</strong> Inven<strong>to</strong>r?<br />

To use <strong>App</strong> Inven<strong>to</strong>r, you will need a Google account. If you have gmail, you already have a Google<br />

account. If you do not have gmail, you can easily set up a Google account using your existing email<br />

address.<br />

To create a Google account, go <strong>to</strong> www.google.com <strong>and</strong> click on the Sign In link in the upper right<br />

h<strong>and</strong> corner of the screen. On the next page, click on create an account now. Follow the<br />

instructions that appear <strong>to</strong> create your account. Note that you will need access <strong>to</strong> an existing<br />

email account <strong>to</strong> complete the process. Make sure you have a Google account before proceeding<br />

<strong>with</strong> the <strong>App</strong> Inven<strong>to</strong>r portion of the workbook.<br />

mbrace Game Development Workbook Page 32


<strong>App</strong> Inven<strong>to</strong>r Interface<br />

The <strong>App</strong> Inven<strong>to</strong>r interface is a bit more complicated than <strong>Scratch</strong>. You will use three different windows<br />

<strong>to</strong> work on your project.<br />

The first window you will use is <strong>App</strong> Inven<strong>to</strong>r<br />

itself. You will use this window <strong>to</strong> create new<br />

projects <strong>and</strong> <strong>to</strong> add user interface elements<br />

<strong>and</strong> resources (like sound files <strong>and</strong> images) <strong>to</strong><br />

your programs.<br />

<strong>App</strong> Inven<strong>to</strong>r is run through a web browser,<br />

like Firefox or Chrome <strong>and</strong> can be found at<br />

http://appinven<strong>to</strong>r.googlelabs.com<br />

From <strong>App</strong> Inven<strong>to</strong>r, you can open the Blocks<br />

Edi<strong>to</strong>r. Blocks Edi<strong>to</strong>r looks a lot like the script<br />

building area of <strong>Scratch</strong>. There is a menu of<br />

blocks on the left h<strong>and</strong> side of the screen. You<br />

have one tab for built in blocks that can be<br />

used in all projects. The other tab says my<br />

blocks <strong>and</strong> contains blocks that are tied <strong>to</strong><br />

specific user interface elements in your<br />

program.<br />

From the blocks edi<strong>to</strong>r, you can launch <strong>and</strong><br />

connect <strong>to</strong> an emula<strong>to</strong>r. The emula<strong>to</strong>r is used<br />

<strong>to</strong> build <strong>and</strong> debug your program if you don’t<br />

have access <strong>to</strong> an Android phone.<br />

In this workbook, we will be using the emula<strong>to</strong>r<br />

for all of our work. If you have access <strong>to</strong> an<br />

Android phone, you can follow the directions<br />

online for running your programs on your<br />

phone.<br />

mbrace Game Development Workbook Page 33


Mole Mash Game<br />

To learn <strong>App</strong> Inven<strong>to</strong>r, we are going <strong>to</strong> step through one of the Google tu<strong>to</strong>rials <strong>to</strong> build a simple Whacka-Mole<br />

game. We’ll also add a few bells <strong>and</strong> whistles of our own. The first thing we will need <strong>to</strong> do is <strong>to</strong><br />

start <strong>App</strong> Inven<strong>to</strong>r <strong>and</strong> create a new project.<br />

A new project looks like this:<br />

This palate contains<br />

the different user<br />

interface elements<br />

that you can use in<br />

your programs<br />

The middle portion of<br />

the screen is where<br />

you lay out the<br />

interface for your<br />

application<br />

To start <strong>App</strong> Inven<strong>to</strong>r, go <strong>to</strong><br />

http://appinven<strong>to</strong>r.googlelabs.com.<br />

Click on the My Projects link at the <strong>to</strong>p of the page.<br />

Click on the new but<strong>to</strong>n in the upper<br />

left h<strong>and</strong> corner of the screen.<br />

In the dialog box that opens, give your<br />

new game a name. I will call mine<br />

MoleMash*<br />

This section shows you a<br />

list of the components <strong>and</strong><br />

media items that have<br />

already been added <strong>to</strong> your<br />

project.<br />

Here you can see <strong>and</strong><br />

change the properties of an<br />

individual interface<br />

component<br />

*The name that I gave my game might look a little funny. I wrote “Mole Mash” <strong>with</strong> no spaces, <strong>and</strong> I capitalized the first letter<br />

of each word. This kind of formatting is called camel case. Camel case is used extensively in programming for naming different<br />

parts of projects. When you learn a new programming language it is always important <strong>to</strong> learn the conventions (like camel case)<br />

that programmers use in that language. It is also important <strong>to</strong> learn whether a language is case sensitive. In some languages<br />

MoleMash, molemash <strong>and</strong> MOLEMASH would all be the same thing – those are case insensitive languages. In other languages<br />

each different combination of upper/lowercase letters would name a distinct entity.<br />

mbrace Game Development Workbook Page 34


The first thing that we will do is change the name of our project. In the Properties portion of the screen<br />

(far right h<strong>and</strong> side), where it says title type the name of your game.<br />

The name that<br />

you gave your<br />

game will now<br />

show up in the<br />

title bar on the<br />

screen<br />

You will notice that now we have changed the title of our screen <strong>to</strong> be MoleMash (the same as the<br />

name of our program). Each element in our interface also has a name that we can use <strong>to</strong> refer <strong>to</strong> the<br />

element in our program. The screen is called Screen 1.<br />

Now we need <strong>to</strong> set up the interface that a user will see when they start the game. There will be two<br />

elements in our user interface:<br />

1) A canvas which will serve as the area that our game is played on. A canvas is a very useful<br />

element that can be blank or contain a background image. For our purposes, one very useful<br />

property of canvas elements is that we can capture when a user <strong>to</strong>uches or drags their finger on<br />

a canvas.<br />

2) An image of a mole. This image will move around on the canvas <strong>and</strong> the user will <strong>to</strong>uch it <strong>to</strong><br />

score points (just like Whack-a-mole).<br />

To get the canvas element, drag a Canvas block from the Basic palate on<strong>to</strong> Screen1.<br />

Type the name<br />

of your game<br />

here<br />

mbrace Game Development Workbook Page 35


When the canvas is created, it is given the default name Canvas1 by <strong>App</strong> Inven<strong>to</strong>r. Let’s rename the<br />

canvas – we will give all of our elements new names <strong>to</strong> help us keep them straight when we are writing<br />

our program. To rename the canvas:<br />

Now we will change the size of our canvas<br />

element. Again, make sure that the<br />

MyCanvas element is selected in the<br />

Components part of the screen. In the<br />

Properties section, change the width <strong>and</strong> the<br />

height <strong>to</strong> 300 pixels.<br />

Note the other properties that you can set at<br />

this point. For each element we use, there<br />

will be a selection of properties that you can<br />

set using this panel of the <strong>App</strong> Inven<strong>to</strong>r.<br />

1) In the Components section of the<br />

screen make sure that Canvas1 is<br />

selected (highlighted green)<br />

2) Click on the rename but<strong>to</strong>n at the<br />

bot<strong>to</strong>m of the panel<br />

3) In the dialog that pops up, give the<br />

canvas element a new name, I am going<br />

<strong>to</strong> call mine MyCanvas<br />

Now that we have our canvas all set up, we need <strong>to</strong> add our mole image. Go <strong>to</strong> this url:<br />

http://appinven<strong>to</strong>r.googlelabs.com/learn/tu<strong>to</strong>rials/molemash/MoleMashAssets/mole.png<br />

You will need <strong>to</strong> save the mole image <strong>to</strong> your desk<strong>to</strong>p. To do this<br />

on a PC, right-click on the image (on a Mac, hold down the CTRL<br />

but<strong>to</strong>n <strong>and</strong> click the mouse). Choose Save Image As from the<br />

menu that pops up. Make sure you save the image on your<br />

desk<strong>to</strong>p so that you can easily find it later.<br />

mbrace Game Development Workbook Page 36


To add the mole <strong>to</strong> our program, we will use an ImageSprite. A sprite (like in <strong>Scratch</strong>) is an element that<br />

can move around on a canvas element. The sprite element is located in the Animation block menu.<br />

Drag an ImageSprite from the<br />

Animation menu on<strong>to</strong> the<br />

MyCanvas element in the<br />

Viewer. Make sure you put the<br />

sprite on the canvas <strong>and</strong> not on<br />

the background of the screen.<br />

Now we need <strong>to</strong> associate our mole image <strong>with</strong> our sprite. In the<br />

Properties panel, click on Picture. This will pop open a menu. On<br />

the menu, click the Add but<strong>to</strong>n <strong>and</strong> use the Browse feature in the<br />

Upload File box <strong>to</strong> find our mole picture (it should be called<br />

mole.png <strong>and</strong> will be located on your desk<strong>to</strong>p).<br />

Every time we use an image or sound, we will add it <strong>to</strong> our project<br />

like this. Unlike <strong>Scratch</strong>, there is not a library of built in images for<br />

<strong>App</strong> Inven<strong>to</strong>r. You can create your own images, or use images that<br />

you find using the Internet. You could even use the images from<br />

the <strong>Scratch</strong> library in your <strong>App</strong> Inven<strong>to</strong>r programs if you have<br />

<strong>Scratch</strong> on the same computer. On a PC, the <strong>Scratch</strong> Images are<br />

located in: C:\Program Files\<strong>Scratch</strong>\Media\Costumes<br />

At this point, also change the name of the ImageSprite <strong>to</strong> Mole.<br />

[Hint, refer back <strong>to</strong> how we changed the name of the canvas if you<br />

need a reminder].<br />

In the Properties panel for the Mole, you will see an X <strong>and</strong> Y.<br />

These are the coordinates for the sprite on the canvas. If you<br />

drag the sprite around on the canvas, you will see that the X <strong>and</strong><br />

Y values au<strong>to</strong>matically update.<br />

mbrace Game Development Workbook Page 37


Now that we have the mole on the canvas, let’s think about how we are going <strong>to</strong> get him <strong>to</strong> move<br />

around. The way that Whack-a-Mole works:<br />

The mole shows up at r<strong>and</strong>om locations<br />

The mole is only visible for a short period of time<br />

Then the mole moves <strong>to</strong> a new r<strong>and</strong>om location<br />

This means that we will need some sort of way <strong>to</strong> control how long the mole is visible each time it<br />

appears. Luckily, just like <strong>Scratch</strong> had a built in timer that we could use, <strong>App</strong> Inven<strong>to</strong>r has a Clock<br />

element that we can add <strong>to</strong> our program.<br />

Drag a Clock block from the Basic palate<br />

on<strong>to</strong> the MoleMash screen in the Viewer<br />

pane. Instead of showing up on the screen,<br />

the clock will show up underneath in a<br />

section labeled non-visible components.<br />

Now that the clock is added, rename it<br />

MoleTimer. Also, under Properties, set the<br />

interval <strong>to</strong> be 500. The interval is in<br />

milliseconds – there are 1000 milliseconds in<br />

a second, so by setting the interval <strong>to</strong> 500,<br />

our clock will trigger every half-second.<br />

Now we are all ready <strong>to</strong> write the code that will move our mole around the screen! So far we have been<br />

using the interface builder in <strong>App</strong> Inven<strong>to</strong>r. To write code, we need <strong>to</strong> open the Blocks Edi<strong>to</strong>r<br />

To open the Blocks Edi<strong>to</strong>r, click the but<strong>to</strong>n in the<br />

upper right-h<strong>and</strong> corner of your screen. You may see<br />

some security warnings from Windows. Blocks Edi<strong>to</strong>r<br />

is 100% safe <strong>to</strong> run, so click OK or Run on the<br />

warning dialog boxes that pop up.<br />

mbrace Game Development Workbook Page 38


Your Blocks Edi<strong>to</strong>r Screen should look something like this:<br />

Here is the menu of blocks<br />

that you can use in your<br />

programs. There are two<br />

tabs. Built-in contains blocks<br />

that can be used in any<br />

program. My Blocks<br />

contains blocks that are<br />

specific <strong>to</strong> the user interface<br />

elements in your app. If you<br />

click on the My Blocks tab<br />

you should see a list of the<br />

elements we have used.<br />

Clicking on any element or<br />

category will open a menu of<br />

blocks that you can use<br />

Drag the blocks in<strong>to</strong> this<br />

center area <strong>to</strong> create a<br />

program.<br />

In our game, we are going <strong>to</strong> need <strong>to</strong> move the mole twice:<br />

1) The mole will move every time the timer expires<br />

2) The mole will move every time the user successfully <strong>to</strong>uches it<br />

We could write two separate pieces of code <strong>to</strong> move the mole, however the movement is the same in<br />

each case – the mole disappears <strong>and</strong> reappears at a new r<strong>and</strong>om location on the screen. Instead of<br />

writing the movement code twice, we are going <strong>to</strong> write one procedure that moves our mole. A<br />

procedure (may also be called a method or function) is a way <strong>to</strong> give a name a chunk of code. Then,<br />

you can use the name <strong>to</strong> call the procedure from multiple places in your program. Each call <strong>to</strong> a<br />

procedure runs the code associated <strong>with</strong> it.<br />

The Blocks Edi<strong>to</strong>r gives us an easy way <strong>to</strong> create methods. In the Built-In blocks, under Definition we<br />

can use the <strong>to</strong> procedure arg block <strong>to</strong> define a new procedure.<br />

Drag a <strong>to</strong> procedure arg block in<strong>to</strong><br />

the middle area of the screen <strong>to</strong><br />

add it <strong>to</strong> your program.<br />

If you make a<br />

mistake <strong>and</strong> need <strong>to</strong><br />

delete a block, drag it<br />

<strong>to</strong> the trash can in<br />

the bot<strong>to</strong>m right<br />

h<strong>and</strong> corner.<br />

mbrace Game Development Workbook Page 39


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


We know that our X <strong>and</strong> Y locations can be anywhere on the canvas. Since we set our canvas <strong>to</strong> be 300<br />

pixels by 300 pixels, we know that the X <strong>and</strong> Y coordinates range from 0 <strong>to</strong> 300. The call r<strong>and</strong>om<br />

fraction block calls a procedure that generates a r<strong>and</strong>om number between 0 <strong>and</strong> 1. This is a built in<br />

procedure which means we don’t have <strong>to</strong> write it ourselves, we can just call it. If we multiply 300 (the<br />

width of the canvas) by 0, we get 0 (the smallest possible X coordinate). If we multiply 300 by 1, we get<br />

300 (the largest possible X coordinate). So, that means that if we multiply 300 by any number between<br />

0 <strong>and</strong> 1, we are guaranteed <strong>to</strong> get a valid X coordinate. We can do the same thing for the height of the<br />

canvas <strong>and</strong> Y coordinates. In code, that would look like this:<br />

You can get the MyCanvas.Width <strong>and</strong> MyCanvas.Height blocks from the MyCanvas menu on the My<br />

Blocks tab. Notice the difference between the set Mole.X <strong>to</strong> block <strong>and</strong> the MyCanvas.Width block.<br />

Blocks that say set ___ <strong>to</strong> <strong>and</strong> are darker blue allow you <strong>to</strong> update the value of a property. Blocks that<br />

just have the name of the property (like MyCanvas.Width) <strong>and</strong> are lighter blue-gray just get the value of<br />

a property. You can see on the Mole menu of blocks that both types of blocks are available for each<br />

property. There is a set Mole.X <strong>to</strong> block that allows you <strong>to</strong> change the value of the Mole.X property <strong>and</strong><br />

a Mole.X block that allows you <strong>to</strong> find out what the current value of that property is. In programming,<br />

blocks like set Mole.X <strong>to</strong> are called muta<strong>to</strong>rs, since you are changing or mutating the value of a property.<br />

Blocks like Mole.X are called accessors since you are accessing the value of a property.<br />

Now we are almost done <strong>with</strong> our MoveMole procedure, but there is one small change <strong>to</strong> make. Right<br />

now it is possible for our Mole <strong>to</strong> appear only partially on the screen. We can fix this issue by updating<br />

our procedure like this:<br />

Why does this work? Here, we worked out the whole procedure for moving the mole in one step, but<br />

often in programming, you will build a procedure in small pieces over time. You will test each piece as<br />

you go. Remember that the way <strong>to</strong> learn programming is just <strong>to</strong> play <strong>and</strong> try new things. If your<br />

program doesn’t work the way you expect it <strong>to</strong>, just undo the last step you did <strong>and</strong> try something else!<br />

This process of trial <strong>and</strong> error is one of the things that makes programming FUN!<br />

mbrace Game Development Workbook Page 41


Let’s see what our procedure looks like when it runs on a phone. To do this, we will need <strong>to</strong> start the<br />

Android emula<strong>to</strong>r.<br />

Once your emulated phone is ready <strong>to</strong> go, you<br />

need <strong>to</strong> connect <strong>to</strong> it. You do this through the<br />

Blocks Edi<strong>to</strong>r by clicking on the Connect <strong>to</strong><br />

Device but<strong>to</strong>n <strong>and</strong> choosing the emula<strong>to</strong>r from<br />

the drop down list (there should only be one<br />

option). As the Block Edi<strong>to</strong>r connects <strong>to</strong> the<br />

emula<strong>to</strong>r, the little phone icon will first turn<br />

yellow, <strong>and</strong> then green.<br />

When the phone icon is green, your Blocks<br />

edi<strong>to</strong>r is connected <strong>to</strong> your emula<strong>to</strong>r <strong>and</strong> your<br />

app will show up on the simulated screen. Any<br />

changes you make <strong>to</strong> your app in <strong>App</strong> Inven<strong>to</strong>r<br />

or the Blocks Edi<strong>to</strong>r will au<strong>to</strong>matically show up<br />

in the emula<strong>to</strong>r.<br />

Click on the New emula<strong>to</strong>r<br />

but<strong>to</strong>n on the <strong>to</strong>p of the<br />

screen.<br />

This will open a window<br />

telling you <strong>to</strong> be patient. Click<br />

OK <strong>and</strong> be patient<br />

Your emulated phone will fire up <strong>and</strong> initialize.<br />

When you see the middle screen, drag the<br />

green lock <strong>to</strong> “unlock” the phone. When you<br />

see the screen on the far right, your emulated<br />

phone is all ready <strong>to</strong> go!<br />

mbrace Game Development Workbook Page 42


Our mole shows up in the emula<strong>to</strong>r, but he isn’t moving. What’s wrong? We need <strong>to</strong> call our<br />

MoveMole procedure – this is true of every procedure, you must call it when you want it <strong>to</strong> run. When<br />

do we want our MoveMole procedure <strong>to</strong> run? Every time our timer goes off. Where do you think we<br />

could find a block for the timer?<br />

On the My Blocks tab, there is a menu for<br />

MoleTimer. One of the blocks is when<br />

MoleTimer.Timer do. This block allows us <strong>to</strong><br />

specify certain comm<strong>and</strong>s <strong>to</strong> run every time that<br />

the timer goes off.<br />

What do we want <strong>to</strong> do each time the timer goes off? We<br />

want <strong>to</strong> call our MoveMole function. To do this, we want <strong>to</strong><br />

go <strong>to</strong> the My Definitions menu on the My Blocks tab. Our<br />

procedure shoes up in this menu, because it is a procedure<br />

that we defined. We will need the call MoveMole block<br />

This is what the code <strong>to</strong> call our function should look like.<br />

Now if you look at the emula<strong>to</strong>r, the mole should be jumping<br />

around the screen.<br />

What should the next step in our project be? Right now, nothing happens if the user <strong>to</strong>uches the mole.<br />

Let’s add on<strong>to</strong> our program so that when the user <strong>to</strong>uches the mole, his/her score goes up by one. The<br />

steps for this process will be:<br />

1) Create a variable called score <strong>to</strong> hold the user’s score (just like we did in the dinosaur game)<br />

2) Add an element <strong>to</strong> the interface <strong>to</strong> display the current score<br />

3) Add one <strong>to</strong> the value of score every time the user <strong>to</strong>uches the mole<br />

To create a variable, we will use the def variable as<br />

block from the Definition menu in the Built-In tab.<br />

You will notice that when you drag this block in<strong>to</strong> the<br />

program area, there is a yellow exclamation point in the<br />

block. This is because Blocks Edi<strong>to</strong>r wants you <strong>to</strong> give<br />

the variable an initial value. Let’s set the initial value <strong>to</strong><br />

zero since the player will start the game <strong>with</strong> zero<br />

points.<br />

mbrace Game Development Workbook Page 43


When you want <strong>to</strong> add a block, you can select it from a<br />

menu, or you can start typing <strong>and</strong> Blocks Edi<strong>to</strong>r will try<br />

<strong>to</strong> guess which block you want.<br />

If I type a 0, Blocks Edi<strong>to</strong>r will open a menu <strong>with</strong> its<br />

guesses on which block I want. Here there is only one<br />

guess, I want the number 0. Go ahead <strong>and</strong> select the<br />

0. Blocks Edi<strong>to</strong>r will create a number block for you<br />

that you can attach <strong>to</strong> your variable block. The yellow<br />

exclamation point should go away.<br />

Now let’s add a user interface element that displays a user’s current score. To do this, we will need <strong>to</strong><br />

go back <strong>to</strong> the <strong>App</strong> Inven<strong>to</strong>r window [Hint: remember that <strong>App</strong> Inven<strong>to</strong>r is running in your web browser]<br />

Drag a Label element so that it shows up<br />

underneath the canvas on our screen in the Viewer.<br />

Change the name of the label <strong>to</strong> be ScoreLabel.<br />

You can use the Text field in the Properties pane <strong>to</strong><br />

set the initial value of the label <strong>to</strong> be Score: 0<br />

Now, switch back <strong>to</strong> the Blocks Edi<strong>to</strong>r. If you go <strong>to</strong> the My Blocks tab, you should now see that there is a<br />

menu available for ScoreLabel. The next step is <strong>to</strong> figure out what we want the label <strong>to</strong> display. We<br />

always want the label <strong>to</strong> show the current value of the variable score. Let’s write a new procedure that<br />

updates ScoreLabel <strong>with</strong> the current value of Score. I will call my new procedure UpdateScore.<br />

We want <strong>to</strong> update the Text field of the ScoreLabel, so we need<br />

the set ScoreLabel.Text <strong>to</strong> block from the ScoreLabel menu.<br />

Since ScoreLabel is a text label, we will need<br />

<strong>to</strong> use some blocks from the Text menu <strong>to</strong><br />

set its value. We want the value <strong>to</strong> be the<br />

word “Score: “ followed by the value of the<br />

variable score. To combine multiple things,<br />

we will use the join block from the Text<br />

menu. The text block can be used for static<br />

text that will not change like “Score: “.<br />

mbrace Game Development Workbook Page 44


The last thing we need is the value of the variable score. Since score is a variable that we defined, we<br />

can get its value from the My Definitions menu. The word “global” just refers <strong>to</strong> the fact that score is a<br />

global variable - it can be accessed from anywhere in our program.<br />

Here is the completed UpdateScore procedure:<br />

Now, for step three, we need <strong>to</strong> increment the value of the<br />

score variable when the mole is <strong>to</strong>uched. If you go <strong>to</strong> the<br />

Mole menu on the My Blocks tab, you will see a big green<br />

block named when Mole.Touched do. This block captures<br />

the user event created when the user <strong>to</strong>uches the mole<br />

<strong>and</strong> allows us <strong>to</strong> run comm<strong>and</strong>s only when that happens.<br />

When you drag the Mole.Touched block on<strong>to</strong> the program area,<br />

you will see that two blocks (x <strong>and</strong> y) are au<strong>to</strong>matically created.<br />

These blocks give you two local variables called x <strong>and</strong> y. Local<br />

variables can only be used inside the procedure where they are<br />

created. These variables give you access <strong>to</strong> the x <strong>and</strong> y coordinates<br />

where the mole was <strong>to</strong>uched when the event was captured. We<br />

don’t need this information in our game, but for other <strong>to</strong>uch events<br />

it can be very useful <strong>to</strong> have!<br />

What do we want <strong>to</strong> do when the Mole.Touched event occurs? We want <strong>to</strong>:<br />

1) Add one <strong>to</strong> the value of the score variable<br />

2) Call the UpdateScore function <strong>to</strong> update the ScoreLabel in the interface<br />

3) Call MoveMole <strong>to</strong> move our mole <strong>to</strong> a new location<br />

(Do you see now why procedures are h<strong>and</strong>y?)<br />

Try <strong>to</strong> fill in the do portion of the Mole.Touched block <strong>to</strong> accomplish steps 1-3 above.<br />

mbrace Game Development Workbook Page 45


Does your code look a little bit like this?<br />

Now give your game a try. You should be able <strong>to</strong> run up the score by clicking (this is how you simulate a<br />

<strong>to</strong>uch) on the mole as it pops up on the screen. Is your game <strong>to</strong>o hard or <strong>to</strong>o easy? You can modify the<br />

difficulty by changing the value on the timer.<br />

Let’s add a reset but<strong>to</strong>n that starts the user over at a score of zero. What are the steps <strong>to</strong> add a new<br />

element <strong>to</strong> our game?<br />

1) Add the interface element in <strong>App</strong> Builder<br />

2) Use Blocks Edi<strong>to</strong>r <strong>to</strong> write code <strong>to</strong> implement the behavior of the element.<br />

Here is what my interface looks like after I add a RESET but<strong>to</strong>n.<br />

Above you can see the code that sets the score back <strong>to</strong> zero<br />

when the user clicks the but<strong>to</strong>n. A but<strong>to</strong>n click, is h<strong>and</strong>led just<br />

like a screen <strong>to</strong>uch – you can capture the event using a green<br />

block.<br />

Now you should see the pattern for adding new elements <strong>to</strong> your game. Right now the game is very<br />

basic. What kinds of things could we add <strong>to</strong> make the game more fun? More challenging? Could you<br />

change the game so that it gets harder as the user scores more points? How about changing it so that<br />

the user loses points if they click <strong>and</strong> miss the mole? Think about how you might make some of these<br />

changes. Pick one change (it can be one of the ones listed here, or you can come up <strong>with</strong> your own) <strong>and</strong><br />

add it <strong>to</strong> the game.<br />

Think about how you might figure out how <strong>to</strong> use new blocks or <strong>to</strong> accomplish tasks that we haven’t<br />

covered.<br />

mbrace Game Development Workbook Page 46


Team Project Worksheet<br />

We have learned two different programming <strong>to</strong>ols: <strong>Scratch</strong> <strong>and</strong> <strong>App</strong> Inven<strong>to</strong>r <strong>and</strong> you have had the<br />

chance <strong>to</strong> build a simple game in each. Now it is time for you <strong>to</strong> make a game of your own! You will do<br />

this project in small groups <strong>and</strong> each group will be able <strong>to</strong> choose which platform they would like <strong>to</strong> use<br />

<strong>and</strong> how their game will work. To help plan your game, fill out this worksheet <strong>with</strong> your group.<br />

Group members:<br />

Our game will use: (Circle one) <strong>Scratch</strong> <strong>App</strong> Inven<strong>to</strong>r<br />

Why did you choose this platform?<br />

Our game is called:<br />

Short description of how game is played:<br />

How does someone win your game?<br />

How does someone lose your game?<br />

What elements (timers, sprites, variables, etc) will you need in your game?<br />

mbrace Game Development Workbook Page 47


What resources/media files (images, sounds, etc) will you need for your game?<br />

What do you think will be the main challenges that your group will encounter while working on your<br />

game?<br />

Are there things that you don’t know how <strong>to</strong> do in <strong>Scratch</strong>/<strong>App</strong> Inven<strong>to</strong>r that you will need <strong>to</strong> figure out<br />

<strong>to</strong> build your game? What are they? How will your team learn them?<br />

mbrace Game Development Workbook Page 48

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

Saved successfully!

Ooh no, something went wrong!