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.

yspeed*=-1;<br />

}<br />

}<br />

Within the draw() function of this last sketch, I include very simple collision detection,<br />

used to reverse the direction of the box if it collides with any of the edges of the display<br />

window. Imagine if there were a lot more detection code—it could get pretty confusing<br />

looking. A better way to organize this would be to write a separate block of code (a function)<br />

to check the collisions. Not only will this code be cleaner looking, but the detection<br />

will be in a sense encapsulated (a word I’ll discuss more when I get to OOP). Modularizing<br />

the code like this in a function gives the ability to potentially plug the function into other<br />

programs. Also, once I develop a template for this collision detection, I can develop variations<br />

on it, building a cool detection library—but I’m getting a bit carried way. Here’s the<br />

sketch with the collision detection function added:<br />

int xpos, ypos;<br />

int xspeed=3;<br />

int yspeed=5;<br />

void setup(){<br />

size(400, 400);<br />

}<br />

void draw(){<br />

background(0);<br />

rect(xpos, ypos, 10, 10);<br />

xpos+=xspeed;<br />

ypos+=yspeed;<br />

checkCollisions(xpos, ypos);<br />

}<br />

void checkCollisions(int xp, int yp){<br />

if (xp>=width-10 || xp=width-10 || yp

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

Saved successfully!

Ooh no, something went wrong!