How to fix a suuuper slow sketch?

He guys,

I made this sketch which is an animation with animals. Somehow it got really slow, and i know line 199 till 203 makes it slow down but i dont know how to fix it. what can i change to make it go normal speed.
All the different images of animals are moving very slow.

1 other question, the images are now starting by 1 image per animal, and then increasing by 10 or something like that. I think it has something to do with the framecount, (line 113) how can i let them increase with less animals at the time. I want them to double so starting with 1 then 2 then 4 etc.

Sorry im just starting with processing so not really know what im doing exactly jet.

int _LEFT = -1;
int _RIGHT = 1;
int _DOWN = -1;
int _UP = 1;

int imax = 9;
PImage[] images = new PImage[imax];
int im=0; 
long startT, totalT = 3000;

class An {
  String make; // FIELD
  float x;
  float y;
  int dirX = _RIGHT;
  int dirY = _UP;
  float speedX;
  float speedY;
  color anColor;
  PImage img;
  PImage foodImg;
  PImage petImg;
  Boolean isFood;
}

boolean teleport = true;

ArrayList<An> animals = new ArrayList<An>();

PImage cow_image; 
PImage cow_image_food;

PImage chicken_image;
PImage chicken_image_food;

PImage varken_image;
PImage varken_image_food;

PImage gans_image;
PImage gans_image_food;

PImage konijn_image;
PImage konijn_image_food;

PImage paard_image;
PImage paard_image_food;

PImage schaap_image;
PImage schaap_image_food;

PImage hond_image;
PImage hond_image_food;

PImage kat_image;
PImage kat_image_food;

PImage meatpet;

float animal_scale_start = 1;
float animal_scale_end = 0.1;
int n_frames = 60 * 30;

PFont font;

void setup() {
  size(displayWidth, displayHeight, P2D);

  for (int k = 0; k < imax; k++) images[k] = loadImage("image"+k+".png");

  cow_image = loadImage("koe.png");  
  cow_image_food = loadImage("Cow.png");
  chicken_image = loadImage("Kip.png");
  chicken_image_food = loadImage("kipvlees.png");
  varken_image = loadImage("varken.png");
  varken_image_food = loadImage("varkenvlees.png");
  gans_image = loadImage("gans.png");
  gans_image_food = loadImage("gansvlees.png");
  konijn_image = loadImage("konijn.png");
  konijn_image_food = loadImage("konijnvlees.png");
  paard_image = loadImage("paard.png");
  paard_image_food = loadImage("paardvlees.png");
  schaap_image = loadImage("schaap.png");
  schaap_image_food = loadImage("schaapvlees.png");
  hond_image = loadImage("hond.png");
  kat_image = loadImage("kat.png");

  meatpet = loadImage("pets and meat.png");

  for (int i = 0; i < 1; i++) {
    create_cow();
    create_chicken();
    create_varken();
    create_gans();
    create_konijn();
    create_paard();
    create_schaap();
    create_hond(); 
    create_kat();
  }
}

void draw() {

  background(190, 208, 208);
  fill(64, 2, 204);
  rect(0, 0, width/2, height); 
  pushMatrix();
  translate(width/2, height/2);
  image(meatpet, -15, -80); 
  popMatrix();


  if (frameCount %100 == 0) {  //THIS MIGHT BE IT
    for (int i = 0; i < frameCount; i++) {
      create_cow(); 
      create_chicken();
      create_varken();
      create_gans();
      create_konijn();
      create_paard();
      create_schaap();
      create_hond(); 
      create_kat();
    }
  }

  for (int i = 0; i < animals.size(); i++) {
    An a = animals.get(i);
    //
    // move
    //
    a.x += a.dirX * a.speedX;
    a.y += a.dirY * a.speedY;
    if (a.x<width/2) {
      if (a.isFood==false)a.img=a.foodImg;
      a.isFood=true;
    } else {
      if (a.isFood==true)a.img=a.petImg;
      a.isFood=false;
    }
    //a.x += noise(a.x);
    //a.y += noise(a.y);

    // x
    if (random(1) < 0.1) {
      a.dirX = random(1) < 0.5 ? _LEFT : _RIGHT;
      a.speedX = random(0, 3);
    }
    // y
    if (random(1) < 0.05) {
      a.dirY = random(1) < 0.5 ? _UP : _DOWN;
      a.speedY = random(0, 3);
    }


    if (teleport) {
      if (a.x > width) {
        a.x = 0;
      }
      if (a.y > height) {
        a.y = 0;
      }
      if (a.x < 0) {
        a.x = width;
      }
      if (a.y < 0) {
        a.y = height;
      }
    } else { // bounce
      if (a.x > width) {
        a.dirX = -a.dirX;
      }
      if (a.y > height) {
        a.dirY = -a.dirY;
      }
      if (a.x < 0) {
        a.dirX = -a.dirX;
      }
      if (a.y < 0) {
        a.dirY = -a.dirY;
      }
    }

    //
    // draw
    //
    imageMode(CENTER);
    pushMatrix();
    translate(a.x, a.y);
    scale(a.dirX, 1);

    float zoom_scale = map(frameCount, 0, n_frames, animal_scale_start, animal_scale_end);
    scale(zoom_scale);

    image(a.img, 0, 0);
    popMatrix();


    pushMatrix();
    translate(width/2, height/2);
    image(images[im], 0, 350);
    myTimer();
    popMatrix();
  }
}

An create_an(PImage petimg, PImage foodimg) {
  An an = new An();
  an.x = random(width);
  an.y = random(height);
  an.dirX = random(1) < 0.5 ? _LEFT : _RIGHT;
  an.dirY = random(1) < 0.5 ? _UP : _DOWN;
  an.speedX = random(2);
  an.speedY = random(2);
  an.img = petimg;
  an.foodImg = foodimg;
  an.petImg = petimg;
  an.isFood=false;
  return an;
}

An create_cow() {
  An cow = create_an(cow_image, cow_image_food); 
  animals.add(cow);
  return cow;
}

An create_chicken() {
  An chicken = create_an(chicken_image, chicken_image_food); 
  animals.add(chicken);
  return chicken;
}

An create_varken() {
  An varken = create_an(varken_image, varken_image_food); 
  animals.add(varken);
  return varken;
}

An create_gans() {
  An gans = create_an(gans_image, gans_image_food); 
  animals.add(gans);
  return gans;
}

An create_konijn() {
  An konijn = create_an(konijn_image, konijn_image_food); 
  animals.add(konijn);
  return konijn;
}

An create_paard() {
  An paard = create_an(paard_image, paard_image_food); 
  animals.add(paard);
  return paard;
}

An create_schaap() {
  An schaap = create_an(schaap_image, schaap_image_food); 
  animals.add(schaap);
  return schaap;
}

An create_hond() {
  An hond = create_an(hond_image, chicken_image_food); 
  animals.add(hond);
  return hond;
}

An create_kat() {
  An kat = create_an(kat_image, chicken_image_food); 
  animals.add(kat);
  return kat;
}

void myTimer() {
  if ( millis() > startT + totalT ) {
    startT = millis();
    im++;
    if (im>=imax)  im=0;
  }
}

Well, can you explain what this code should be supposed to do?

 if (frameCount %100 == 0) {  //THIS MIGHT BE IT
    for (int i = 0; i < frameCount; i++) {
    ...

Because what you do here is the following: Every 100th frame, you create a new animal of each type, 0 to frameCount times. So the first time you create 100 animals, then 200, 300, 400, 500 and so on. This seems not as intended, or is it?

I did not look too much into your code, but I believe with removing this it already gets faster.

1 Like