# Trouble with gravity and ending game

**URL:** <https://discourse.processing.org/t/trouble-with-gravity-and-ending-game/10729>\
**Category:** Coding Questions\
**Created:** [April 28, 2019, 9:59pm UTC](https://discourse.processing.org/t/trouble-with-gravity-and-ending-game/10729 "2019-04-28T21:59:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NeedHelp](https://avatars.discourse-cdn.com/v4/letter/n/fbc32d/32.png) [@NeedHelp](https://discourse.processing.org/u/NeedHelp)\
**Post date:** [April 28, 2019, 9:59pm UTC](https://discourse.processing.org/t/trouble-with-gravity-and-ending-game/10729/1 "2019-04-28T21:59:02Z")

</div>

I’m trying to make a game for my Processing class. It is due tomorrow morning and is almost completely finished, but I am having trouble triggering the end game screen when the ball leaves the screen. I also need the ball to do more than bounce in one place when it hits the wall. Any help is appreciated.

here is what I have.

```auto
int gameScreen = 0;

int ballX, ballY;
int ballSize = 20;
int ballColor = color(0, 0, 255);
int score = 0;

void setup() {
  size(500, 500);
  ballX=width/4;
  ballY=height/5;
}

void draw() {
  if (gameScreen == 0) {
    initScreen();
  } else if (gameScreen == 1) {
    gameScreen();
  } else if (gameScreen == 2) {
    gameOverScreen();
  }
}

/ *********SCREEN CONTENTS********* /

void initScreen() {
  background(0);
  textAlign(CENTER);
  text("Click to start wall pong", height/2, width/2);
}

float gravity = .5;
float ballSpeedVert = 0;
float airfriction = 0.01;
float friction = 0.1;
color racketColor = color(255, 0, 255);
float racketWidth = 10;
float racketHeight = 100;
int racketBounceRate = 1;

void gameScreen() {
  background(0, 255, 0);
  drawBall();
  applyGravity();
  keepInScreen();
  drawRacket();
  watchRacketBounce();
  printScore();
}

void drawRacket() {
  fill(racketColor);
  rectMode(CENTER);
  rect(mouseX, mouseY, racketWidth, racketHeight);
}

void watchRacketBounce() {
  float overhead = mouseY - pmouseY;
  if ((ballX+(ballSize/2) > mouseX-(racketWidth/2)) && (ballX-(ballSize/2) < mouseX+(racketWidth/2))) {
    if (dist(ballX, ballY, ballX, mouseY)<=(ballSize/2)+abs(overhead)) {
      makeBounceBottom(mouseY);

      if (overhead<0) {
        ballY+=overhead;
        ballSpeedVert+=overhead;
      }
      score();
    }
  }
}

void drawBall() {
  fill(ballColor);
  ellipse(ballX, ballY, ballSize, ballSize);
}

void applyGravity() {
  ballSpeedVert += gravity;
  ballX += ballSpeedVert;
  ballSpeedVert -= (ballSpeedVert * airfriction);
}
void makeBounceBottom(int surface) {
  ballX = surface-(ballSize/2);
  ballSpeedVert*=-1;
  ballSpeedVert -= (ballSpeedVert * friction);
}
void makeBounceTop(int surface) {
  ballX = surface+(ballSize/2);
  ballSpeedVert*=-1;
  ballSpeedVert -= (ballSpeedVert * friction);
}
void keepInScreen() {
  if (ballX-(ballSize/2) < 0) makeBounceTop(0);
  if (ballX+(ballSize/2) > width) endGame();
}

void lose() {
  if (ballX-(ballSize/2) > -5) {
    endGame();
  }
}

void score() {
  score++;
}

void gameOverScreen() {
  background(0);
  textAlign(CENTER);
  fill(255);
  textSize(30);
  text("Game Over", height/2, width/2 - 20);
  textSize(15);
  text("Click to Restart", height/2, width/2 + 10);
}

void printScore() {
  textAlign(CENTER);
  fill(0);
  textSize(30); 
  text(score, height/2, 50);
}

/ *********INPUTS********* /

public void mousePressed() {

  if (gameScreen==0) {
    startGame();
  }

  if (gameScreen==2) {
    restart();
  }
}

/ *********OTHER FUNCTIONS********* /

void startGame() {
  gameScreen=1;
}
void endGame () {
  gameScreen=2;
}

void restart() {
  score = 0;
  ballX=width/4;
  ballY=height/5;
  gameScreen = 0;
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [April 29, 2019, 1:09am UTC](https://discourse.processing.org/t/trouble-with-gravity-and-ending-game/10729/2 "2019-04-29T01:09:54Z")

</div>

pls format you code in the processing IDE with [ctrl][t]  
and paste it in ( repair your above ) forum posting using the

```auto
</> code tag

```

looks like

> ```  
> my code  
> ```

* * *

what you expect when you tell us:

> that is home work and you are late

* * *

you write some questionable code to check on ballX position ( lose() ) but not use/call it,  
try:

```auto
void keepInScreen() {
  if (ballX-(ballSize/2) < 0) makeBounceTop(0);
  if (ballX+(ballSize/2) > width) endGame();
}

```

---

<div class="post-metadata">

**Author:** ![NeedHelp](https://avatars.discourse-cdn.com/v4/letter/n/fbc32d/32.png) [@NeedHelp](https://discourse.processing.org/u/NeedHelp)\
**Post date:** [April 29, 2019, 1:57am UTC](https://discourse.processing.org/t/trouble-with-gravity-and-ending-game/10729/3 "2019-04-29T01:57:40Z")

</div>

It worked and the end triggered. Thank you!
