Random color multiple times
in
Programming Questions
•
1 year ago
Hi everybody.
I am very new to programming and am learning the basics. I made a block that bounces off of the walls and I wanted to make the color of the block randomly generate every time I clicked the mouse. I did some research and learned how to make the random color, but it will only work for my first click.
Here is my code:
int xspeed = 2;
int yspeed = 1;
float xpos;
float ypos;
float R = random(255);
float G = random (255);
float B = random (255);
void setup() {
fill(255, 150, 0);
size(600, 200);
noStroke();
smooth();
xpos = 20;
ypos = 20;
}
void draw() {
background (0);
xpos = xpos + xspeed;
if (xpos+20 > 600 || xpos-20 < 0) {
xspeed *= -1;
}
ypos = ypos + yspeed;
if (ypos+20 > 200 || ypos-20 < 0) {
yspeed *= -1;
}
rectMode(CENTER);
rect(xpos, ypos, 40, 40);
if (mousePressed) {
fill(R, G, B);
}
}
Any advice about this would be very much appreciated! Thanks!
Grayson
1