Locust movement 4
November 5, 2013 § Leave a comment
Slowly making progress in making the coding into what i want it to be like. After restructuring the code i manged to implement a second locust. With a more tidy format of code i can more easily put more images.
Here is the version with 6 locusts in. My next step from here is to re-photograph my locusts so i can code them so they are all unique.
Here is a tidier version of my code. I have highlighted the lines of code for one individual locust, which has to be copied for all additional locust, but with the values changed.
PImage locust1;
PImage locust2;
PImage locust3;
float x1;
float y1; // X1 AND Y1 FOR LOCUST1
float x2;
float y2; // X2 AND Y2 FOR LOCUST2
float x3;
float y3; // X3 AND Y3 FOR LOCUST3
float hr, vr; // Size of images (94×44)void setup() {
size (640,360);
background(255);imageMode(CENTER);
locust1=loadImage(“locust1.png”);
locust2=loadImage(“locust2.png”);
locust3=loadImage(“locust3.png”);hr =94 / 2;
vr = 44 / 2;
noStroke();
x1 = width / 2;
y1 = height / 2;
x2 = width / 2;
y2 = height / 2;x3 = width / 2;
y3 = height / 2;}
void draw() {
// Instead of clearing the background, fade it by drawing
// a semi-transparent rectangle on top
fill(255, 255);
rect(0, 0, width, height);// If the cursor is over the text, change the position
if (abs(mouseX – x1) < hr &&
abs(mouseY – y1) < vr) {
x1 += random(-10, 10);
y1 += random(-10, 10);
}
if (abs(mouseX – x2) < hr &&
abs(mouseY – y2) < vr) {
x2 += random(-10, 10);
y2 += random(-10, 10);
}
if (abs(mouseX – x3) < hr &&
abs(mouseY – y3) < vr) {
x3 += random(-10, 10);
y3 += random(-10, 10);}
fill(0);
image(locust1,x1,y1);
image(locust2,x2,y2);
image(locust3,x3,y3);
}
Leave a comment