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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

PROCESSING: CREATIVE CODING AND COMPUTATIONAL ART<br />

604<br />

color rectBackground = color(0, 0, 255);<br />

void setup(){<br />

size(400, 400);<br />

background(255);<br />

rectMode(CENTER);<br />

}<br />

void draw(){<br />

fill(rectBackground);<br />

rect(width/2, height/2, 200, 200);<br />

}<br />

void keyPressed(){<br />

if(key == CODED){<br />

if (keyCode == UP) {<br />

rectBackground = color(255, 0, 0);<br />

}<br />

else if (keyCode == DOWN) {<br />

rectBackground = color(255, 255, 0);<br />

}<br />

else if (keyCode == RIGHT) {<br />

rectBackground = color(0, 255, 0);<br />

}<br />

else {<br />

rectBackground = color(255, 0, 255);<br />

}<br />

}<br />

}<br />

The keyCode property is used to check special keys on the keyboard, such as the arrow<br />

keys. <strong>Processing</strong> defines these keys with constants (that’s why the arrow direction names<br />

are in all caps). Before checking the individual key codes, you need to check if the key is<br />

coded at all; this is h<strong>and</strong>led by the external conditional if(key == CODED){}. For an up-todate<br />

list of the available key codes in <strong>Processing</strong>, please refer to the online reference at<br />

http://processing.org/reference/keyCode.html.<br />

In addition to key codes, it’s also possible to detect the value of each st<strong>and</strong>ard key. The<br />

next sketch is based on the last example, replacing the arrow keys with the keys A, S, W,<br />

<strong>and</strong> Z, for left, right, up, <strong>and</strong> down, respectively.<br />

color rectBackground = color(0, 0, 255);<br />

void setup(){<br />

size(400, 400);<br />

background(255);<br />

rectMode(CENTER);<br />

}<br />

void draw(){<br />

fill(rectBackground);<br />

rect(width/2, height/2, 200, 200);<br />

}<br />

void keyPressed(){<br />

if(key == 'a'){

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

Saved successfully!

Ooh no, something went wrong!