这是 12 年级的 OPP 作业。
目前我有一个问题,球偶尔会进入积木而不是反射。
这是我的代码:
main 如何检查球是否击中障碍物:
public void theBall(){
//System.out.println(blocks.size());
if(!started){
balls.get(0).withPaddle(paddle.returnX());
}
if(started){
for(int i=0;i<balls.size();i++){
if(balls.get(i).returnX()>=0 && balls.get(i).returnX()<=800 && balls.get(i).returnY()>=0){
balls.get(i).move();
}
if(balls.get(i).returnX()+balls.get(i).returnDx()<= 0){
balls.get(i).collide(1);//collide with left wall
}
if(balls.get(i).returnX()+10+balls.get(i).returnDx() >= 800){
balls.get(i).collide(2);//collide with right wall
}
if(balls.get(i).returnY()-balls.get(i).returnDy() <= 0){
balls.get(i).collide(3);//collide with up wall
}
if(paddle.returnX() < balls.get(i).returnX() && balls.get(i).returnX()< paddle.returnX()+paddle.returnSize() && balls.get(i).returnY()-balls.get(i).returnDy() >= 545){//fix this && balls.get(i).returnY()-balls.get(i).returnDy() <= 560
balls.get(i).collidePaddle(balls.get(i).returnX()-paddle.returnX());//collide with paddle
//System.out.println(ball.returnDy());
}
for(int j=0;j<blocks.size();j++){
//blocks.get(j).getHit(balls.get(i));
if(balls.get(i).returnX()+10+balls.get(i).returnDx() >= blocks.get(j).returnX() && balls.get(i).returnX()+10+balls.get(i).returnDx() <= blocks.get(j).returnX()+5 && balls.get(i).returnY() >= blocks.get(j).returnY() && balls.get(i).returnY() < blocks.get(j).returnY()+50){
if(!blocks.get(j).returnHit()){
balls.get(i).collide(2);//collide with right wall (block's left)
blocks.get(j).getHit(balls.get(i));
System.out.println("boiboiboiboibo1");
}
}
if(balls.get(i).returnX()+balls.get(i).returnDx() <= blocks.get(j).returnX()+50 && balls.get(i).returnX()+balls.get(i).returnDx() >= blocks.get(j).returnX()+45 && balls.get(i).returnY() >= blocks.get(j).returnY() && balls.get(i).returnY() < blocks.get(j).returnY()+50){
if(!blocks.get(j).returnHit()){
balls.get(i).collide(1);//collide with left wall (block's right)
blocks.get(j).getHit(balls.get(i));
System.out.println("boiboiboiboibo2");
}
}
if(balls.get(i).returnX() >= blocks.get(j).returnX() && balls.get(i).returnX() < blocks.get(j).returnX()+50 && balls.get(i).returnY()-balls.get(i).returnDy() <= blocks.get(j).returnY()+50 && balls.get(i).returnY()-balls.get(i).returnDy() >= blocks.get(j).returnY()+45){
if(!blocks.get(j).returnHit()){
balls.get(i).collide(3);//collide with up wall (block's bottom)
blocks.get(j).getHit(balls.get(i));
System.out.println("boiboiboiboibo3");
}
}
if(balls.get(i).returnX() >= blocks.get(j).returnX() && balls.get(i).returnX() < blocks.get(j).returnX()+50 && balls.get(i).returnY()+10-balls.get(i).returnDy() >= blocks.get(j).returnY() && balls.get(i).returnY()+10-balls.get(i).returnDy() <= blocks.get(j).returnY()+5){
if(!blocks.get(j).returnHit()){
balls.get(i).collide(4);//collide with down wall (block's top)
blocks.get(j).getHit(balls.get(i));
System.out.println("boiboiboiboibo4");
}
}
}
for(int j=0;j<specialBlocks.size();j++){
if(balls.get(i).returnX()>=specialBlocks.get(j).returnX() && balls.get(i).returnX()<=specialBlocks.get(j).returnX()+100 && balls.get(i).returnY()>=specialBlocks.get(j).returnY() && balls.get(i).returnY()<=specialBlocks.get(j).returnY()+100){
oneAndThreeBalls(i, j);
balls.get(i).changeType(j);
}
}
}
}
}
球如何反射:
public void collide(int collideWith){
if(collideWith==1){//collide with left wall
if(90<direction && direction<180){
direction = 180-direction;
}
if(180<direction && direction<270){
direction = 540-direction;
}
}
if(collideWith==2){//collide with right wall
if(0<direction && direction<90){
direction = 180-direction;
}
if(270<direction && direction<360){
direction = 540-direction;
}
}
if(collideWith==3){//collide with up wall
direction = 360-direction;
}
if(collideWith==4){//collide with down wall
direction = 360-direction;
}
}
我很清楚这个错误是由于对边界的双重检查。当球在一个街区的角落时,它就会发生。但这是我能想到的检查球的位置以反映的唯一方法。关于如何修复此错误的任何想法?谢谢你。