《JAVA程序开发课程设计》项目设计项目名称:TankWar 软件专业:软件工程班级:13软工1班姓名:毛晨光学号:1322120124一、需求分析:基本功能:1.玩家控制的坦克能够四处移动并且打击敌方坦克;2.敌方坦克能够随机四处移动并且打击玩家控制的坦克;3.玩家控制的坦克拥有血量,而敌方坦克没有;4.坦克受到攻击时血条会缩短;5.敌方坦克被消灭完之后,提示游戏胜利;6.用户方坦克被消灭后提示游戏结束;特色功能:1.坦克具有图片,不单单只是个圈圈。
2.增加了血包功能,地图上会随机出现一个血包,我方坦克开过会增加血量。
二、系统设计:1.TankMap类:实现游戏界面地图的初始化。
2.PainTread类:绘制和重绘功能。
3.DirectionHandler:监听用户的键盘输入。
4.Tank类:实现坦克的初始化,绘制,移动,发射等功能。
5.EnemyTank:实现敌方坦克的初始化,绘制,移动,发射等功能。
6.Shell类:实现炮弹的初始化,绘制,移动,攻击功能。
7.Explor类:实现爆炸的初始化。
绘制功能,爆炸效果由绘制半径从小到大再到小的圆实现。
8.Direction类:包含枚举。
9.Blood类:用于实现血包的功能。
三、功能实现。
一.绘制地图功能:public class TankMap extends Frame{//定义地图的尺寸。
public static final int MAPWIDTH=800;public static final int MAPHEIGHT=600;//我方坦克Tank t=null;//定义随机出现的血包Random r=new Random();Image bufferImage=null;public static java.util.List<Shell>shells=new ArrayList<Shell>();//地方坦克集合public static java.util.List<EnemyTank>->enemys=new ArrayList<EnemyTank>();public static java.util.List<Blood> bloods=new ArrayList<Blood>();//爆炸集合public.static.java.util.List<Explor>explors=new java.util.ArrayList<Explor>(); //敌方坦克数量默认10个public int enemyCount=5;//主方法public static void main(String[] args) {TankMap tv=new TankMap();tv.init();}public void drawImage(){}//地图初始化方法public void init(){//初始化地图this.setSize(MAPWIDTH,MAPHEIGHT);this.setTitle("TankWar");this.setVisible(true);//添加键盘监听器this.addKeyListener(new DirectionHandler());//添加穿口关闭监听器this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent arg0){System.exit(0);}});//初始化我方坦克t=new Tank(this);//初始化敌方坦克for(int i=0;i<enemyCount;i++){enemys.add(new EnemyTank(40+30*i,80,Color.YELLOW)); }//启动绘制线程new Thread(new PaintThread()).start();}//@Overridepublic void paint(Graphics g){//画地图Color c=g.getColor();g.setColor(Color.GREEN);g.fillRect(0,0,MAPWIDTH,MAPHEIGHT);g.setColor(Color.RED);g.drawString("当前炮弹数目:"+shells.size(),20,40);g.drawString("生命值:",20,60);g.fillRect(65,55,t.getLife(),5);g.setColor(c);//画坦克t.draw(g);if(r.nextInt(10)==9 && bloods.size()==0)bloods.add(new Blood());if(r.nextInt(60)==7 && bloods.size()==1)bloods.remove(0);for(int i=0;i<enemys.size();i++){EnemyTank et=enemys.get(i);et.draw(g);}//画爆炸for(int i=0;i<explors.size();i++){Explor e=explors.get(i);e.draw(g);}//绘制血包for(int i=0;i<bloods.size();i++){Blood b=bloods.get(i);b.bloodb(t);b.draw(g);}//画炮弹for(int i=0;i<shells.size();i++){Shell s=shells.get(i);if(s.islive&&s.isgood){s.hitTanks(enemys);}else if(s.islive&&!s.isgood){s.hitTank(t);}s.draw(g);}}//双缓冲,防止游戏地图“频闪”public void update(Graphics g){paint(g);if(bufferImage==null){bufferImage=this.createImage(MAPWIDTH,MAPHEIGHT);}Graphics gbuffer=bufferImage.getGraphics();Color c=gbuffer.getColor();gbuffer.setColor(Color.GREEN);gbuffer.fillRect(0, 0, MAPWIDTH, MAPHEIGHT);gbuffer.setColor(Color.RED);gbuffer.drawString("当前炮弹数目:"+shells.size(),20,40);gbuffer.drawString("生命值:", 20, 60);gbuffer.fillRect(65,55,t.getLife(),5);gbuffer.setColor(c);paint(gbuffer);g.drawImage(bufferImage, 0, 0, null);}//内部类,主要通过调用repaint方法,实现绘图功能class PaintThread implements Runnable{@Overridepublic void run(){while(true){repaint();try{Thread.sleep(260);}catch(InterruptedException e){e.printStackTrace();}}}}//内部类,键盘监听器class DirectionHandler extends KeyAdapter{@Overridepublic void keyPressed(KeyEvent arg0){t.checkDirection(arg0);}}二.绘制坦克功能:public class Tank {//坦克的尺寸和移动速度public static final int WIDTH=50,HEIGHT=50,xspeed=5,yspeed=5;//坦克的初始位置public int x=400,y=300;//坦克的初始化方向,STOP即停止,Direction是自定义的枚举常量,见枚举源代码public Direction direction=Direction.STOP;//地图TankMap tm;//坦克是否存活boolean isLive=true;//坦克属于哪一方,true为用户方,false为敌方boolean isgood=true;//坦克初始化生命值int life=100;//定义坦克的初始化方向,以便插图坦克的图片public int zhuanxiang=1;//获取和设置存活状况public boolean isLive(){return isLive;}public void setLive(boolean isLive){this.isLive=isLive;}//获取和设置生命值public int getLife(){return life;}public void setLife(int life){this.life=life;}//构造方法public Tank(){};public Tank(TankMap t){tm=t;}//绘制坦克//根据坦克的移动方向,插入不同的坦克的图片public void draw(Graphics g){if(zhuanxiang==1){ImageIcon icon=new ImageIcon("Etank_shang.gif");Image tankImage=icon.getImage();g.drawImage(tankImage,x,y,null);}else if(zhuanxiang==-2){ImageIcon icon=new ImageIcon("Etank_you.gif");Image tankImage=icon.getImage();g.drawImage(tankImage,x,y,null);}else if(zhuanxiang==2){ImageIcon icon=new ImageIcon("Etank_zuo.gif");Image tankImage=icon.getImage();g.drawImage(tankImage,x,y,null);}else{ImageIcon icon=new ImageIcon("Etank_xia.gif");Image tankImage=icon.getImage();g.drawImage(tankImage,x,y,null);}/*Color c=g.getColor();g.setColor(Color.RED);g.fillOval(x,y,WIDTH,HEIGHT);g.setColor(c);*/move(direction);}//根据方向,移动坦克,不允许坦克移出地图尺寸public void move(Direction d){if(d==Direction.STOP){}else if(d==Direction.UP){zhuanxiang=1;y -= yspeed;if(y<=20)y=20;}else if(d==Direction.RIGHT){zhuanxiang=-2;x+=xspeed;if(x>=800-WIDTH)x=800-WIDTH;}else if(d==Direction.DOWN){zhuanxiang=-1;y+=yspeed;if(y>=600-HEIGHT)y=600-HEIGHT;}else if(d==Direction.LEFT){zhuanxiang=2;x-=xspeed;if(x<=0)x=0;}}//确定用户单机的方向键,修正坦克即将移动的方向public void checkDirection(KeyEvent k){if(k.getKeyCode()==KeyEvent.VK_UP){direction=Direction.UP;}else if(k.getKeyCode()==KeyEvent.VK_RIGHT){direction=Direction.RIGHT;}else if(k.getKeyCode()==KeyEvent.VK_DOWN){ direction=Direction.DOWN;}else if(k.getKeyCode()==KeyEvent.VK_LEFT){direction=Direction.LEFT;}if(k.getKeyCode()==KeyEvent.VK_F){fire();}}//发射炮弹的方法public void fire(){tm.shells.add(newShell(this.x+WIDTH/2,this.y+HEIGHT/2,this.direction,tm,Color.RED,true));}//返回坦克当前位置矩形,便于判断是否与敌方坦克或炮弹重叠public Rectangle getRec(){return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT);}}三.敌方坦克的绘制方法public class EnemyTank extends Tank{//初始化敌方坦克的属性public static final int WIDTH=50,HEIGHT=50,xspeed=5,yspeed=5;public int x=400,y=300;public Direction direction=Direction.DOWN;public boolean islive=true;Color color=Color.YELLOW;TankMap tm;//随机数产生器,供敌方坦克随机产生前进方向使用Random r=new Random();public int zhuanxiang;//方向更改限制,避免敌方坦克每移动一次就更改方向int randomCount=r.nextInt(10)+5;//构造方法public EnemyTank(){}public EnemyTank(int wx,int wy,Color c){x=wx;y=wy;color=c;}@Overridepublic void draw(Graphics g){//根据敌方坦克的移动方向插入不同的坦克图片if(zhuanxiang==1){ImageIcon icon=new ImageIcon("Etank_shang.gif");Image tankImage=icon.getImage();g.drawImage(tankImage,x,y,null);}else if(zhuanxiang==-2){ImageIcon icon=new ImageIcon("Etank_you.gif");Image tankImage=icon.getImage();g.drawImage(tankImage,x,y,null);}else if(zhuanxiang==2){ImageIcon icon=new ImageIcon("Etank_zuo.gif");Image tankImage=icon.getImage();g.drawImage(tankImage,x,y,null);}else{ImageIcon icon=new ImageIcon("Etank_xia.gif");Image tankImage=icon.getImage();g.drawImage(tankImage,x,y,null);}move();}public void move(){//当方向更改限制为0时,产生一个随机方向if(randomCount==0){Direction dirs[]=Direction.values();direction = dirs[r.nextInt(dirs.length-1)];randomCount = r.nextInt(10)+5;}//方向更改限制减1randomCount--;//随机发射炮弹,当产生的随机数大于35时,发射炮弹if(r.nextInt(40)>36){fire();}//确定敌方的坦克的移动if(direction==Direction.UP){y-=yspeed;zhuanxiang=1;if(y<=HEIGHT)y=HEIGHT;}else if(direction==Direction.RIGHT){x+=xspeed;zhuanxiang=-2;if(x>=800-WIDTH)x=800-WIDTH;}else if(direction==Direction.DOWN){y+=yspeed;zhuanxiang=-1;if(y>=600-HEIGHT)y=600-HEIGHT;}else if(direction==Direction.LEFT){x-=xspeed;zhuanxiang=2;if(x<=0)x=0;}}@Overridepublic void fire(){tm.shells.add(newShell(this.x+WIDTH/2,this.y+HEIGHT/2,this.direction,tm,Color.BLUE,false) );}public Rectangle getRec(){return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT);}}四.炮弹绘制方法public class Shell {//初始化炮弹的基本属性public final int WIDTH=5,HEIGHT=5,xspeed=14,yspeed=14;public int x,y;Direction dir=Direction.STOP;public boolean islive=true;TankMap tm=null;Color color=Color.RED;boolean isgood=true;boolean isLive=true;//构造方法public Shell(){}public Shell(int xd,int yd,Direction d,TankMap t){x=xd;y=yd;dir=d;tm=t;}public Shell(int xd,int yd,Direction d,TankMap t,Color c,boolean g){ x=xd;y=yd;dir=d;tm=t;color=c;isgood=g;}//绘制炮弹public void draw(Graphics g){if(islive){Color c=g.getColor();g.setColor(color);g.fillOval(x,y,WIDTH,HEIGHT);g.setColor(c);move(dir);}else{tm.shells.remove(this);}}//炮弹的移动,如果炮弹移动出了地图尺寸,则设定炮弹死亡public void move(Direction d){if(d==Direction.UP){y-=yspeed;if(y<=0)islive=false;}else if(d==Direction.RIGHT){x+=xspeed;if(x>=800)islive=false;}else if(d==Direction.DOWN){y+=yspeed;if(y>=600)islive=false;}else if(d==Direction.LEFT){x-=xspeed;if(x<=0)islive=false;}}public Rectangle getRec(){return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT);}//打击坦克方法public boolean hitTank(Tank t){if(this.isLive&&t.isLive&&this.getRec().intersects(t.getRec())){//用户坦克生命值减20t.setLife(t.getLife()-20);//如果用户坦克生命值《=0,游戏结束if(t.getLife()<=0){t.setLive(false);t.direction=Direction.STOP;JOptionPane.showMessageDialog(tm,"Game Over!");System.exit(0);}//炮弹死亡this.isLive=false;//产生爆炸Explor e=new Explor(x-3,y-3,this.tm);tm.explors.add(e);return true;}return false;}//用户坦克打击敌方坦克的方法public boolean hitTanks(List<EnemyTank>enemyTanks){ EnemyTank e;for(int i=0;i<enemyTanks.size();i++){e=enemyTanks.get(i);if(this.getRec().intersects(e.getRec())){System.out.println("hittanks");e.setLive(false);tm.enemys.remove(e);this.isLive=false;Explor ex=new Explor(x-3,y-3,this.tm);tm.explors.add(ex);return true;}}return true;}}五.爆炸类源代码:public class Explor {//初始化爆炸属性int x,y;TankMap tm;boolean islive=true;int diameter[]={5,9,13,18,25,31,21,12,6};int step=0;//构造方法public Explor(){}public Explor(int x,int y,TankMap t){this.x=x;this.y=y;this.tm=t;}//绘制爆炸public void draw(Graphics g){//如果爆炸已经发生过,从地图的爆炸集合中移出该爆炸if(!this.islive){tm.explors.remove(this);return;}//判断爆炸是否发生完if(step==diameter.length){this.islive=false;step=0;return;}Color c=g.getColor();g.setColor(Color.ORANGE);g.fillOval(x, y, diameter[step], diameter[step]);step++;g.setColor(c);}}六.枚举方向源代码package tank;//定义枚举public enum Direction{UP,RIGHT,DOWN,LEFT,STOP;}七.随机血包产生的方法public class Blood{public final int WIDTH=30,HEIGHT=30;public int x= new Random().nextInt(800),y= new Random().nextInt(600);TankMap tm;public void draw(Graphics g){ImageIcon icon=new ImageIcon("blood.gif");Image tankImage=icon.getImage();g.drawImage(tankImage,x,y,null);}public boolean bloodb(Tank t){if(t.isLive&&this.getRec().intersects(t.getRec())){if(t.getLife()<=60){System.out.println("blood up");t.setLife(t.getLife()+40);tm.bloods.remove(this);}else{System.out.println("blood up");t.setLife(100);tm.bloods.remove(this);}}return false;}public Rectangle getRec(){return new Rectangle(this.x,this.y,this.WIDTH,this.HEIGHT); }}四、总结。