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.

mouse trails<br />

if (isTrailable){<br />

fill(150, 16);<br />

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

}<br />

else {<br />

background(150);<br />

}<br />

The if...else statement paints either a semi-opaque layer or an opaque layer, based on<br />

the state of the isTrailable Boolean variable. Clicking the trails check box button controls<br />

whether the variable is true or false. Within the draw() function, the drawing of<br />

each of the buttons is h<strong>and</strong>led with the following code:<br />

stroke(100);<br />

//btn 1 - decreases speed<br />

fill(btn1Background);<br />

rect(btn1X, btn1Y, btn1W, btn1H);<br />

fill(75);<br />

text("speed -", btn1X-17, btn1Y+btn1H/4);<br />

//btn2 - increases speed<br />

fill(btn2Background);<br />

rect(btn2X, btn2Y, btn2W, btn2H);<br />

fill(75);<br />

text("speed +", btn2X-17, btn2Y+btn2H/4);<br />

//btn3 - check box controls mouse trails<br />

fill(255);<br />

rect(btn3X, btn3Y, btn3W, btn3H);<br />

fill(75);<br />

text("trails", btn3X+10, btn3Y+btn3H/4);<br />

if (isTrailsSelected){<br />

// draw x in box<br />

line(btn3X-btn3W/2, btn3Y-btn3H/2, btn3X+btn3W/2, btn3Y+btn3H/2);<br />

line(btn3X-btn3W/2, btn3Y+btn3H/2, btn3X+btn3W/2, btn3Y-btn3H/2);<br />

}<br />

The last snippet in the preceding code controls whether the check box is filled or empty,<br />

based on another Boolean variable, isTrailsSelected.<br />

Each time the square exits the screen, either right or left, it is mapped back to the opposite<br />

side so that it will keep moving. Also, just for some variety, the color <strong>and</strong> y position of<br />

the box change r<strong>and</strong>omly:<br />

if (x>width){<br />

x = 0;<br />

y = r<strong>and</strong>om(h/2, 70);<br />

movingSquareBackground = color(r<strong>and</strong>om(255),➥<br />

INTERACTIVITY<br />

587<br />

12

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

Saved successfully!

Ooh no, something went wrong!