09.04.2016 Views

www.ebook777.com

Make_Getting_Started_with_Processing_Second_Edition

Make_Getting_Started_with_Processing_Second_Edition

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.

Robot[] bots; // Declare array of Robot objects<br />

void setup() {<br />

size(720, 480);<br />

PShape robotShape = loadShape("robot2.svg");<br />

// Create the array of Robot objects<br />

bots = new Robot[20];<br />

// Create each object<br />

for (int i = 0; i < bots.length; i++) {<br />

// Create a random x coordinate<br />

float x = random(-40, width-40);<br />

// Assign the y coordinate based on the order<br />

float y = map(i, 0, bots.length, -100, height-200);<br />

bots[i] = new Robot(robotShape, x, y);<br />

}<br />

}<br />

void draw() {<br />

background(0, 153, 204);<br />

// Update and display each bot in the array<br />

for (int i = 0; i < bots.length; i++) {<br />

bots[i].update();<br />

bots[i].display();<br />

}<br />

}<br />

class Robot {<br />

float xpos;<br />

float ypos;<br />

float angle;<br />

PShape botShape;<br />

float yoffset = 0.0;<br />

// Set initial values in constructor<br />

Robot(PShape shape, float tempX, float tempY) {<br />

botShape = shape;<br />

xpos = tempX;<br />

ypos = tempY;<br />

angle = random(0, TWO_PI);<br />

}<br />

// Update the fields<br />

void update() {<br />

angle += 0.05;<br />

yoffset = sin(angle) * 20;<br />

}<br />

Arrays 163

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

Saved successfully!

Ooh no, something went wrong!