make triangles stick to mouse
in
Programming Questions
•
1 year ago
hey guys i really, really need help, i'm new to coding so have no idea what i'm doing :(
what i want to do with my code now is make the bigger, darker green triangles stick to the cursor one by one when the mouse comes in contact with them
here is my code so far,
thanks for the help, much appretiated:)
int tri_count = 20;
float[]xp2=new float[tri_count];
float[]yp2=new float[tri_count];
float[]speed2=new float [tri_count];
float[] xp = new float[tri_count];
float[] yp = new float[tri_count];
float[] speed = new float[tri_count];
int i = 0;
void setup(){
size(500,500);
int i = 0;
while( i < tri_count){
xp2[i]=random (0,width);
yp2[i]=random (0,width);
speed2[i]=random (0.1,2);
xp[i] = random(0,width);
yp[i] = random(0,height);
speed[i] = random(0.1,5);
i = i + 1;
}
}
void draw(){
background(255);
strokeWeight(0.5);
update_main_layer();
gfx2();
gfx();
update_background();
}
void gfx(){
fill (17,192,16,100);
/* stops code and symbol below starts it
*/
noStroke();
int i = 0;
int angle = 0;
while( i < tri_count){
pushMatrix();
translate(xp[i],yp[i]);
rotate(angle);
triangle(xp[i]-15,yp[i]+15,xp[i],yp[i],xp[i]+15,yp[i]+15);
popMatrix();
i = i + 1;
angle = angle+1;
}
}
void update_background(){
int i = 0;
while( i < tri_count){
yp[i] = yp[i] + speed[i];
if(yp[i] >width){
yp[i] = -40;
}
i = i + 1;
}
}
void gfx2(){
fill (34,144,9);
noStroke();
int i=0;
int angle=0;
while( i< tri_count) {
pushMatrix();
translate(xp2[i],yp2[i]);
rotate(angle);
triangle(xp2[i]-30,yp2[i]+30,xp2[i],yp2[i],xp2[i]+30,yp2[i]+30);
popMatrix();
i = i + 1;
angle = angle+1;
}
}
void update_main_layer(){
int i = 0;
while( i < tri_count){
yp2[i] = yp2[i] + speed2[i];
if(yp2[i] >width){
yp2[i] = -40;
}
i = i + 1;
}
}
1