# Stopping the game when touching sides pong game

**URL:** https://discourse.processing.org/t/stopping-the-game-when-touching-sides-pong-game/21810
**Category:** Coding Questions
**Created:** [June 12, 2020, 4:31pm UTC](https://discourse.processing.org/t/stopping-the-game-when-touching-sides-pong-game/21810 "2020-06-12T16:31:27Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mrporcessing](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mrporcessing](https://discourse.processing.org/u/mrporcessing)
#### Post date: [June 12, 2020, 4:31pm UTC](https://discourse.processing.org/t/stopping-the-game-when-touching-sides-pong-game/21810/1 "2020-06-12T16:31:27Z")

</div>

Hi, I just need help trying to stop the game, if the ball touches the left or right side of the screen and not the paddles. here is my code:

```auto
int x,y,w,h,speedX,speedY;//circle variables
int paddleX, paddleY,paddlew,paddleH,paddleS,paddleX2,paddleY2,paddlew2,paddleH2,paddleS2;//left and right paddles variables
boolean up,down;//up down for right paddle
boolean positive, negative;//up down for left paddle
boolean a_pressed=false;//pauses game
boolean q_pressed=false;//unpauses game
int scoreL=0;
int scoreR=0;
void setup() {
  size(600,600);
  x=width/2;
  y=height/2;
  w=50;
  h=50;
  speedX=3;
  speedY=3;
  paddleX=570;
  paddleY=height/2;
  paddlew=30;
  paddleH=100;
  paddleS=5;
  paddleX2=10;
  paddleY2=height/2;
  paddlew2=30;
  paddleH2=100;
  paddleS2=5;
  
}
  void draw() {
background(0);
fill(224);
rect(paddleX,paddleY,paddlew,paddleH);
rect(paddleX2,paddleY2,paddlew2,paddleH2);
fill(255,0,0);
ellipse(x,y,w,h);
textSize(30);
fill(0,255,255);
text(scoreL,250,50);
text("L SCORE:",100,50);
text(scoreR,500,50);
text("R SCORE:",350,50);
  
//moving circle
x=x+speedX;
y=y+speedY;
//preventing it leaving screen
if(x>width-w/2){
  speedX=-speedX;
}else if(x<0+w/2) {
  speedX=-speedX;
}
if(y>height-h/2){
  speedY=-speedY;
}else if(y<0+h/20){
  speedY=-speedY;
}
//moving paddle
if(up) {
paddleY=paddleY+paddleS;
}
if(positive) {
  paddleY2=paddleY2+paddleS2;
}
if(down) {
  paddleY=paddleY-paddleS;
  }
if(negative) {
  paddleY2=paddleY2-paddleS2;
}
//preventing paddle from going off screen
if(paddleY-paddleH/2<0) {
  paddleY=paddleY+paddleS;
}
if(paddleY2-paddleH2/2<0) {
  paddleY2=paddleY2+paddleS2;
}
if(paddleY+paddleH/2>height) {
  paddleY=paddleY-paddleS;
}
if(paddleY2+paddleH2/2>height) {
  paddleY2=paddleY2-paddleS2;
}
//ball bouncing off paddle
if(x>paddleX - w/2 && x < paddleX+paddlew - w/2 && y < paddleY + paddleH + h/2 && y > paddleY - h/2) {
speedX=-speedX;
scoreR=scoreR+10;
}
if(x>paddleX2 + w/2 && x < paddleX2 + paddlew2 + w/2 && y < paddleY2 + paddleH2 + h/2 && y > paddleY2 - h/2) {
speedX=-speedX;
scoreL=scoreL+10;
}
//stop game if ball touches the end of left and right sides without paddle contact

void keyPressed() {
  if(key=='d') {
    up=true;
  }
  if(key=='u') {
    down=true;
  }
  if(key=='m') {
    positive=true;
  }
  if(key=='p') {
    negative=true;
  }
  if(key=='a'){
  a_pressed=true;
  noLoop();
}
if(key=='q') {
  q_pressed=true;
  loop();
}
  }
void keyReleased() {
  if(key=='d') {
    up=false;
  }
  if(key=='u') {
    down=false;
  }
  if(key=='m') {
    positive=false;
  }
  if(key=='p') {
    negative=false;
  }
  if(key=='a'){
  a_pressed=true;
  noLoop();
}
if(key=='q') {
  q_pressed=true;
  loop();
}
}

```

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [June 12, 2020, 5:14pm UTC](https://discourse.processing.org/t/stopping-the-game-when-touching-sides-pong-game/21810/2 "2020-06-12T17:14:29Z")

</div>

Hi,

Can you please format your code by using the `</>` button in the message editor on the forum? (You can press `Ctrl+T` in the Processing IDE to auto format your code 🙂 )

Because for now it’s actually impossible to read or copy into Processing to test it. (I already told you this on [I need help to keep score](https://discourse.processing.org/t/i-need-help-to-keep-score/21724/3))

**And please do not post the same code on multiple threads, try to summarize your problem…**  
([Bouncing ball with paddle](https://discourse.processing.org/t/bouncing-ball-with-paddle/21798))  
([I need help to keep score](https://discourse.processing.org/t/i-need-help-to-keep-score/21724))  
([Creating menu for game and touching sides pong game](https://discourse.processing.org/t/creating-menu-for-game-and-touching-sides-pong-game/21821))

But testing if your ball touches left or right side is not complicated :

- The location of your ball is described by `x` and `y`
- It’s diameter is `w` and `h` (or just `w` because a ball is always circular in pong)
- The left side of the screen is at coordinates `[0, y]` (`y` can be whatever)
- The right side of the screen is at coordinates `[width, y]`

So in order to test if it touches the left wall, you test if the x coordinate of the ball is inferior (on the left side) to 0 (the left wall), you need to take into account the diameter of the ball (in fact it’s radius) :

```auto
boolean touchLeftWall(x, diameter){
  return (x - diameter / 2) <= 0;
}

```

This function returns a `boolean` (`true` or `false`) that tells if the ball hit the left wall. You can do the same for the right wall.

---

<div class="post-metadata">

### Author: ![mrporcessing](https://avatars.discourse-cdn.com/v4/letter/m/dc4da7/32.png) [@mrporcessing](https://discourse.processing.org/u/mrporcessing)
#### Post date: [June 12, 2020, 6:28pm UTC](https://discourse.processing.org/t/stopping-the-game-when-touching-sides-pong-game/21810/3 "2020-06-12T18:28:33Z")

</div>

Thanks, but where do put this boolean In the variable label or in void draw()?

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [June 13, 2020, 10:48am UTC](https://discourse.processing.org/t/stopping-the-game-when-touching-sides-pong-game/21810/4 "2020-06-13T10:48:27Z")

</div>

In the `draw()` function, you are displaying your game each frame. In order to stop it, you can check if the ball touches either left or right wall and then call the [`noLoop()`](https://processing.org/reference/noLoop_.html) function which basically stop the draw loop :

```auto
void draw(){
  // Display your game

  // Stop the game if the ball touched the wall
  if(touchLeftWall(x, diameter) || touchRightWall(y, diameter)){
    noLoop();
  }
}

```
