case 4:g.setColor(color);paintLeftTank(x,y,g);break; } } } /**纯画图打造坦克*/ public void paintUpTank(int x,int y,Graphics g){ g.fill3DRect(x, y, 15, 50, false); g.fill3DRect(x+35, y, 15, 50, false); g.fill3DRect(x+15, y+10, 20, 30, false); g.setColor(Color.black); g.fill3DRect(x+23, y-10, 5, 33, false); g.setColor(Color.yellow); g.fillOval(x+20, y+18, 10, 10); } public void paintLeftTank(int x,int y,Graphics g){ g.fill3DRect(x, y, 50, 15, false); g.fill3DRect(x, y+35, 50, 15, false); g.fill3DRect(x+10, y+15, 30, 20, false); g.setColor(Color.black); g.fill3DRect(x-10, y+22, 33, 5, false); g.setColor(Color.yellow); g.fillOval(x+20, y+18, 10, 10); } public void paintDownTank(int x,int y,Graphics g){ g.fill3DRect(x, y, 15, 50, false); g.fill3DRect(x+35, y, 15, 50, false); g.fill3DRect(x+15, y+10, 20, 30, false); g.setColor(Color.black); g.fill3DRect(x+23, y+25, 5, 33, false); g.setColor(Color.yellow); g.fillOval(x+20, y+18, 10, 10); } public void paintRightTank(int x,int y,Graphics g){ g.fill3DRect(x, y, 50, 15, false); g.fill3DRect(x, y+35, 50, 15, false); g.fill3DRect(x+10, y+15, 30, 20, false); g.setColor(Color.black); g.fill3DRect(x+23, y+22, 33, 5, false); g.setColor(Color.yellow); g.fillOval(x+20, y+18, 10, 10); } /**重新开始游戏的时候会清空子弹数组里面的数据*/ public void cleanShoot(){ for (int i = 0; i < Shoot.enemyShoot.length; i++) { Shoot.enemyShoot[i]=0; } for (int i = 0; i < Shoot.myShoot.length; i++) { Shoot.myShoot[i]=0; } } /**子弹消失了*/ public void ShootDispeal(){ for (int i = 0; i < Shoot.myShoot.length; i+=4) {//撞到边缘 if(Shoot.myShoot[i]<0||Shoot.myShoot[i]>WIDTH||Shoot.myShoot[i+1]<0||Shoot.myShoot[i+1]>HEIGHT){ Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0; Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0; shootNum++; shootNum1++; } int x=Shoot.myShoot[i]; int y=Shoot.myShoot[i+1]; //撞到柱子 if((x>330&&x<360&&y>150&&y<380)||(x>130&&x<570&&y>240&&y<280)){ Shoot.myShoot[i]=0;Shoot.myShoot[i+1]=0; Shoot.myShoot[i+2]=0;Shoot.myShoot[i+3]=0; shootNum++; shootNum1++; } } for (int i = 0; i < Shoot.enemyShoot.length; i+=4) {//撞到边缘 if(Shoot.enemyShoot[i]<0||Shoot.enemyShoot[i]>WIDTH||Shoot.enemyShoot[i+1]<0||Shoot.enemyShoot[i+1]>HEIGHT){ Shoot.enemyShoot[i]=0;Shoot.enemyShoot[i+1]=0; Shoot.enemyShoot[i+2]=0;Shoot.enemyShoot[i+3]=0; } int x=Shoot.enemyShoot[i]; int y=Shoot.enemyShoot[i+1]; //撞到柱子 if((x>330&&x<360&&y>150&&y<380)||(x>130&&x<570&&y>240&&y<280)){ Shoot.enemyShoot[i]=0;Shoot.enemyShoot[i+1]=0; Shoot.enemyShoot[i+2]=0;Shoot.enemyShoot[i+3]=0; } } } }
/********************Tanks类***********************/
package tank; import java.util.Random; /**坦克父类,继承了线程*/ public class Tanks extends Thread{ private int x;//坦克坐标x private int y;//坦克坐标y private int step;//坦克的速度 private int direct;//方向,1表示向上,2表示向右,3表示向下,4表示向左 private int who;//坦克标识,0和-1表示自己的坦克,1,2,3表示敌人的坦克,颜色随机. private boolean isLive = true;//判断坦克是否死亡 Shoot shoot;//shoot坦克的射击 public Tanks(int x, int y, int step, int direct,int who ,boolean isLive) { super(); this.x = x; this.y = y; this.step = step+2; this.direct = direct; this.who = who; this.isLive = isLive; } public boolean canMove(){//boolean返回值类型.判断坦克是否能移动,比如撞墙了. if(x<0){ x=0; return false; }else if(x>690){ |