# Rectangle not moving

**URL:** https://discourse.processing.org/t/rectangle-not-moving/24161
**Category:** Coding Questions
**Created:** [September 27, 2020, 5:39pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161 "2020-09-27T17:39:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@Tobias](https://discourse.processing.org/u/Tobias)
#### Post date: [September 27, 2020, 5:39pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/1 "2020-09-27T17:39:25Z")

</div>

Hi.

I am new to processing and I am trying to make Pong. I want both the paddles to be able to move at once. Since keyPressed only allows for one button press at a time, i tried to add a boolean for each button press in hope that it would solve the problem, but now the paddles wont even move. Can someone help me get both of the paddles moving at once using two button presses at once?

Here is my code:

Paddle p1;  
Paddle p2;

void setup(){  
size(600, 600);

p1 = new Paddle(20);  
p2 = new Paddle(width - 30);  
}

void draw(){  
background(0);

p1.show1();  
p1.move();

p2.show2();  
p2.move();  
}

class Paddle{  
float x;  
float y1 = height/2 - 50;  
float y2 = height/2 - 50;  
boolean wp = false;  
boolean sp = false;  
boolean Up = false;  
boolean Dp = false;

Paddle(float tempX){  
x = tempX;  
}

void show1(){  
stroke(255);  
fill(255);  
rect(x, y1, 10, 100);  
}

void show2(){  
stroke(255);  
fill(255);  
rect(x, y2, 10, 100);  
}

void move(){  
if(wp == true){  
y1 -= 4;  
}  
if(sp == true){  
y1 +=4;  
}  
if(Up == true){  
y2 -= 4;  
}  
if(Dp == true){  
y2 += 4;  
}  
}

void keyPressed(){  
if(key == ‘w’){  
wp = true;  
}  
if(key == ‘s’){  
sp = true;  
}  
if(keyCode == UP){  
Up = true;  
}  
if(keyCode == DOWN){  
Dp = true;  
}  
}

void keyReleased(){  
if(key == ‘w’){  
wp = false;  
}  
if(key == ‘s’){  
sp = false;  
}  
if(keyCode == UP){  
Up = false;  
}  
if(keyCode == DOWN){  
Dp = false;  
}  
}  
}

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 27, 2020, 5:48pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/2 "2020-09-27T17:48:37Z")

</div>

keyPressed and keyReleased won‘t be called inside the class

Move it outside the class

say p1.wp etc. to adress the variables inside the class

---

<div class="post-metadata">

### Author: ![Tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@Tobias](https://discourse.processing.org/u/Tobias)
#### Post date: [September 27, 2020, 5:51pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/3 "2020-09-27T17:51:23Z")

</div>

Thanks. It worked. I am trying to get better at writing efficient and small code. Do you se anuthing in my code that could have been better writen?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 27, 2020, 5:56pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/4 "2020-09-27T17:56:06Z")

</div>

Yeah, well…

I mean, first of all you could use ctrl-t in processing prior to posting

When posting: format your code.

Then: in the class you have one y for one paddle and one y for the other. This defies the principle of a class.

A class represents only ONE paddle and is oblivious to any other. And the class should be able to represent 1000 paddles with the same y for all

---

<div class="post-metadata">

### Author: ![Tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@Tobias](https://discourse.processing.org/u/Tobias)
#### Post date: [September 27, 2020, 6:01pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/5 "2020-09-27T18:01:31Z")

</div>

Thank you for your time and effort.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 27, 2020, 6:03pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/6 "2020-09-27T18:03:41Z")

</div>

Please show your latest version

---

<div class="post-metadata">

### Author: ![Tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@Tobias](https://discourse.processing.org/u/Tobias)
#### Post date: [September 27, 2020, 6:06pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/7 "2020-09-27T18:06:27Z")

</div>

I wont bother to change the y variables because i only need two paddles for this project.

```auto
Paddle p1;
Paddle p2;
Ball b;

boolean wp = false;
boolean sp = false;
boolean Up = false;
boolean Dp = false;

void setup() {
  size(600, 600);

  p1 = new Paddle(20);
  p2 = new Paddle(width - 30);
  b = new Ball();
}

void draw() {
  background(0);

  p1.show1();
  p1.move();

    p2.show2();
  p2.move();

  b.show();
}

void keyPressed() {
  if (key == 'w') {
    wp = true;
  }
  if (key == 's') {
    sp = true;
  }
  if (keyCode == UP) {
    Up = true;
  }
  if (keyCode == DOWN) {
    Dp = true;
  }
}

void keyReleased() {
  if (key == 'w') {
    wp = false;
  }
  if (key == 's') {
    sp = false;
  }
  if (keyCode == UP) {
    Up = false;
  }
  if (keyCode == DOWN) {
    Dp = false;
  }
}

class Paddle {
  float x;
  float y1 = height/2 - 50;
  float y2 = height/2 - 50;

  Paddle(float tempX) {
    x = tempX;
  }

  void show1() {
    stroke(255);
    fill(255);
    rect(x, y1, 10, 100);
  }

  void show2() {
    stroke(255);
    fill(255);
    rect(x, y2, 10, 100);
  }

  void move() {
    if (wp == true) {
      y1 -= 4;
    }
    if (sp == true) {
      y1 +=4;
    }
    if (Up == true) {
      y2 -= 4;
    }
    if (Dp == true) {
      y2 += 4;
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 27, 2020, 6:09pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/8 "2020-09-27T18:09:39Z")

</div>

> [@Tobias](#):
>
> ```auto
> p2.show2();
> p2.move();
> 
> ```

Here we can see you forgot to use ctrl-t

> [@Tobias](#):
>
> I won’t bother to change the y variables

You really should AND delete one of your show functions too please. Otherwise it really looks you didn’t understand OOP. 😉  
Having two different y variables for two objects doesn’t really reflect what a class is for.

There is a nice tutorial about objects [Objects / Processing.org](https://www.processing.org/tutorials/objects/)  
You should read it

Warm regards,

Chrisir

😉

---

<div class="post-metadata">

### Author: ![Tobias](https://avatars.discourse-cdn.com/v4/letter/t/c67d28/32.png) [@Tobias](https://discourse.processing.org/u/Tobias)
#### Post date: [September 27, 2020, 6:11pm UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/9 "2020-09-27T18:11:03Z")

</div>

I used ctrl-t, but i had to edit the comment due to a error in my code so the spacing got messed up.

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [September 28, 2020, 11:10am UTC](https://discourse.processing.org/t/rectangle-not-moving/24161/10 "2020-09-28T11:10:02Z")

</div>

here is a version more clean in terms of OOP

```auto

Paddle p1;
Paddle p2;
//Ball b;

//--------------------------------------------------------

void setup() {
  size(600, 600);

  p1 = new Paddle(20, height/2 - 50);
  p2 = new Paddle(width - 30, height/2 - 50);
  // b = new Ball();
} // func

void draw() {
  background(0);

  p1.show();
  p1.move();

  p2.show();
  p2.move();

  //b.show();
} // func

//--------------------------------------------------------

void keyPressed() {
  if (key == 'w') {
    p1.goUp = true;
  }
  if (key == 's') {
    p1.goDown = true;
  }
  if (keyCode == UP) {
    p2.goUp = true;
  }
  if (keyCode == DOWN) {
    p2.goDown = true;
  }
} // func

void keyReleased() {
  if (key == 'w') {
    p1.goUp = false;
  }
  if (key == 's') {
    p1.goDown = false;
  }
  if (keyCode == UP) {
    p2.goUp = false;
  }
  if (keyCode == DOWN) {
    p2.goDown = false;
  }
} // func

//=================================================================

class Paddle {

  float x = 0; // Pos 
  float y = 0;

  boolean goUp = false; // Flags for up/down
  boolean goDown = false;

  Paddle(float tempX, float tempY) {
    x = tempX;
    y = tempY;
  } // constr 

  void show() {
    stroke(255);
    fill(255);
    rect(x, y, 
      10, 100);
  } // func

  void move() {
    if (goUp) {
      y -= 4;
    } else if (goDown) {
      y += 4;
    }
  } // func
  //
} //class
//

```
