Tank Game bug

关于上一篇V0.2游戏的一个bug

防止敌人坦克重叠运动

public boolean isTouchOtherEnemy() {
		boolean b = false;

		for (int i = 0; i < ets.size(); i++) {
			EnemyTank tank = ets.get(i);
			if (tank != this) {
				if (this.x >= tank.x && this.x <= tank.x + 30 && this.y >= tank.y && this.y <= tank.y + 30) {
					return true;
				}

				if (this.x + 30 >= tank.x && this.x + 30 <= tank.x + 30 && this.y >= tank.y && this.y <= tank.y + 30) {
					return true;
				}

				if (this.x >= tank.x && this.x <= tank.x + 30 && this.y + 30 >= tank.y && this.y + 30 <= tank.y + 30) {
					return true;
				}

				if (this.x + 30 >= tank.x && this.x + 30 <= tank.x + 30 && this.y + 30 >= tank.y
						&& this.y + 30 <= tank.y + 30) {
					return true;
				}

			}
		}

		return b;
	}

分析了4种 坦克互相intersects情况,但是上述代码导致只要发生一次cover,就无法再次分开;

原因是:坦克cover后不管方向是什么都会停止运动,故 isTouchOtherEnemy要建立在坦克的方向才来判断是否重叠。

举例:如果坦克1和坦克二有碰撞,这是二者都需要等待,然后二者都会进行随机方向,假设他们的方向改变了,按照新的方向运动不会有重合,但是由于isTouchOtherEnemy函数任然得到Touch 为True的结果,所以相遇的坦克不会分开,即便是改变成合理的方向。

故:上面这种isTouchOtherEnemy函数错误,应该在某个方向上判断是否重合


    public boolean isTouchOtherEnemy() {  
        boolean b = false;  
  
        switch (this.direction) {  
        case 0:// UP  
            for (int i = 0; i < ets.size(); i++) {  
                EnemyTank et = ets.get(i);  
                if (et != this) {  
                    if (et.direction == 0 || et.direction == 1) {  
                        if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {  
                            System.out.println(1);  
                            return true;  
                        }  
                        if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {  
                            System.out.println(2);  
                            return true;  
                        }  
                    }  
                    if (et.direction == 2 || et.direction == 3) {  
                        if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {  
                            System.out.println(3);  
                            return true;  
                        }  
                        if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {  
                            System.out.println(4);  
                            return true;  
                        }  
                    }  
                }  
            }  
  
            break;  
        case 1:// DOWN  
            for (int i = 0; i < ets.size(); i++) {  
                EnemyTank et = ets.get(i);  
                if (et != this) {  
                    if (et.direction == 0 || et.direction == 1) {  
                        if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {  
                            System.out.println(5);  
                            return true;  
                        }  
                        if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y  
                                && this.y + 30 <= et.y + 30) {  
                            System.out.println(6);  
                            return true;  
                        }  
                    }  
                    if (et.direction == 2 || et.direction == 3) {  
                        if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {  
                            System.out.println(7);  
                            return true;  
                        }  
  
                        if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y  
                                && this.y + 30 <= et.y + 30) {  
                            System.out.println(8);  
                            return true;  
                        }  
                    }  
                }  
            }  
  
            break;  
        case 2:// LEFT  
            for (int i = 0; i < ets.size(); i++) {  
                EnemyTank et = ets.get(i);  
                if (et != this) {  
                    if (et.direction == 0 || et.direction == 1) {  
                        if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {  
                            System.out.println(9);  
                            return true;  
                        }  
                        if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {  
                            System.out.println(10);  
                            return true;  
                        }  
                    }  
                    if (et.direction == 2 || et.direction == 3) {  
                        if (this.x >= et.x && this.x <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {  
                            System.out.println(11);  
                            return true;  
                        }  
                        if (this.x >= et.x && this.x <= et.x + 30 && this.y + 30 >= et.y && this.y + 30 <= et.y + 30) {  
                            System.out.println(12);  
                            return true;  
                        }  
                    }  
                }  
            }  
  
            break;  
        case 3:// RIGHT  
            for (int i = 0; i < ets.size(); i++) {  
                EnemyTank et = ets.get(i);  
                if (et != this) {  
                    if (et.direction == 0 || et.direction == 1) {  
                        if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {  
                            System.out.println(13);  
                            return true;  
                        }  
                        if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y  
                                && this.y + 30 <= et.y + 30) {  
                            System.out.println(14);  
                            return true;  
                        }  
                    }  
                    if (et.direction == 2 || et.direction == 3) {  
                        if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y >= et.y && this.y <= et.y + 30) {  
                            System.out.println(15);  
                            return true;  
                        }  
                        if (this.x + 30 >= et.x && this.x + 30 <= et.x + 30 && this.y + 30 >= et.y  
                                && this.y + 30 <= et.y + 30) {  
                            System.out.println(16);  
                            return true;  
                        }  
                    }  
                }  
            }  
  
            break;  
        }  
  
        return b;  
    } 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值