当球击中桨的边缘时,如何增加球的速度然后 return 回到原来的速度?
How to increase the speed of the ball when it hits the edge of a paddle and then return back to original speed?
如何提高球击中球拍边缘时的速度,然后return回到原来的速度?在这个乒乓球游戏中,我希望我的球在击中球拍边缘时加快速度,然后 return 在击中球拍中间时恢复到正常速度。我评论了一些 "if statements" 因为我试过它们但它们没有用。我使用的语言是 "processing"
你的代码有点乱,但我设法找到了解决你问题的方法。
我注意到球的速度总是 "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;
}
}
I want my ball to increase its speed when it hits the edge of a paddle and then return back to its normal speed when it hits the middle of the paddle
添加一个保存当前速度的变量:
final int MIN_SPEED = 3;
final int MAX_SPEED = 6;
float currentSpeed = (float)MIN_SPEED;
在函数 bounce
:
中使用此变量代替 MIN_SPEED
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);
}
如何提高球击中球拍边缘时的速度,然后return回到原来的速度?在这个乒乓球游戏中,我希望我的球在击中球拍边缘时加快速度,然后 return 在击中球拍中间时恢复到正常速度。我评论了一些 "if statements" 因为我试过它们但它们没有用。我使用的语言是 "processing"
你的代码有点乱,但我设法找到了解决你问题的方法。 我注意到球的速度总是 "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;
}
}
I want my ball to increase its speed when it hits the edge of a paddle and then return back to its normal speed when it hits the middle of the paddle
添加一个保存当前速度的变量:
final int MIN_SPEED = 3;
final int MAX_SPEED = 6;
float currentSpeed = (float)MIN_SPEED;
在函数 bounce
:
MIN_SPEED
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);
}