How do I make a line of code one variable?

Hi there, I don’t know if there’s a way to do this but I have a rect() function in my code and I want to know if it’s possible to group the line of code as a variable to make it all move at once. Read more at the bottom;

Here is the code:

// Mouth Variables
int x = 190; // Mouth move 
int dx = 1; // Mouth move 

// Body Variables

// Arm Variables

int ax =180;
int ax1 = 1;

int ax2 = 270;
int ax21 = 1;
void setup () {

  size (400, 400); 
  ellipseMode(CENTER); 
  rectMode(CENTER); 
  smooth();
}

void draw () {

  background (25, 86, 219); 
  textSize (32); 
  text ("A Shaky Robot", 10, 30); 

  // Arms 

  line (180, 270, 60, 270); // Left Arm 
  ax = ax + ax1; 
  ax = +1;

  line (360, 270, 60, 270); // Right Arm 

  // Legs

  line (160, 300, 160, 360); 
  line (237, 300, 237, 360); 

  // Body 

  rect (200, 250, 120, 120);

  // Head 

  strokeWeight (3); 
  stroke (0);
  fill (89, 16, 98);
  rect (200, 150, 200, 120); 

  // Eyes
  strokeWeight (3); 
  stroke (0); 
  fill (92, 151, 198); 
  rect (150, 140, 40, 40); // Left Eye 
  rect (250, 140, 40, 40); // Right Eye
  rect (150, 140, 20, 20); 
  rect (250, 140, 20, 20);

  // Mouth

  rect (200, x, 20, 20); // Mouth 
  x = x + dx;
  if (x > 190) 
    dx = - 1; 
  if (x < 185)
    dx = 1;
}

If you go down to the // Body section, the coordinates of the rect() function is what I want to change simultaneously, so that as one line of code I can make it move left and return right and then move right and return left.

1 Like

Hi RienziDeal,

First of all, can you please format your code?
For that, click the pen icon on the bottom right of your previous post to edit it.
Then select all of your code and hit the following icon </> to format it properly.
Don’t forget to hit ctrl+t on the processing IDE to properly indent everything.

Now regarding your question, the best way to do what you want is to study OOP. It’s a big subject that we can’t answer here in one post. Look for some example and tutorial on the net.

You’ll will need to create classes with the keyword class to create a template for an object.

1 Like

Check these links. They could help:

Bouncy Bubbles / Examples / Processing.org

Objects / Processing.org

Kf

2 Likes