Lab 9 – Click-and-Blow-Up (Functions, if/else, event

advertisement

Lab 9 – Click-and-Blow-Up

(Functions, if/else, event-driven program)

In this lab you will be creating your own program that, when you click on an individual object, will blow it up. If, however, the object has already been blown up, then you will want to change it back to being alive (you have magic powers). So let’s think this through. You will need to create a world with a bunch of people and animals in it. You will need to create a basic method for blowing something up. You will then need to change that method into a function. Once you have your function, you’ll want to create an event such that when you click on something, the function is called with the object you clicked on.

Finally, you will want to add a conditional statement to your function to check whether something has already been blown up or not, and, if it has, then make it come back to life. In our world, an object that is dead is black, and an object that is alive is its original color (the “no color” option).

1.

So let’s start by creating a world with a bunch of people and animals. I happened to include an

Eskimo in my world, but you don’t have to.

2.

Once you are happy with your world, let’s write a method to blow up one of the objects in your world. Be as creative as you want in how you would like your object to blow up. The only

absolute is that the blowup method should turn the object black to indicate that it is dead. a.

Here is the code I used to blow up my Eskimo. Feel free to be creative and vary it.

3.

Now change your method into a function by creating a parameter and dragging it over each object. a.

Note: you can’t change the sound – it is associated with an individual object. I didn’t mind leaving the sound be the same for each object clicked on.

4.

We now want to make this program event-driven. Create a new event that happens when the mouse clicks on an object, and make sure the blowup function is called with the object under your mouse.

5.

Hit play and test out your program by clicking on some of the people in your world. Do you like it so far? If not, modify your function until you have something you like.

Try clicking on a person who’s already been blown up. What happens? Is this what we want?

6.

We’re going to want to use a conditional control statement (if/else) to check whether the object is dead (black) or alive (no color).

7.

Drag the if/else statement to the top of your blowup function.

8.

Make the condition check if the object is dead.

9.

Now add code to make your object come back to life. You can make the object come back to life any way you want. The only absolute is that you must change the object’s color back to “no color”.

10.

Under the else statement, we want the instructions that tell an object how to die.

11.

Try playing your program again. See if it is doing what you want it to do. If not, change it to make it do as you wish.

What to Turn in for Lab 11:

1.

Kangaroo world

2.

Blow up world

Extra Credit 1 (8 pts):

Add 2 inanimate objects to your blow-up world. If you play your program now, you can click on those inanimate objects and they’ll blow up and die just like your people and animals. Modify your function so that if you click on the inanimate objects, nothing happens. (Hint: this is just another if/else statement. The expression “!=” is like saying “is not equal to”.)

Download