# How to make the game continue playing, not just playing once

**URL:** <https://discourse.processing.org/t/how-to-make-the-game-continue-playing-not-just-playing-once/4215>\
**Category:** Coding Questions\
**Created:** [October 7, 2018, 7:40am UTC](https://discourse.processing.org/t/how-to-make-the-game-continue-playing-not-just-playing-once/4215 "2018-10-07T07:40:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![applelovestree](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@applelovestree](https://discourse.processing.org/u/applelovestree)\
**Post date:** [October 7, 2018, 7:40am UTC](https://discourse.processing.org/t/how-to-make-the-game-continue-playing-not-just-playing-once/4215/1 "2018-10-07T07:40:04Z")

</div>

At the moment, the red color circle can be played only once, can the game continue to play maybe for 1 minute, then it will stop and show the scores? And when the following game continues, so the answer to the game will be automatically changed.

 ![17%20PM](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/234450f08b4bc9f516fab322a2c59af2b9692f96.png)

The code link is here:

[https://editor.p5js.org/mtwhill/sketches/ryMrazX5Q](https://editor.p5js.org/mtwhill/sketches/ryMrazX5Q)

```auto
var bx;
var by;
var ellSize = 75;
var overBox = false;
var locked = false;
var xOffset = 0.0; 
var yOffset = 0.0; 

function setup() {
  createCanvas(600, 300);
  bx = width/2.0;
  by = height/2.0;
  rectMode(RADIUS);
  strokeWeight(1.5);
}

function draw() { 
  background(230);
  
  // Test if the cursor is over the box 
  if (mouseX > bx-ellSize && mouseX < bx+ellSize && 
      mouseY > by-ellSize && mouseY < by+ellSize) {
    overBox = true;  
    if(!locked) { 
      stroke(255); 
    } 
  } else {
    stroke(0);
    overBox = false;
  }
  
  // Draw the box
	fill(250, 0, 0);
  ellipse(bx, by, ellSize, ellSize);
	rect(50, 50, 80, 80);
	fill(0, 250, 0);
	rect(550, 50, 80, 80);
	fill(0, 0, 250);
	rect(50, 250, 80, 80);
	fill(0, 0, 0);
	rect(550, 250, 80, 80);

}

function mousePressed() {
  if(overBox) { 
    locked = true; 
  } else {
    locked = false;
  }
  xOffset = mouseX-bx; 
  yOffset = mouseY-by; 

}

function mouseDragged() {
  if(locked) {
    bx = mouseX-xOffset; 
    by = mouseY-yOffset; 
  }
}

function mouseReleased() {
  if(bx>120|by >120) {
		bx = 300;
		by = 150;
		 }
	else {
		ellSize = 0;
	}
}

```

---

<div class="post-metadata">

**Author:** ![julian2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/julian2/32/1551_2.png) [@julian2](https://discourse.processing.org/u/julian2)\
**Post date:** [October 7, 2018, 9:07am UTC](https://discourse.processing.org/t/how-to-make-the-game-continue-playing-not-just-playing-once/4215/2 "2018-10-07T09:07:27Z")

</div>

[https://editor.p5js.org/julian2/sketches/Bko1oSDq7](https://editor.p5js.org/julian2/sketches/Bko1oSDq7)

I think the goal you wanted to create with this was to drag the red ball to the red square/corner right? I’ve tried to make minimal changes in order to get to a ‘game loop’ where we check for the condition `isGameOver()` in every loop, and a `setupGame()` function to call in order to _restart_ 🙂

---

<div class="post-metadata">

**Author:** ![applelovestree](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@applelovestree](https://discourse.processing.org/u/applelovestree)\
**Post date:** [October 7, 2018, 9:25am UTC](https://discourse.processing.org/t/how-to-make-the-game-continue-playing-not-just-playing-once/4215/3 "2018-10-07T09:25:16Z")

</div>

Awesome, thank you so much for your help, that is what I want! Do you know how could make the questions placed in different locations, and the answers are different, rather than the red color circle always showing on the interface.

---

<div class="post-metadata">

**Author:** ![julian2](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/julian2/32/1551_2.png) [@julian2](https://discourse.processing.org/u/julian2)\
**Post date:** [October 7, 2018, 7:25pm UTC](https://discourse.processing.org/t/how-to-make-the-game-continue-playing-not-just-playing-once/4215/4 "2018-10-07T19:25:10Z")

</div>

Currently, you have _hard coded_ the location of the red square in your _collision checker_. What you probably want now is to make a _general_ square \<-\> ball collision detection function, that takes in the square & the ball, and outputs whether the ball is colliding with the square or not! 🙂

something like the following (note: this is untested code & the parameters _square_ and _ball_ are hypothetical objects), I’m not sure if you’ve looked into classes yet, but if you have, then _square_ and _ball_ can be thought of as classes with properties such as ‘width, height, x and y’:

```auto
function isCollision(square, ball) {
    if (
        ball.x > square.x && ball.x < square.x + square.width &&
        ball.y > square.y && ball.y < square.y + square.height
    ) {
        return true;
    }
    return false
}

```

this way, regardless of how many squares & where the squares are located, you can check if the ball is placed inside it or not.

---

<div class="post-metadata">

**Author:** ![applelovestree](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@applelovestree](https://discourse.processing.org/u/applelovestree)\
**Post date:** [October 7, 2018, 10:18pm UTC](https://discourse.processing.org/t/how-to-make-the-game-continue-playing-not-just-playing-once/4215/5 "2018-10-07T22:18:57Z")

</div>

Thanks a lot for spending your time on it. Yes, I like to make the “questions” and “answers” appear randomly when the game is continuing. And I like to put the image(png) as the sources of questions and answers, rather than draw the circle and square with colors.

---

<div class="post-metadata">

**Author:** ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)\
**Post date:** [October 7, 2018, 10:21pm UTC](https://discourse.processing.org/t/how-to-make-the-game-continue-playing-not-just-playing-once/4215/6 "2018-10-07T22:21:23Z")

</div>

If you post your question to multiple sites, please link between crossposts.

This question has also been asked (and answered) [here](https://stackoverflow.com/q/52685805/873165).

---

<div class="post-metadata">

**Author:** ![applelovestree](https://avatars.discourse-cdn.com/v4/letter/a/8491ac/32.png) [@applelovestree](https://discourse.processing.org/u/applelovestree)\
**Post date:** [October 7, 2018, 10:41pm UTC](https://discourse.processing.org/t/how-to-make-the-game-continue-playing-not-just-playing-once/4215/7 "2018-10-07T22:41:55Z")

</div>

Thanks for your reminding. The question has some problems going.
