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.

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

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

Saved successfully!

Ooh no, something went wrong!