Processing 当球碰到桨叶边缘时,如何提高球的速度,然后恢复到原来的速度?

Processing 当球碰到桨叶边缘时,如何提高球的速度,然后恢复到原来的速度?,processing,Processing,当球碰到桨叶边缘时,如何提高球的速度,然后恢复到原来的速度?在这个乒乓球游戏中,我希望我的球在碰到球拍边缘时加快速度,然后在碰到球拍中间时恢复到正常速度。我评论了一些“if语句”,因为我尝试了它们,但它们不起作用。我使用的语言是“处理”您的代码有点混乱,但我设法找到了解决您问题的方法。 我注意到球的速度总是“MIN_speed”,因为在弹跳方法的每次循环迭代中,这就是你对球位置的影响 查看代码后,我注意到在方法“pailebounce()”中检测到与划桨的碰撞,因此我去了那里,并以更高的值“8”

当球碰到桨叶边缘时,如何提高球的速度,然后恢复到原来的速度?在这个乒乓球游戏中,我希望我的球在碰到球拍边缘时加快速度,然后在碰到球拍中间时恢复到正常速度。我评论了一些“if语句”,因为我尝试了它们,但它们不起作用。我使用的语言是“处理”

您的代码有点混乱,但我设法找到了解决您问题的方法。 我注意到球的速度总是“MIN_speed”,因为在弹跳方法的每次循环迭代中,这就是你对球位置的影响 查看代码后,我注意到在方法“pailebounce()”中检测到与划桨的碰撞,因此我去了那里,并以更高的值“8”为例影响MIN_SPEED,最后我在draw事件中添加了一个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;
  }
}
//球的大小
国际球码;
//球的位置
浮球位置X;
浮球定位;
//球的速度
浮球速度x,球速度;
//桨的大小
最终内桨_宽度=(20);
最终内桨高度=(100);
//左桨的坐标
浮动键桨;
浮动键盘;
//右桨的坐标
浮动鼠标垫;
浮动鼠标垫;
//键盘乐谱
int keyScore=0;
//老鼠得分
int mouseScore=0;
//球的运动
布尔值moveDown=true;
布尔值moveRight=true;
//游戏结束
布尔gameOver,moveNewSpeed;
//桨速
速度;
浮动最小速度=3;
最终int最大速度=6;
无效设置(){
大小(500500);
背景(FF7C00);
帧率(60);
ballPositionX=宽度/2;
球位Y=高度/2;
ballSpeedX=3;
b=3;
Keypailey=200;
键盘X=20;
mousePaddleX=宽度-40;
mousepadley=200;
球径=20;
keyScore=0;
mouseScore=0;
桨速=4;
}
无效提款(){
最小速度=lerp(最小速度,3,0.07);
背景(FF7C00);
抽签游戏();
反弹();
分数计数();
划桨反弹();
gameover();
拉票();
如果(按键){
if(keyCode==UP){
Keypailey=Keypailey-桨叶速度;
}
/*如果((键桨==0)| |(键桨==高度)){
桨速=0;
}*/ 
如果(键代码==向下){
键桨Y=键桨Y+桨速;
}
}
/*如果((mousepadley==0)| |(mousepadley==height)){
桨速=0;
}*/
如果(鼠标按下){
如果(鼠标按钮==左){
mousepadley=mousepadley-桨速;
} 
如果(鼠标按钮==右){
鼠标垫=鼠标垫+划桨速度;
}
}
}//空苞
虚空抽签游戏(){
抽签分数();
defaultBall();
//球
填充(255);
冲程重量(2);
椭圆(ballPositionX、ballPositionY、ballSize、ballSize);
填充(255);
冲程重量(0.8);
//左拨杆,由键盘控制
rect(键桨X、键桨Y、桨宽、桨高);
//右桨,由鼠标控制
rect(鼠标垫、鼠标垫、桨叶宽度、桨叶高度);
}
无效分数(){
文本大小(20);
字符串toPrint=“键盘:”+keyScore;
文本(toPrint,宽度/4-textWidth(toPrint)/2,50);
toPrint=“鼠标:”+mouseScore;
文本(toPrint,宽度*3/4-textWidth(toPrint)/2,50);
}
/*当球打不到屏幕中央时,此功能将球放回屏幕中央
挡板或屏幕的顶部或底部
*/
void defaultBall(){
如果((球位X<-球号/2)| |(球位X>宽度+球号)){
ballPositionX=宽度/2;
球位Y=高度/2;
}
}
无效记分计数(){
如果(球位置X<-球尺寸/2){
鼠标孔=鼠标孔+1;
}
如果(球位置X>宽度+球尺寸){
键芯=键芯+1;
}
}
无效反弹(){
//从屏幕的顶部和底部反弹
如果(向下移动){
球位置Y+=最小速度+(sin(四分之一π));
}否则{
球位置Y-=最小速度+(sin(四分之一π));
}
如果(向右移动){
ballPositionX+=最小速度+(sin(半圆周率+四分之一圆周率));
}否则{
ballPositionX-=最小速度+(sin(-PI));
}
/*如果(移动新闻速度){
球位置X=球位置X+最大速度+(sin(四分之一π));
}否则{
ballPositionX=ballPositionX-最大速度+(cos(四分之一π));
}
*/
}
虚空弹跳(){
如果(球位置X(键拨杆)和球位置X>键拨杆X){
moveRight=true;
最小速度=8;
}
如果(ballPositionX>=(mousePaddleX-ballSize/2)和&ballPositionY>(mousePaddleY)和&ballPositionY<(mousePaddleY+100)和&ballPositionX高度-球尺寸/2){
moveDown=false;
}
}
void gameover(){
如果(游戏结束){
ballSpeedX=0;
ball=0;
桨速=0;
ballPositionX=宽度/2;
球位Y=高度/2;
文本大小(50);
填充(随机(255)、随机(255)、随机(255));
String toPrint=“游戏结束!!”;
文本(TopPrint,宽度/2-textWidth(TopPrint)/2,高度/2);
}
如果(键芯==11 | |鼠标芯==11){
gameOver=true;
}否则{
gameOver=false;
}
}
我想让我的球在碰到桨的边缘时加快速度,然后在碰到桨的中间时恢复到正常速度

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

fina