06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

168 <strong>Learning</strong> <strong>Processing</strong><br />

Now let’s see if we can take the “ Rain Game ” idea and break it down in<strong>to</strong> smaller parts. How do we<br />

do this? For one, we can start by thinking of the elements in the game: the raindrops and the catcher.<br />

Secondly, we should think about these elements ’ behaviors. For example, we will need a timing<br />

mechanism so that the drops fall “ every so often ”. We will also need <strong>to</strong> determine when a raindrop is<br />

“ caught. ” Let’s organize these parts more formally.<br />

Part 1. Develop a program with a circle controlled by the mouse. Th is circle will be the usercontrolled<br />

“ rain catcher. ”<br />

Part 2. Write a program <strong>to</strong> test if two circles intersect. Th is will be used <strong>to</strong> determine if the rain<br />

catcher has caught a raindrop.<br />

Part 3. Write a timer program that executes a function every N seconds.<br />

Part 4. Write a program with circles falling from the <strong>to</strong>p of the screen <strong>to</strong> the bot<strong>to</strong>m. Th ese will be<br />

the raindrops.<br />

Parts 1 through 3 are simple and each can be completed in one fell swoop. However, with Part 4,<br />

even though it represents one piece of the larger project, it is complex enough that we will need <strong>to</strong><br />

complete this exact exercise by breaking it down in<strong>to</strong> smaller steps and building it back up.<br />

Exercise 10-2: Take your idea from Exercise 10-1 and write out the individual parts. Try <strong>to</strong><br />

make the parts as simple as possible (almost <strong>to</strong> the point that it seems absurd). If the parts are<br />

<strong>to</strong>o complex, break them down even further.<br />

Sections 10.4 <strong>to</strong> 10.7 will follow the process of Steps 2a, 2b, and 2c (see breakout box on p. 166) for each<br />

individual part. For each part, we will fi rst work out the algorithm in pseudocode, then in actual code,<br />

and fi nish with an object-oriented version. If we do our job correctly, all of the functionality needed will<br />

be built in<strong>to</strong> a class which can then be easily copied in<strong>to</strong> the fi nal project itself when we get <strong>to</strong> Step 3<br />

(integration of all parts).<br />

10.4<br />

Part 1: The Catcher<br />

Th is is the simplest part <strong>to</strong> construct and requires little beyond what we learned in Chapter 3. Having<br />

pseudocode that is only two lines long is a good sign, indicating that this step is small enough <strong>to</strong> handle<br />

and does not need <strong>to</strong> be made in<strong>to</strong> even smaller parts .<br />

Pseudocode:<br />

• Erase background.<br />

• Draw an ellipse at the mouse location.<br />

Translating it in<strong>to</strong> code is easy:<br />

void setup() {<br />

size(400,400);<br />

smooth();<br />

}

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

Saved successfully!

Ooh no, something went wrong!