我在java的ping pong游戏中有球弹跳错误 [英] I have bouncing error with ball in ping pong game in java
本文介绍了我在java的ping pong游戏中有球弹跳错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不幸的是球的运动是非常重复的,尽管有一些努力我不能在球界上反弹,所以我需要你的帮助:
>
public void run(){
boolean bounceBall = false;
while (true){
if (ball_Service){
if (leftDisplacement&& ball_X> BALL_X_MIN){
bounceBall =( ball_Y> = computerPaddle_Y&& ball_Y<(computerPaddle_Y + LENGTH_PADDLES));
ball_X = ball_X - SPEED_BALL;
ball_Y = ball_Y - verticalDisplacement;
table.ballPosition(ball_X,ball_Y);
if (ball_X< = PADDLES_COMPUTER_X&& bounceBall){
leftDisplacement = false;
}
}
如果(!leftDisplacement&& ball_X< = table.ball_x_max){
bounceBall =(ball_Y> = playerPaddle_Y&& ball_Y<(playerPaddle_Y + LENGTH_PADDLES));
ball_X = ball_X + SPEED_BALL;
table.ballPosition(ball_X,ball_Y);
if (ball_X> = table.place_paddles&& bounceBall){
leftDisplacement = true;
}
}
if (computerPaddle_Y< ball_Y&& computerPaddle_Y< table.down_Table){
computerPaddle_Y = computerPaddle_Y + SPEED_PADDLES;
} 其他 如果(computerPaddle_Y> TOP_TABLE){
computerPaddle_Y = computerPaddle_Y - SPEED_PADDLES;
}
table.computerMovementPaddle(computerPaddle_Y);
if (ballInPlay()){
if (ball_X> table.ball_x_max){
score_computer ++;
displayScore();
} else if (ball_X< BALL_X_MIN){
score_plyer ++;
displayScore();
}
}
}
尝试 {
Thread.sleep (table.slow_game);
} catch (InterruptedException exception){
exception.printStackTrace();
}
}
}
解决方案
如果球移动出现问题,分数没有被更新然后错误很可能在你的ballInPlay()函数中 - 因为如果返回false则分数不会改变。
或者可能有一些问题
如果(ball_X > table.ball_x_max){
score_computer ++;
displayScore();
} else if (ball_X < BALL_X_MIN){
score_plyer ++;
displayScore();
,因为你似乎有table.ball_x_max和BALL_X_MIN - 你有没有想要table.ball_x_min?
unfortunately the ball movement is very repetitive, Despite of some effort I can not bounce the ball on the bounds, so I need your help:
public void run() {
boolean bounceBall = false;
while (true) {
if (ball_Service) {
if (leftDisplacement && ball_X > BALL_X_MIN) {
bounceBall = (ball_Y >= computerPaddle_Y && ball_Y < (computerPaddle_Y + LENGTH_PADDLES));
ball_X = ball_X - SPEED_BALL;
ball_Y = ball_Y - verticalDisplacement;
table.ballPosition(ball_X, ball_Y);
if (ball_X <= PADDLES_COMPUTER_X && bounceBall) {
leftDisplacement = false;
}
}
if (!leftDisplacement && ball_X <= table.ball_x_max) {
bounceBall = (ball_Y >= playerPaddle_Y && ball_Y < (playerPaddle_Y + LENGTH_PADDLES));
ball_X = ball_X + SPEED_BALL;
table.ballPosition(ball_X, ball_Y);
if (ball_X >= table.place_paddles && bounceBall) {
leftDisplacement = true;
}
}
if (computerPaddle_Y < ball_Y && computerPaddle_Y < table.down_Table) {
computerPaddle_Y = computerPaddle_Y + SPEED_PADDLES;
} else if (computerPaddle_Y > TOP_TABLE) {
computerPaddle_Y = computerPaddle_Y - SPEED_PADDLES;
}
table.computerMovementPaddle(computerPaddle_Y);
if (ballInPlay()) {
if (ball_X > table.ball_x_max) {
score_computer++;
displayScore();
} else if (ball_X < BALL_X_MIN) {
score_plyer++;
displayScore();
}
}
}
try {
Thread.sleep(table.slow_game);
} catch (InterruptedException exception) {
exception.printStackTrace();
}
}
}
解决方案
If there is a problem with the ball moving off and the score not being updated then the error is most likely in your ballInPlay() function - because if that returns false the score will not change.
Alternatively there may be some problem with
if (ball_X > table.ball_x_max) { score_computer++; displayScore(); } else if (ball_X < BALL_X_MIN) { score_plyer++; displayScore();
as you seem to have table.ball_x_max and BALL_X_MIN - did you maybe mean table.ball_x_min?
这篇关于我在java的ping pong游戏中有球弹跳错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文