# Pong Game glitch!

**URL:** <https://discourse.processing.org/t/pong-game-glitch/7158>\
**Category:** Coding Questions\
**Created:** [January 4, 2019, 11:44am UTC](https://discourse.processing.org/t/pong-game-glitch/7158 "2019-01-04T11:44:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benasin](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Benasin](https://discourse.processing.org/u/Benasin)\
**Post date:** [January 4, 2019, 11:44am UTC](https://discourse.processing.org/t/pong-game-glitch/7158/1 "2019-01-04T11:44:52Z")

</div>

I have been using Processing for a week and I have created a simple pong game but I have a problem is that when the ball hit the side parts of the rectangle movable bar it shows glitches and the ball doesn’t bounce but move right through the bar. Is there anyway to resolve this? Here is my code :

```auto
float x = 400;
float y = 35;
float xdirection = random(1,2);
float ydirection = random(1,2);
float xspeed = 2;
float yspeed = 2;
boolean gameStart = false;
int point = 0;
void setup(){
  size(800, 800);
  background(0);
}
void draw(){
  background(0);
  ellipse(x ,y,55,55);
  fill(255);
  textSize(50);
  text(point,15,50);
  rect(mouseX - 75,800-25 ,150,20);
  if(gameStart){
    x += xspeed * xdirection;
    y += yspeed * ydirection;
      if(x < mouseX + 75 && x > mouseX - 75 && y > 745){
        ydirection *= -1;
        point += 1;
        fill(255, 0, 0);
      }
      if (x > 775 || x < 35){
        xdirection *= -1;
        xspeed += 1;
      }
      if (y < 35){
        ydirection *= -1;
        yspeed += 1;
      }
      if (y > 800){
        gameStart = false;
        x = 400;
        y = 35;
        point = 0;
        xspeed = 2;
        yspeed = 2;
      }
  }
}
void mousePressed(){
  gameStart = !gameStart;
}

```

Thanks !!! 🙂

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 4, 2019, 12:21pm UTC](https://discourse.processing.org/t/pong-game-glitch/7158/2 "2019-01-04T12:21:38Z")

</div>

Do you have an ‘if’ where the collisions between the ball and the bar are checked?

---

<div class="post-metadata">

**Author:** ![Benasin](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Benasin](https://discourse.processing.org/u/Benasin)\
**Post date:** [January 4, 2019, 12:30pm UTC](https://discourse.processing.org/t/pong-game-glitch/7158/3 "2019-01-04T12:30:14Z")

</div>

Yes here `if(x < mouseX + 75 && x > mouseX - 75 && y > 745)`. I mean it does bounce off but when it hits this part it gets glitchy and go through ![Capture](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/908161f56f7888e51d10814eaf2f5f62e6f42845.png)

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 4, 2019, 12:33pm UTC](https://discourse.processing.org/t/pong-game-glitch/7158/4 "2019-01-04T12:33:15Z")

</div>

I just changed in your ‘if’ the -25 to - 50

```auto
float x = 400;
float y = 35;
float xdirection = random(1,2);
float ydirection = random(1,2);
float xspeed = 2;
float yspeed = 2;
boolean gameStart = false;
int point = 0;

int width = 800;
int height = 600;

void setup(){
  size(800, 600);
}
void draw(){
  background(0);
  ellipse(x ,y,55,55);
  fill(255);
  textSize(50);
  text(point,15,50);
  rect(mouseX - 75,height-25 ,150,20);
  if(gameStart){
    x += xspeed * xdirection;
    y += yspeed * ydirection;
      if(x < mouseX + 75 && x > mouseX - 75 && y > height - 50){
        ydirection *= -1;
        point += 1;
        fill(255, 0, 0);
      }
      if (x > width - 25 || x < 35){
        xdirection *= -1;
        xspeed += 1;
      }
      if (y < 35){
        ydirection *= -1;
        yspeed += 1;
      }
      if (y > height){
        gameStart = false;
        x = 400;
        y = 35;
        point = 0;
        xspeed = 2;
        yspeed = 2;
      }
  }
}
void mousePressed(){
  gameStart = !gameStart;
}

```

Done, now it should work.  
I added width and height bc the window size was bigger than my screen.  
Everything should work now

---

<div class="post-metadata">

**Author:** ![Architector\_4](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/architector_4/32/813_2.png) [@Architector\_4](https://discourse.processing.org/u/Architector_4)\
**Post date:** [January 4, 2019, 12:39pm UTC](https://discourse.processing.org/t/pong-game-glitch/7158/5 "2019-01-04T12:39:54Z")

</div>

The problem happens because when it touches the side of the paddle, it tries to fly up, but then it touches the paddle again on the next frame as it didn’t fly off it, and the code gets run again and again.  
Solution above moves the `y` check down more so that it’s touchable “side” is below the screen, where the ball can never be. It’s more of a workaround, and will make the sides of your paddle intangible. It doesn’t matter much, because the pad is pretty thin, but is still something to consider.

I think I have a better solution. Replace:

```auto
if(x < mouseX + 75 && x > mouseX - 75 && y > 745)

```

With:

```auto
if(ydirection > 0 && x < mouseX + 75 && x > mouseX - 75 && y > 745)

```

So that it does the collision check only if the ball is moving down. This way, the sides will still reflect the ball’s movement, without glitching.  
When you get to making another paddle at the top, make sure to check `ydirection < 0` for it instead.

---

<div class="post-metadata">

**Author:** ![Sky](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/sky/32/3013_2.png) [@Sky](https://discourse.processing.org/u/Sky)\
**Post date:** [January 4, 2019, 12:44pm UTC](https://discourse.processing.org/t/pong-game-glitch/7158/6 "2019-01-04T12:44:47Z")

</div>

And, if I’m not wrong, ellipse 3rd and 4th args are radiuses, not diameters, you dont need to halve them when checking collisions

---

<div class="post-metadata">

**Author:** ![Benasin](https://avatars.discourse-cdn.com/v4/letter/b/8c91f0/32.png) [@Benasin](https://discourse.processing.org/u/Benasin)\
**Post date:** [January 4, 2019, 12:47pm UTC](https://discourse.processing.org/t/pong-game-glitch/7158/7 "2019-01-04T12:47:22Z")

</div>

Wow, thanks I understand it now !!!
