为什么gameover后分数一直在计算?

why does the score keep counting after gameover?

除了即使输了比赛,分数还在计算以外,一切都正常进行。当游戏输了时应该冻结分数,以便当方法 gameover();被称为可以为该游戏给出分数。

它也没有增加特定的数量,分数只是从 10 增加到 210 到 430 - 到处都是。 有人能找出原因吗?

    // variables for the ball
    int ball_width = 15, 
      ball_height = 15; 

    float ballX, 
      ballY;

    // // variables for the paddles
    int paddle_width = 20, 
      paddle_height = 200;

    int paddle1, 
      paddle2=0; 

    // // direction variables 
    float  directionX = 12, 
      directionY = 12; 

    // // variables for the score
    int scorecounter = 0;

    // //game states
    boolean playing = false, 
      gameover = false, 
      finalscore = false, 
      score = true;


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

    void setup () {

      size (1900, 800); // the field game is going to be 1900x800 px big
      background (0); // black background
      rectMode (CENTER);

      paddle1 = 60;
      paddle2 = width - 60;
    }

    void draw () {

      background (0); // black background

      playing ();
      contact ();
      gameover ();
    } 

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

    void playing () {

      if (keyPressed) {  // starts the game and makes the restart possible
        playing = true;
        scorecounter = 0 ;
        gameover = false;
        ballX = width/2;
        ballY = height/2;
      }

      if (!playing) {
        // playing = false

        fill(255); 
        textSize(80); 
        textAlign(CENTER); 
        text("Press Space to Play", width/2, height/4);

        fill (255); 
        ellipse (width/2, height/2, ball_width, ball_height); // this is the starting point of the ball
        fill (255, 10, 20); 
        rect(paddle1, (height/2), paddle_width, paddle_height);  // red pong
        fill (60, 255, 0); 
        rect(paddle2, (height/2), paddle_width, paddle_height);  // green pong
      }

      if (playing) { // playing = true

        score(); // does what i wrote down in void score () -- keeps track of the score

        ballX = ballX + directionX;
        ballY = ballY + directionY; // gives the directions of the ball

        fill (255); 
        ellipse (ballX, ballY, ball_width, ball_height); // ball itself

        fill ( 255, 10, 20 ); 
        rect(paddle1, mouseY, paddle_width, paddle_height); // red pong

        fill ( 60, 255, 0 ); 
        rect(paddle2, mouseY, paddle_width, paddle_height);  // green pong

        // top and bottom of screen --------------------
        if ( ballY  + ball_width/2 > height ) { 
          directionY = -directionY;
        } // if the ball reaches the lower wall it will bounce off

        if ( ballY - ball_width/2 < 0 ) { 
          directionY = -directionY;
        } // if the ball reaches the upper wall it will bounce off

        if ( ballX > width || ballX < 0 ) { 
          gameover = true;
        } // if the ball reaches one of the bounderies it will be game over
      }
    }

    void contact () {

      // right paddle = paddle2 
      if (ballY - ball_width/2 > mouseY - paddle_height/2 && 
        ballY + ball_width/2 < mouseY + paddle_height/2 && 
        ballX + ball_width/2 > paddle2-4 ) {
        directionX = -abs(directionX);
        scorecounter = scorecounter + 10;
      }

      // left paddle = paddle1
      if (ballY - ball_width/2 > mouseY - paddle_height/2 && 
        ballY + ball_width/2 < mouseY + paddle_height/2 && 
        ballX + ball_width/2 < paddle1+ paddle_width+4 ) {
        directionX = abs(directionX);
        scorecounter = scorecounter + 10; // if the ball touches the paddles it will bounce off
      }

      score ();
    }

    void gameover () {

      if (gameover) {
        background (0);
        finalscore (); // gives me the final score + play again option
        score = false;
      } // it overrides the scorecounter with a black background

    } 

    void score () {

      if (playing) {

        fill(255); 
        textSize(45); 
        textAlign(CENTER); 
        text ( scorecounter, width/2, height/4); // keeps the score on the display while playing
      }

    }

    void finalscore () {

      if (gameover) {

        score = false; // the scorecounter will stop counting

        fill(255); 
        textSize(45); 
        textAlign(CENTER); 
        text("Game Over. Press a key to play again.", width/2, height/4); // game over message
        fill(255); 
        textSize(80); 
        textAlign(CENTER); 
        text("You scored " + scorecounter+ " points", width/2, (height/4) * 3); // the final score will appear here
      }
    } 

你应该有 playing = false;
在游戏结束方法或其他地方的某个地方。如我所见,您没有这样的代码行。 我问一个问题。您在哪里将播放更改为 false 以便 您期望获得

 If(!playing)

条件而不是

If(playing)

你按下了键。游戏开始。现在你达到

If(playing)

然后小球到达边界,代码到达

 if ( ballX > width || ballX < 0 ) {
gameover = true; } // if the ball reaches one of the bounderies it will be game over 

在这种情况下。 playing var 和 gameover var 都是真实的。正确的? 在

If(playing) 

你有 score() method.so 即使 gameover 为真,score 方法仍然会被执行。

您可以将播放设置为假:

if ( ballX > width || ballX < 0 ) {     gameover = true;
Playing =false ; }

或者你可以这样修改评分方法:

 void score () { if (playing && !gameover) { fill(255); textSize(45); textAlign(CENTER); text(scorecounter, width/2,height/4); 
// keeps the score on the display  while playing }