Is this the best way to do this?

Hey everyone! I have just started learning Processing and I wanted to know, are the expressions I am using for the parameters (x,y,w,h) in this code the best way to do them, or is there a much more efficient way.

void setup () {
  smooth();
  noStroke();
  size(720, 720);
}
int x=285;
int y=400;
int w=150;
int h=200;
int eheight = 30*h/200;
int extendedy = y+200*h/200;
int footpos = y+230*h/200;
void draw() {
  background(255);
  //legs
  noStroke();
  fill(#898484);
  rect(x+25*w/150, extendedy, 30*w/150, eheight);
  rect(x+95*w/150, extendedy, 30*w/150, eheight);
  //feet
  fill(#5F5C5C);
  rect(x-15*w/150, footpos, 70*w/150, 30*h/200);
  rect(x+95*w/150, footpos, 70*w/150, 30*h/200);
  //head accessories
  fill(255, 0, 0);
  ellipse(x+75*w/150, y-110*h/200, 20*w/150, 30*h/200);//red light
  stroke(0);
  strokeWeight(2);
  line(x+115*w/150, y-75*h/200, x+115*w/150, y-130*h/200);//antennae
  fill(0);
  ellipse(x+115*w/150, y-130*h/200, 10*w/150, 10*h/200);//antennae top
  //neck
  noStroke();
  fill(#898484);
  rect(x+(17.5*w/150), y-(25*h/200), 15*w/150, 50*h/200);//left prong
  rect(x+(67.5*w/150), y-(25*h/200), 15*w/150, 50*h/200);//middle prong
  rect(x+(117.5*w/150), y-(25*h/200), 15*w/150, 50*h/200);//right prong
  //head
  fill(#160255);
  arc(x+75*w/150, y-20*h/200, 180*w/150, 180*h/200, radians(180), radians(360));
  //face
  fill(255);
  ellipse(x+75*w/150, y-70*h/200, 35*w/150, 35*h/200);//big eye
  fill(0);
  ellipse(x+75*w/150, y-70*h/200, 15*w/150, 15*h/200);//pupil
  fill(#787B93);
  ellipse(x+45*w/150, y-50*h/200, 15*w/150, 15*h/200);//button
  ellipse(x+45*w/150, y-90*h/200, 15*w/150, 15*h/200);//button
  ellipse(x+105*w/150, y-70*h/200, 15*w/150, 15*h/200);//button
  //body
  fill(#DBDBDE);
  rect(x, y, w, h);
  //safe handles
  fill(0);
  ellipse(x+75*w/150, y+75*h/200, 50*w/150, 50*h/200);//center of handle
  ellipse(x+75*w/150, y+50*h/200, 25*w/150, 50*h/200);//top handle
  ellipse(x+100*w/150, y+75*h/200, 50*w/150, 25*h/200);//right handle
  ellipse(x+75*w/150, y+100*h/200, 25*w/150, 50*h/200);//left handle
  ellipse(x+50*w/150, y+75*h/200, 50*w/150, 25*h/200);//bottom handle
  //safe outline
  stroke(0);
  strokeWeight(3);
  line(x+15*w/150, y+15*h/200, x+15*w/150, y+175*h/200);
  line(x+15*w/150, y+15*h/200, x+135*w/150, y+15*h/200);                            
  line(x+135*w/150, y+15*h/200, x+135*w/150, y+175*h/200);
  line(x+135*w/150, y+175*h/200, x+15*w/150, y+175*h/200);
  //combination box
  fill(#B9B9B9);
  rect(x+25*w/150, y+130*h/200, 100*w/150, 35*h/200);
  //dials
  strokeWeight(1.5);
  fill(0);
  ellipse(x+45*w/150, y+147*h/200, 25*w/150, 25*h/200);
  ellipse(x+75*w/150, y+147*h/200, 25*w/150, 25*h/200);
  ellipse(x+105*w/150, y+147*h/200, 25*w/150, 25*h/200);
  fill(255);
  strokeWeight(2);
  ellipse(x+45*w/150, y+142*h/200, 5*2/150, 5*h/200);
  ellipse(x+80*w/150, y+153*h/200, 5*2/150, 5*h/200);
  ellipse(x+98*w/150, y+150*h/200, 5*2/150, 5*h/200);
}

void keyPressed() {
  if (key == CODED) {
    //right movement
    if (keyCode == RIGHT) {
      x+=5;
    }
    if (x>540) {
      x=540;
    }
    //left movement
    if (keyCode==LEFT) {
      x-=5;
    }
    if (x<30) {
      x=30;
    }
    //up movement
    if (keyCode==UP) {
      footpos=extendedy+30*eheight/30;
      eheight+=5;
      extendedy-=5;
      y-=5;
    }
    //down movement
    if (keyCode==DOWN) {
      eheight-=5;
      extendedy+=5;
      y+=5;
    }
    //up parameter
    if (y<=130) {
      y=130;
      extendedy=y+200*h/200;
      eheight=300;
    }
    //down parameter
    if (y>=425) {
      y=425;
      extendedy=y+200*h/200;
      eheight=5;
    }
  }
}

Hi @Captiosus
Welcome to the forum.
Really nice and fun sketch!
I think that’s well coded.
Just for the sake of saving computer processing power, you could take the whole code in draw() into an if boolean block that is only set to true when a key is pressed, and set to false when the key released.

1 Like

Hi and welcome.

you code is fine, however as there are lots of values repeated across the sketch, you could store them in variables. This isnt necessarily more efficient but means that if you want to make changes to your code later it won’t be too much trouble.

so every instance of w/150 or h/200 could be replaced by a predeclared variable.

1 Like

Hello,

Cool!

Learning Processing has a simple character called Zoog that they build.
You may get some insight from the examples there:

You can CENTER shapes:

  // Set ellipses and rects to CENTER mode
  ellipseMode(CENTER);
  rectMode(CENTER);

:)

1 Like