比赛结束后为什么得分仍在计算?

时间:2016-05-21 12:29:54

标签: processing

除了事实上即使这个人输掉了比赛,得分也在不断计算,所以一切都应该完美无缺。当游戏丢失时,分数应该冻结,以便当方法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
      }
    } 

1 个答案:

答案 0 :(得分:0)

你应该有 playing = 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 

在这种情况下。播放var和gameover var都是正确的。对?

If(playing) 

你有得分()方法。即使游戏结果为真,仍然会执行得分方法。

您可以在以下情况下将播放设置为false:

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 }