04.04.2013 Views

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

Processing: Creative Coding and Computational Art

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Hopefully, you were able to successfully run this sketch. I created an alternative rendering<br />

option as well, which you can mess with in a bit. When you run the sketch, you should see<br />

many (actually 500) white balls moving r<strong>and</strong>omly around, bouncing off the edges of the<br />

window. This code is not simple, so don’t feel bad if it is a struggle to underst<strong>and</strong> it. You’re<br />

going to be doing a lot more of this type of coding beginning in Chapter 6—so this is really<br />

more like a teaser than something you need to fully grasp at this point. I just thought it<br />

was good to show you something a little more interesting after all the hard work you’ve<br />

been putting in. The main concept I want to reinforce at this point is the power of arrays<br />

<strong>and</strong> loops. Let’s look at the program in sections:<br />

// global variables<br />

int ballCount = 500;<br />

int ballSize = 8;<br />

int ballSpeed = 3;<br />

float[]xspeed = new float[ballCount];<br />

float[]yspeed= new float[ballCount];<br />

float[]xpos = new float[ballCount];<br />

float[]ypos = new float[ballCount];<br />

float[]wdth = new float[ballCount];<br />

float[]ht = new float[ballCount];<br />

After my comments, I declare <strong>and</strong> initialize some global variables; these variables will be<br />

accessible throughout the entire program. I chose the specific data type, in this case int or<br />

float, based on the type of value that would need to be stored in the respective<br />

variables/arrays. Obviously, ballCount could be an integer value, but a speed value would<br />

need to be a float value. The arrays are initialized with enough memory to hold the total<br />

number of balls. Remember, I can’t change the arrays’ sizes once they’re initialized (in<br />

truth, you’ll see later that there are indeed functions to do such a thing), so it’s important<br />

to make them big enough at the beginning. Although the arrays are initialized to each hold<br />

500 values (based on ballCount), there is still nothing in them.<br />

//initialize sketch<br />

void setup(){<br />

//set sketch window size <strong>and</strong> background color<br />

size(400, 400);<br />

background(0);<br />

//initialize values for all balls<br />

for (int i=0; i

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

Saved successfully!

Ooh no, something went wrong!