当球碰到球拍的边缘然后回到原始速度时,如何提高球的速度?

时间:2019-02-11 06:57:02

标签: processing

当球碰到球拍的边缘然后又回到原始速度时,如何提高球的速度?在此乒乓球游戏中,我希望我的球在击中球拍边缘时提高其速度,然后在击中球拍中间时恢复其正常速度。我评论了一些“ if语句”,因为我尝试了它们,但是它们没有用。我使用的语言是“处理中”

2 个答案:

答案 0 :(得分:2)

  

我希望我的球在击中球拍的边缘时提高速度,然后在击中球拍的中部时回到其正常速度< / p>

添加一个保存当前速度的变量:

final int MIN_SPEED = 3;
final int MAX_SPEED = 6;
float currentSpeed = (float)MIN_SPEED;

在函数MIN_SPEED中使用此变量代替bounce

void bounce () {
    //bouncing off the top and bottom of the screen  
    if (moveDown) {
        ballPositionY += currentSpeed +(sin(QUARTER_PI));
    } else {
        ballPositionY -= currentSpeed + (sin(QUARTER_PI));
    }

    if (moveRight) {
        ballPositionX += currentSpeed + (sin(HALF_PI + QUARTER_PI));

    } else {
        ballPositionX -= currentSpeed + (sin(-PI));
    }
}

要评估球是否击中面板中间,必须计算到球拍中心的距离。如果桨距中心的距离较大(未达到MAX_SPEED的距离较大),则提高速度;如果桨距中心的较近位置,则将速度重置为MIN_SPEED

void paddleBounce () {

    boolean hitMousePaddleX = ballPositionX >= (mousePaddleX - ballSize/2) && ballPositionX < mousePaddleX;
    boolean hitMousePaddleY = ballPositionY > (mousePaddleY) && ballPositionY < (mousePaddleY + PADDLE_HEIGHT);
    if (hitMousePaddleX && hitMousePaddleY) {

        float distToMidMouse = abs(ballPositionY - (mousePaddleY + PADDLE_HEIGHT/2));
        if ( distToMidMouse < 20.0 ) {
            currentSpeed = MIN_SPEED;
        } else if (currentSpeed < MAX_SPEED) {
            if (currentSpeed < MAX_SPEED) {
                currentSpeed += 0.5;
            }
        } 

        moveRight = false;
    }

    boolean hitKeyPaddleX = ballPositionX <= (keyPaddleX + PADDLE_WIDTH + ballSize/2) && ballPositionX > keyPaddleX;
    boolean hitKeyPaddleY = ballPositionY < (keyPaddleY + PADDLE_HEIGHT) && ballPositionY > keyPaddleY;
    if (hitKeyPaddleX && hitKeyPaddleY) {

        float distToMidKey = abs(ballPositionY - (keyPaddleY + PADDLE_HEIGHT/2));
        if ( distToMidKey < 20.0 ) {
            currentSpeed = MIN_SPEED;
        } else {
            if (currentSpeed < MAX_SPEED) {
                currentSpeed += 0.5;
            }
        }   

        moveRight = true;
    }
}

如果游戏中出现新球,则将速度重置为MIN_SPEED

void scoreCount () {

    if (ballPositionX  < -ballSize/2) {

    mouseScore += 1;
    currentSpeed = MIN_SPEED;
    }

    if (ballPositionX > width + ballSize) {

    keyScore +=  1;
    currentSpeed = MIN_SPEED;
    }
}

加上屁股,您可以通过提高速度来着色读取的球:

void drawGame () {

    drawScore ();
    defaultBall ();

    //the ball
    float fSpeed = 1.0 - (currentSpeed - MIN_SPEED) / (MAX_SPEED - MIN_SPEED);
    fill(255, 255*fSpeed, 255*fSpeed);
    strokeWeight (2);
    ellipse (ballPositionX, ballPositionY, ballSize, ballSize);

    fill (255);
    strokeWeight (0.8);
    //the left paddle, which is controlled by the keyboard
    rect (keyPaddleX, keyPaddleY, PADDLE_WIDTH, PADDLE_HEIGHT);

    //the right paddle, which is controlled by the mouse
    rect (mousePaddleX, mousePaddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
}

答案 1 :(得分:-1)

您的代码有点混乱,但是我设法找到了解决您问题的方法。 我注意到,球的速度始终为“ MIN_SPEED”,因为这是反弹方法中每次循环迭代对球位置的影响 看完代码后,我注意到在方法“ paddleBounce()”中检测到了与桨的碰撞,所以我去了那里,以MIN_SPEED的较高值为“ 8”为例,最后,我添加了一个lerp绘制事件中的版本,以使MIN_SPEED的值以非线性方式缓慢地回到“ 5”(这样看起来就很好了)

这是最终代码:

 //size of the ball
int ballSize;

//position of the ball
float ballPositionX; 
float ballPositionY; 

//speed of the ball
float ballSpeedX, ballSpeedY;

//size of the paddles
final int PADDLE_WIDTH = (20);
final int PADDLE_HEIGHT = (100);

//coordinates of the LEFT paddle
float keyPaddleY;
float keyPaddleX;

//coordinates of the RIGHT paddle
float mousePaddleX ;
float mousePaddleY;

//score of keyboard
int keyScore = 0;

//score of mouse
int mouseScore = 0;

//movement of the ball
boolean moveDown = true;
boolean moveRight = true;

//gameover
boolean gameOver, moveNewSpeed;

//paddle speed
int paddleSpeed;

 float MIN_SPEED = 3;
final int MAX_SPEED = 6;



void setup () {

  size (500, 500);
  background (#FF7C00);
  frameRate (60);
  ballPositionX = width/2;
  ballPositionY = height/2;

  ballSpeedX = 3;
  ballSpeedY = 3;

  keyPaddleY= 200;
  keyPaddleX = 20;

  mousePaddleX= width - 40;
  mousePaddleY = 200;

  ballSize = 20;

  keyScore = 0;
  mouseScore = 0;

  paddleSpeed = 4;
}

void draw () {

  MIN_SPEED = lerp(MIN_SPEED,3,0.07);


  background (#FF7C00);
  drawGame ();
  bounce ();
  scoreCount ();
  paddleBounce ();
  gameover ();
  canvasBounce ();

  if (keyPressed) {

    if (keyCode == UP) {

      keyPaddleY = keyPaddleY - paddleSpeed;
    }

    /*if ((keyPaddleY == 0) || (keyPaddleY == height )) {

      paddleSpeed = 0;

    }*/ 

    if (keyCode == DOWN) {

      keyPaddleY = keyPaddleY + paddleSpeed;
    }
  }

  /*if ((mousePaddleY == 0) || (mousePaddleY == height )) {

      paddleSpeed = 0;

    }*/

  if (mousePressed) {

    if (mouseButton == LEFT ) {

      mousePaddleY = mousePaddleY - paddleSpeed;
    } 

    if (mouseButton == RIGHT ) {

      mousePaddleY = mousePaddleY + paddleSpeed;
    }
  }
}//VOID BRAC
void drawGame () {

  drawScore ();
  defaultBall ();


  //the ball
  fill ( 255);
  strokeWeight (2);
  ellipse (ballPositionX, ballPositionY, ballSize, ballSize);

  fill (255);
  strokeWeight (0.8);
  //the left paddle, which is controlled by the keyboard
  rect (keyPaddleX, keyPaddleY, PADDLE_WIDTH, PADDLE_HEIGHT);

  //the right paddle, which is controlled by the mouse
  rect (mousePaddleX, mousePaddleY, PADDLE_WIDTH, PADDLE_HEIGHT);
}

void drawScore() {

  textSize(20);

  String toPrint = "Keyboard: " + keyScore;

  text(toPrint, width/4-textWidth(toPrint)/2, 50);

  toPrint = "Mouse: "+ mouseScore;

  text(toPrint, width*3/4-textWidth(toPrint)/2, 50);
}

/*this funtion puts the ball back into the centre of the screen when it fails to hit either
 the paddle or the top or the bottom of the screen 
 */
void defaultBall () {

  if ((ballPositionX < -ballSize/2) || (ballPositionX > width + ballSize)) {

    ballPositionX = width/2;
    ballPositionY = height/2;
  }
}

void scoreCount () {

  if (ballPositionX  < -ballSize/2) {

    mouseScore = mouseScore + 1;
  }

  if (ballPositionX > width + ballSize) {

    keyScore =  keyScore + 1;
  }
}


void bounce () {
  //bouncing off the top and bottom of the screen  
  if (moveDown) {

    ballPositionY += MIN_SPEED +(sin(QUARTER_PI));
  } else {

    ballPositionY -= MIN_SPEED + (sin(QUARTER_PI));
  }

  if (moveRight) {

    ballPositionX += MIN_SPEED + (sin(HALF_PI + QUARTER_PI));

  } else {

    ballPositionX -= MIN_SPEED + (sin(-PI));
  }

  /*if (moveNewSpeed) {

   ballPositionX = ballPositionX + MAX_SPEED + (sin(QUARTER_PI));
   } else {

   ballPositionX = ballPositionX - MAX_SPEED + (cos(QUARTER_PI));
   }
  */
}

void paddleBounce () {


  if (ballPositionX <= (keyPaddleX + PADDLE_WIDTH + ballSize/2) && ballPositionY < (keyPaddleY + 100) && ballPositionY > (keyPaddleY) && ballPositionX > keyPaddleX  ) {

    moveRight = true;
    MIN_SPEED = 8;


  }


  if (ballPositionX >= (mousePaddleX - ballSize/2) && ballPositionY > (mousePaddleY) && ballPositionY < (mousePaddleY + 100) && ballPositionX < mousePaddleX ) {

    moveRight = false;
    MIN_SPEED = 8;


  }

  /*if (ballPositionX == keyPaddleX + PADDLE_WIDTH && ballPositionY == keyPaddleY + PADDLE_WIDTH ) {

   moveNewSpeed  = false;  

   } 

   if (ballPositionX == mousePaddleX && ballPositionY == mousePaddleY) {

   moveNewSpeed = true;

   }   

   */

}

void canvasBounce () {

  if (ballPositionY < ballSize/2) {

    moveDown = true;
  }

  if (ballPositionY > height - ballSize/2) {

    moveDown = false;
  }

}

void gameover () {

  if (gameOver) {

    ballSpeedX = 0;
    ballSpeedY = 0;
    paddleSpeed = 0;
    ballPositionX = width/2;
    ballPositionY = height/2;

    textSize(50);

    fill (random(255), random(255), random (255));

    String toPrint = "GAME OVER!!";

    text(toPrint, width/2-textWidth(toPrint)/2, height/2);
  }

  if (keyScore == 11 || mouseScore == 11) {

    gameOver = true;
  } else {

    gameOver = false;
  }
}