Beth - K

By Elizabeth
//Declare Bug b1; Bug b2; void setup() { size(400, 400); smooth(); randomSeed(5); b1 = new Bug(); //construct b2 = new Bug(); b1.assignValues(width * 0.5, height/2, b2, 1, 1, "bug1.svg", true); b2.assignValues(width * 0.25, height/2, b1, 3.0, 3.0, "bug2.svg", false); } void draw() { background(45, 1, 67); b1.move(); b1.display(); b2.move(); b2.display(); } class Bug { PShape bee; float x; float y; float speedX = 1.0; float speedY = 0.4; float widthBug; float heightBug; int directionX = 1; int directionY = -1; boolean shake = true; Bug other; Bug() { } void assignValues(float centerx, float centery, Bug otherbug, float sx, float sy, String name, boolean bugShake) { x = centerx; y = centery; speedX = sx; speedY = sy; other = otherbug; bee = loadShape(name); widthBug = bee.getWidth(); heightBug = bee.getHeight(); shake = bugShake; } void move() { x += speedX * directionX; y += speedY * directionY; if (shake == true) { x = x + speedX * random(-5, 5); y = y + speedY * random(-5, 5); } if ((x > width-widthBug) || (x < 0)) { directionX = -directionX; } if ((y> height-heightBug) || (y < 0)) { directionY = -directionY; } x = constrain (x, 0, width); y = constrain (y, 0, height); } void display() { shape (bee, x, y); } }

Post a Comment

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

*
*