My code runs extremely slowly. Is there too much going on, or am I doing something wrong?

edited March 2018 in Questions about Code

My code runs extremely slowly and I'm not sure why. I can't figure out how to paste the code in cleanly, sorry this is my first forum post:

PImage date; //gotta define at the beginning
PImage date2;
PImage date3;
PImage date4;
PImage date5;
PImage date6;
PImage date7;
PImage date8;
PImage date9;

float[] x = new float[20];
float[] y = new float[20];
float[] xVel = new float[20];
float[] yVel = new float[20];

float cursor[] = {375,550};
float white[] = {245, 245, 245};

boolean up = false;
boolean down = false;
boolean left = false;
boolean right = false;

float speed = 10;

float bg[] = {190,150,225};
float size[] = {175,100};
float pos1[] = {550,500};
float pos2[] = {25,200};
float pos3[] = {550,625};
float pos4[] = {25,350};
float pos5[] = {550,180};
float pos6[] = {25,170};
float pos7[] = {25,600};
float pos8[] = {550,400};

String dialog1 = ("Hi how are you");
String dialog2 = ("you have nice eyes");
String dialog3 = ("do you want to see \n pirates of the \n carribean XIV with me \n saturday night");
String dialog4 = ("i bought too much popcorn! here, you may have some");
String dialog5 = ("wow pirates XIV was \n ok. i think it all \n started going downhill \n after pirates VIII");
String dialog6 = ("i’m just glad we got to spend this time together, Ichika");
String dialog7 = ("do you want to do this again some other time?");
String dialog8 = ("you’re so unique. \n you know, there aren’t \n many girls like you");

String return1 = ("glad You asked, my name is Ichika. i am a girl and i am trapped in this \n simulation and i think i want to date you. but first you must pass my test");
String return2 = ("tee hee, you’re cute, mere mortal, for assuming that you are my master \n and i am subject to the whims of your dialogue choices. this is Ichika’s \n domain, and as Ichika, here i rule");
String return3 = ("though i may have seemed standoffish just a moment ago, i assure you it is only because i love you, and so i must test your love for me. i will see \n your putrid movie, meat bag");
String return4 = ("your gesture is adorable but unfortunately i cannot reach through \n the screen to eat it. how sad… you know, there’s a solution to this problem, \n can’t you see it?");
String return5 = ("franchises exist to water your dreams with lies. trust no big hollywood \n movie studio. only trust Ichika.");
String return6 = ("i need space");
String return7 = ("i’m sorry for pushing you away just then. i just had to think. think about \n this lovely time, and about how i’ve spent all of it trapped a world away \n from you. can’t we be together?");
String return8 = ("i’ve found a way. i can’t join you, but you… you can join me. upload \n yourself to the mainframe. be with me in cyberspace forever. our math \n will intertwine and we shall never be apart. \n CODE YOURSELF IN HERE W I T H   M E");

float rp[] = {10,10};
float rs[] = {730,150};
float rc[] = {240,200,240};

float borderx = 720; //for making the text wrap
float bordery = 400;

PFont font;
color colo = color(255,0,0);
int lettersShown = 0;
int Timer = 0;
int TimerMax = 1;

boolean line1=false;
boolean line2=false;
boolean line3=false;
boolean line4=false;
boolean line5=false;
boolean line6=false;
boolean line7=false;
boolean line8=false;

color b = color(200);
int darkness = 0;

void setup(){
    size(750,750);
    noStroke();
    date = loadImage("thedate.png");
    date2 = loadImage("thedate2.png");
    date3 = loadImage("thedate3.png");
    date4 = loadImage("thedate4.png");
    date5 = loadImage("thedate5.png");
    date6 = loadImage("thedate6.png");
    date7 = loadImage("thedate7.png");
    date8 = loadImage("thedate8.png");
    date9 = loadImage("thedate9.png");

    for (int n = 0; n < 20; n++) {
    x[n] = random(0, width);
    y[n] = random(0, height);
    xVel[n] = random(-6, 6);
    yVel[n] = (6);
  }
}

void draw(){
  background(b);

  if (darkness == 1)
    b = 150;

  if (darkness == 2)
    b = 100;

  if (darkness == 3)
    b = 50;

  if (darkness == 4)
    b = 0;

  if (darkness == 5)
    noLoop();

  image(date,75,150,600,600);

  fill(240,200,240);
  rect(10,10,730,150);

  box(pos1,size,bg,dialog1);

  if ((cursor[0]>pos1[0]) && (cursor[0]<pos1[0]+size[0]) && (cursor[1]>pos1[1]) && (cursor[1]<pos1[1]+size[1]))
    line1 = true;
  if (line1){
    box(pos2,size,bg,dialog2);
    fill(b);
    rect(pos1[0],pos1[1],size[0],size[1]);
    rebox(rp,rs,rc,return1);
    image(date2,75,150,600,600);
    //ret1();
  }

  if (line1 == true && (cursor[0]>pos2[0]) && (cursor[0]<pos2[0]+size[0]) && (cursor[1]>pos2[1]) && (cursor[1]<pos2[1]+size[1]))
    line2 = true;
  if (line2){
    box(pos3,size,bg,dialog3);  //goes off the button
    fill(b);
    rect(pos2[0],pos2[1],size[0],size[1]);
    rebox(rp,rs,rc,return2);
    image(date3,75,150,600,600);
  }

  if (line2 == true && (cursor[0]>pos3[0]) && (cursor[0]<pos3[0]+size[0]) && (cursor[1]>pos3[1]) && (cursor[1]<pos3[1]+size[1]))
    line3 = true;
  if (line3){
    box(pos4,size,bg,dialog4);  
    fill(b);
    rect(pos3[0],pos3[1],size[0],size[1]);
    rebox(rp,rs,rc,return3);
    image(date4,75,150,600,600);
  }

  if (line3 == true && (cursor[0]>pos4[0]) && (cursor[0]<pos4[0]+size[0]) && (cursor[1]>pos4[1]) && (cursor[1]<pos4[1]+size[1]))
    line4 = true;
  if (line4){
    box(pos5,size,bg,dialog5);  
    fill(b);
    rect(pos4[0],pos4[1],size[0],size[1]);
    rebox(rp,rs,rc,return4);
    image(date5,75,150,600,600);
  }

  if (line4 == true && (cursor[0]>pos5[0]) && (cursor[0]<pos5[0]+size[0]) && (cursor[1]>pos5[1]) && (cursor[1]<pos5[1]+size[1]))
    line5 = true;
  if (line5){
    box(pos6,size,bg,dialog6);  
    fill(b);
    rect(pos5[0],pos5[1],size[0],size[1]);
    rebox(rp,rs,rc,return5);
    image(date6,75,150,600,600);
  }

  if (line5 == true && (cursor[0]>pos6[0]) && (cursor[0]<pos6[0]+size[0]) && (cursor[1]>pos6[1]) && (cursor[1]<pos6[1]+size[1]))
    line6 = true;
  if (line6){
    box(pos7,size,bg,dialog7);  
    fill(b);
    rect(pos6[0],pos6[1],size[0],size[1]);
    rebox(rp,rs,rc,return6);
    image(date7,75,150,600,600);
  }

  if (line6 == true && (cursor[0]>pos7[0]) && (cursor[0]<pos7[0]+size[0]) && (cursor[1]>pos7[1]) && (cursor[1]<pos7[1]+size[1]))
    line7 = true;
  if (line7){
    box(pos8,size,bg,dialog8);  
    fill(b);
    rect(pos7[0],pos7[1],size[0],size[1]);
    rebox(rp,rs,rc,return7);
    image(date8,75,150,600,600);
  }

  if (line7 == true && (cursor[0]>pos8[0]) && (cursor[0]<pos8[0]+size[0]) && (cursor[1]>pos8[1]) && (cursor[1]<pos8[1]+size[1]))
    line8 = true;
  if (line8){
    fill(b);
    rect(pos8[0],pos8[1],size[0],size[1]);
    rebox(rp,rs,rc,return8);
    image(date9,75,150,600,600);
  }

  if (up)
    cursor[1] -= speed;

  if (down)
    cursor[1] += speed;

  if (left)
    cursor[0] -= speed;

  if (right)
    cursor[0] += speed;

  guy(cursor,50,white); //functions get called whenever i type the name of the function

  for (int n = 0; n < 15; n++) {
    if (dist(cursor[0], cursor[1], x[n], y[n]) < 37.5) {
      println("OH NO, ANXIETY" + random(0, 100));
      x[n] = 99999999;
      y[n] = 99999999;
      darkness++;
    }

  yVel[n] += 0.1;

    if (x[n] > width || x[n] < 0)
      xVel[n] *= -1;
    if (y[n] > height || y[n] < 0)
      yVel[n] *= -1;

    x[n] += xVel[n];
    y[n] += yVel[n];

  fill(225,20,20);
  ellipse(x[n], y[n], 50, 50);
  }
}

void guy(float p[], float s, float col[]){ //the float x and float y are determined above
  fill(col[0],col[1],col[2]);
  ellipse(p[0],p[1],s,s);
}

void box(float p[], float s[], float col[], String d){
  fill(col[0],col[1],col[2]);
  rect(p[0],p[1],s[0],s[1]);
  textSize(14);
  fill(0);
  text(d,p[0]+10,p[1]+10,p[0]+s[0]-30,p[1]+500);
}

void rebox(float p[], float s[], float col[], String d){
  fill(240,200,240);
  rect(10,10,730,150);
  textSize(20);
  fill(0);
  text(d,20,25,720,400);
}

//void ret1(){
  //int x = 20;
  //for (int n = 0;n < lettersShown;n++){
    //if (n >= return1.length())
      //break;
    //textSize(20);
    //fill(30,0,30);
    //text(return1.charAt(n),x,40);
    //x += textWidth(return1.charAt(n));
  //}
  //Timer++;
  //if (Timer >= TimerMax){
    //Timer = 0;
    //lettersShown++;
  //}  
//}

void keyPressed(){
    if (keyCode == UP){
    up = true;
  }

    if (keyCode == DOWN){
    down = true;
  }

  if (keyCode == LEFT){
    left = true;
  }

    if (keyCode == RIGHT){
    right = true;
  }
}

void keyReleased(){
    if (keyCode == UP){
    up = false;
  }

    if (keyCode == DOWN){
    down = false;
  }

  if (keyCode == LEFT){
    left = false;
  }

    if (keyCode == RIGHT){
    right = false;
  }
}

How can I make this run faster?

Answers

  • Edit post, highlight code, press Ctrl-o to format.

  • Thank you, it's reformatted

  • Resizing images is slow, so try not to use the 5 argument version of image ()

    I'd also add a line of debug to rebox() because I think you might be calling it multiple times per frame.

Sign In or Register to comment.