Howdy! This is my first post.
I created this program that generates grids with colored squares. The colors are randomized. Each is a new object that I create with mousePressed(). When they are created they are get placed in (mouseX,MouseY).
My problem is, that I want them to move, that is, as they are (I don’t want their colors to change every frame!). Instead, when they are created, they just get stuck where they were born. I am hoping to find a way to store them in an arrayList as completed after rendering so that then I can call them in Draw and have them move randomly about the screen by random increments along the X and Y axes.
int mouseCount = 0;
float x = 555;
float y = 555;
float xOffset = 0;
float yOffset = 0;
float xpos = 0;
float ypos = 0;
ArrayList<Animal> animals;
void setup() {
size(894, 894);
frameRate(60);
animals = new ArrayList<Animal>();
}
void draw() {
//I DON'T KNOW WHAT TO WRITE HERE
}
//Add a new animal
void mousePressed() {
animals.add(new Animal(mouseX, mouseY));
Animal a = animals.get(mouseCount);
a.render();
mouseCount ++;
}
class Animal {
float x = 0;
float y = 0;
float xOffset = 0;
float yOffset = 0;
int[] genotype = new int[24];
Animal(float tempX, float tempY) {
x = tempX;
y = tempY;
}
void render() {
//Randomizes all genes
for (int i = 0; i < this.genotype.length; i++) {
this.genotype[i] = int(random(5));
}
for (int i = 0; i < this.genotype.length; i++) {
if (this.genotype[i] == 0) {
fill(0, 0, 0, 0.0);
}
if (this.genotype[i] == 1) {
fill(255, 0, 0);
}
if (this.genotype[i] == 2) {
fill(0, 0, 255);
}
if (this.genotype[i] == 3) {
fill(255, 255, 255);
}
if (this.genotype[i] == 4) {
fill(255, 255, 0);
}
rect(x + xOffset, y + yOffset, 20, 20);
xOffset +=20;
if (i == 3 || i == 7 || i == 11 || i == 15 || i == 19) {
xOffset -=80;
yOffset +=20;
}
}
}
}