Restarting the Draw loop with mouseClicked
in
Programming Questions
•
6 months ago
Hi everyone,
Sorry to ask, but in my program, I'm trying to have it so that there is a "game over" screen after a timer runs out, and then I can start the game again from there (have another attempt at it) by clicking on the screen, but I haven't figured out a way to make it work (with function calls and loop() and noLoop()). It compiles and runs, but every time, it won't go off the "game over" screen when I get there. The object of the game is to click the circles before they disappear because time is up. Is the way I timed it suitable?
Any help is appreciated.
Thanks.
- //Ball Tap
- int minBallDiam = 20;
- int maxBallDiam = 60;
- int b1 = 250;
- int b2 = 250;
- int b3 = 250;
- byte ballDiam;
- int ballX;
- int ballY;
- int score = 0;
- int scoreX = 0;
- boolean hundred = true;
- byte fr = 50;
- int secOpen = 2;
- int counter = 0;
- ////////////////////////
- void setup() {
- ballDiam = (byte)random(minBallDiam, maxBallDiam);
- ballX = (int)random(ballDiam, width - ballDiam );
- ballY = (int)random(ballDiam, height - ballDiam);
- orientation(PORTRAIT);
- size(320, 480);
- smooth();
- frameRate(fr);
- noStroke();
- fill(200, 100, 40);
- playGame();
- };
- /////////////////////////////////
- void draw() {
- // playGame();
- }
- void playGame() {
- background(b1, b2, b3);
- ellipse(ballX, ballY, ballDiam, ballDiam);
- textSize(15);
- text("Score: " + score, width-60 - scoreX, 10);
- if (mouseX >= ballX && mouseX <= ballX +ballDiam) {
- if ( mouseY >= ballY && mouseY <=ballY + ballDiam) {
- background(b1, b2, b3);
- ballDiam = (byte)random(minBallDiam, maxBallDiam);
- ballX = (int)random(ballDiam, width - ballDiam );
- ballY = (int)random(ballDiam, height - ballDiam);
- score += 1 + (maxBallDiam/10);
- counter = 0;
- if (hundred) { // to move the writing in the top right hand corner left when an extra digit is put on the score
- if (score > 100 && score< 1000) { // a bit cumbersome...any ideas, or just stick to putting the score on the left side?
- scoreX += 10;
- hundred = false;
- }
- }
- }
- }
- counter++;
- if (counter >= fr * secOpen) {
- gameOver();
- //tried noLoop() here and then loop() in line 103 where marked
- }
- }
- void gameOver() {
- background(b1, b2, b3);
- textSize(55);
- text("GAME OVER!", 5, 50);
- textSize(20);
- text("Your score: "+ score, 80, 200);
- textSize(30);
- noFill();
- stroke(10);
- rect(50, 400, 250, 50);
- text("Tap to Play Again", 60, 420);
- }
- void mouseClicked() {
- fill(100, 100, 40);
- noStroke();
- playGame(); //tried putting loop() in as well
- }
1