x=690; return false; }else if(y<0){ y=0; return false; }else if(y>450){ y=450; return false; }else if(x>270&&x<370&&y>100&&y<380){//撞到竖着的柱子 if(x-270<20){ x=270; }else{ x=370; } return false; }else if(x>85&&x<570&&y>195&&y<290){//撞到横着的柱子 if(y-195<10){ y=190; }else{ y=290; } return false; } return true; } public void moveUp(){//坦克向上移动一步 if(canMove()){ y-=step; direct=1; } } public void moveDown(){//坦克向下移动一步 if(canMove()){ y+=step; direct=3; } } public void moveLeft(){//坦克向左移动一步 if(canMove()){ x-=step; direct=4; } } public void moveRight(){//坦克向右移动一步 if(canMove()){ x+=step; direct=2; } } public void shoot(){//每次射击调用此函数 shoot = new Shoot(x,y,direct,step,who); } public String toString() {//用于调试 return "Tanks [direct=" + direct + ", isLive=" + isLive + ", step=" + step + ", x=" + x + ", y=" + y + "]"; } /**以下是get() set()函数*/ public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getStep() { return step; } public void setStep(int step) { this.step = step; } public int getDirect() { return direct; } public void setDirect(int direct) { this.direct = direct; } public boolean isLive() { return isLive; } public void setLive(boolean isLive) { this.isLive = isLive; } public void setWho(int who) { this.who = who; } public int getWho() { return who; } } class MyTank extends Tanks{//我的坦克继承了Tanks public MyTank(int x, int y, int step, int direct, int who ,boolean isLive) { super(x, y, step, direct, who, isLive); } } class EnemyTanks extends Tanks{//敌人的坦克继承了tanks,也继承了父类的线程 private int time = 500;//线程占用时间500毫秒 boolean s=true;//定义一个boolean的开关,坦克死亡,关闭开关,线程死亡. public EnemyTanks(int x, int y, int step, int direct, int who, boolean isLive) { super(x, y, step, direct, who,isLive); } public void move(){//敌人的坦克自己移动 Random r = new Random(); int random = r.nextInt(50); int r1=r.nextInt(4); if(!(r1==0)){//如果r1=0,就射击一次 shoot(); } for (int i = 0; i < random; i++) {//random表示坦克的一次随机移动的距离 try { Thread.sleep(50);//线程sleep,移动使移动看起来自然一点 } catch (InterruptedException e) { e.printStackTrace(); } if(r1==0){//根据r1的值判断,此次移动往那个方向 if(this.getY()==0||this.getY()==290){ r1=3; }else{ moveUp(); } }else if(r1==1){ if(this.getX()==690||this.getX()==270){ r1=2; }else{ moveRight(); } }else if(r1==2){ if(this.getX()==0||this.getX()==370){ r1=1; }else{ moveLeft(); } }else if(r1==3){ if(this.getY()==450||this.getY()==190){ r1=0; }else{ moveDown(); } } } } public void run(){//继承线程功能,必须实现run()函数 while(s){//s=true表示坦克没有死亡.s=false跳出死循环,坦克死亡 move(); try { Thread.sleep(time);//每次线程占用时间500毫秒 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
/*******************
|