# How to add a target to a pong simulator game?

**URL:** https://discourse.processing.org/t/how-to-add-a-target-to-a-pong-simulator-game/32427
**Category:** Coding Questions
**Tags:** homework
**Created:** [September 26, 2021, 1:26pm UTC](https://discourse.processing.org/t/how-to-add-a-target-to-a-pong-simulator-game/32427 "2021-09-26T13:26:13Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Lulua](https://avatars.discourse-cdn.com/v4/letter/l/eada6e/32.png) [@Lulua](https://discourse.processing.org/u/Lulua)
#### Post date: [September 26, 2021, 1:26pm UTC](https://discourse.processing.org/t/how-to-add-a-target-to-a-pong-simulator-game/32427/1 "2021-09-26T13:26:13Z")

</div>

I have made a pong simulator game. I added a circle as a target but need to make it so that when the ball hits the target, the player wins and the game restarts. Help!!!

PImage dragon;  
PFont font;  
int base=40;  
int x,y,gameScore=0;  
int changeX=-5;  
int changeY=-5;  
int gameOver=0;  
boolean win;

void setup(){  
size(760, 640);  
dragon = loadImage(“dragon.jpg”);  
// x=(int)random(width); //x for the ball  
x = width/2;  
y = height/2;

//y=height-base; // y for the ball  
font = loadFont (“HelveticaNeue-CondensedBlack-48.vlw”);  
textFont(font);  
win = false;  
}  
void draw(){  
if(gameOver==0)  
{  
background(0);  
image(dragon,0,2, 760, 640);  
ellipse (200,200,50,50);

// target  
if(win == true){  
background(28,155,35);  
textSize(26);  
text(“YOU WIN!”,380,320);  
textSize(26);  
text(“CLICK TO RESTART”,380,320+20);  
}  
if( x \> (200 - 100) &&  
x \< (200 + 100) &&  
y \> (200 + 100) &&  
y == (200 + 100)  
){  
win = true;  
}

//game score  
text(“SCORE:”+gameScore+“00”,width/2,height/2);  
// make the rectangle move with the mouse  
rect(mouseX,height-base,200,base);  
ellipse(x,y,10,10);  
x=x+changeX; // x change  
y=y+changeY; //y change  
if(x\<0 | x\>width) //if x is less than 0  
{  
changeX=-changeX;  
}  
if(y\<0)  
{  
changeY=-changeY;  
}  
if(y\>height-base)  
{  
//check whether it is falling inside the rectangle or not  
if(x\>mouseX && x\<mouseX+200)  
{  
changeY=-changeY; //bounce back  
gameScore++; // if the ball bounces back off the rectangle, the gamescore goes up  
}  
else  
{  
gameOverSplash(); // if the ball misses the rectangle, game over  
}  
}  
}  
else  
{  
background(#A71627);  
textSize(26);  
text(“GAME OVER”,380,320);  
textSize(26);  
text(“CLICK TO RESTART”,380,320+20);  
}

}  
void gameOverSplash()  
{  
gameOver=1;  
}

void mouseClicked()  
{  
changeY=-changeY;  
gameScore=0;  
gameOver=0;  
}

void mousePressed(){  
println(mouseX, mouseY);  
}

---

<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 26, 2021, 8:38pm UTC](https://discourse.processing.org/t/how-to-add-a-target-to-a-pong-simulator-game/32427/2 "2021-09-26T20:38:05Z")

</div>

your if-clause was a bit off.

try this

```auto
    if ( x > (200 - 100) &&
      x < (200 + 100) &&
      y > (200 - 100) &&
      y < (200 + 100)
      ) {
      win = true;
      println("HIT");
    }

```

Hey, and welcome to the forum!

Great to have you here!

Warm regards,

Chrisir

**Full Code**

```auto
PImage dragon;
PFont font;
int base=40;
int x, y, gameScore=0;
int changeX=-5;
int changeY=-5;
int gameOver=0;
boolean win = false;

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

void setup() {
  size(760, 640);
  dragon = loadImage("dragon.jpg");
  // x=(int)random(width); //x for the ball
  x = width/2;
  y = height/2;

  //y=height-base; // y for the ball
  font = createFont ("HelveticaNeue-CondensedBlack-48.vlw", 14);
  textFont(font);
  win = false;
}

void draw() {
  if (gameOver==0) {
    background(0);

    image(dragon, 0, 2, 760, 640);
    ellipse (200, 200, 50, 50);

    // target
    if (win == true) {
      background(28, 155, 35);
      textSize(26);
      text("YOU WIN!", 380, 320);
      textSize(26);
      text("CLICK TO RESTART", 380, 320+20);
    }

    if ( x > (200 - 100) &&
      x < (200 + 100) &&
      y > (200 - 100) &&
      y < (200 + 100)
      ) {
      win = true;
      // println("HIT");
    }

    if (win == false) {
      //game score
      text("SCORE:"+gameScore+"00", width/2, height/2);
      // make the rectangle move with the mouse
      rect(mouseX, height-base, 200, base);
      ellipse(x, y, 10, 10);
      x=x+changeX; // x change
      y=y+changeY; // y change
      if (x<0 | x>width) //if x is less than 0
      {
        changeX=-changeX;
      }
      if (y<0)
      {
        changeY=-changeY;
      }
      if (y>height-base)
      {
        //check whether it is falling inside the rectangle or not
        if (x>mouseX && x<mouseX+200)
        {
          changeY=-changeY; //bounce back
          gameScore++; // if the ball bounces back off the rectangle, the gamescore goes up
        } else
        {
          gameOverSplash(); // if the ball misses the rectangle, game over
        }
      }
    }
  } else
  {
    background(#A71627);
    textSize(26);
    text("GAME OVER", 380, 320);
    textSize(26);
    text("CLICK TO RESTART", 380, 320+20);
  }
}

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

void gameOverSplash() {
  gameOver=1;
}

void mouseClicked() {
  changeY=-changeY;

  win=false; 
  x = width/2+300;
  y = height/2;

  gameScore=0;
  gameOver=0;
}

void mousePressed() {
  println(mouseX, mouseY);
}

```
