Charlene - K

By Charlene
/* The purple creature is chasing the pink creature. When the pink creature gets into the middle of purple creature, the purple creature becomes pink. */ Creature c1; // Declare the object Creature c2; void setup() { size(400, 400); smooth(); randomSeed(3); c1 = new Creature(); c2 = new Creature(); c1.assignValues(width * 0.45, height/2, 100, c2); c2.assignValues(width * 0.55, height/2, 25, c1); } void draw() { fill(0, 20); noStroke(); rect(0, 0, width, height); fill(214, 45, 93, 50); // Color of Creature 2 c2.move(); c2.display(); noFill(); strokeWeight(5); stroke(153, 101, 177, 200); // Color of Creature 1 c1.move(); c1.display(); } class Creature { float x, y; float diameter = 50; float speed = 0.0; int direction = 1; Creature other; Creature() { } void assignValues(float xpos, float ypos, float dia, Creature otherCreat) { x = xpos; y = ypos; diameter = dia; other = otherCreat; } void move() { y += (speed * direction); if (y > 400) { direction *= -1; } else if (y < 0) { direction *= -1; } x = x + random(-speed, speed); y = y + random(-speed, speed); x = constrain (x, 0, width); y = constrain (y, 0, height); if (dist(x, y, other.x, other.y) < (diameter/2 - other.diameter/2)) { speed = 0.001; stroke(214, 45, 93, 50); } else { speed = 4.0; } } void display() { ellipse(x, y, diameter, diameter); } }

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*