《java练习坦克大战.docx》由会员分享,可在线阅读,更多相关《java练习坦克大战.docx(12页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、坦克大战version3.5Java2014/7/12界面:简介:代码:/* * 功能:我的坦克可以控制移动、发射子弹 * 敌方坦克未处理 * java练习 * 韩顺平 */package TankGame3;/package MyTankGame4;import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.*;import javax.swing.*;import java.util.Vector;public class MyTankGame4 ex
2、tends JFrame MyPanel mp=null; public static void main(String args) MyTankGame4 mytankgame1=new MyTankGame4(); public MyTankGame4() mp=new MyPanel(); /启动mp线程 Thread t=new Thread(mp); t.start(); this.add(mp); this.addKeyListener(mp); this.setSize(400,300); this.setVisible(true); this.setDefaultCloseOp
3、eration(EXIT_ON_CLOSE); class MyPanel extends JPanel implements KeyListener,Runnable /定义我的坦克,成员变量 Hero hero=null; /定义敌人的坦克组 Vector ets=new Vector(); int enSize=3; public void paint (Graphics g) super.paint(g); g.fillRect(0,0,400,300); /画出自己的坦克 this.drawTank(hero.getX(), hero.getY(), g, this.hero.dir
4、ect, 1); /从Vector ss中取出每一颗子弹,并画出 for(int i=0;ithis.hero.ss.size();i+) Shot myShot=hero.ss.get(i); /画出子弹,只画一颗子弹 if (myShot!=null&myShot.isLive=true) g.draw3DRect(myShot.x, myShot.y, 1, 1, false); if (myShot.isLive=false) /从ss中删除该子弹 hero.ss.remove(myShot); /画出敌人的坦克 for(int i=0;iet.x&s.xet.y&s.yet.x&s.
5、xet.y&s.y(et.y+20) /击中了 /子弹死亡 s.isLive=false; /敌人坦克也要死亡 et.isLive=false; /画出坦克函数(扩展) public void drawTank(int x,int y,Graphics g,int direct,int type) /判断类型 switch (type) case 0: g.setColor(Color.cyan);break; case 1: g.setColor(Color.yellow);break; /判断方向 switch(direct) /向上 case 0: /画出我的坦克(到时候再封装成一个函数
6、) /1.画出左面的矩形 /g.drawRect(hero.getX(), hero.getY(), 5, 30); g.fill3DRect(x,y,5,30,false); /2.画出右边的矩形 g.fill3DRect(x+15,y,5,30,false); /3.画出坦克的中间矩形 g.fill3DRect(x+5, y+5, 10, 20,false); /画出中间的圆 g.fillOval(x+4, y+10,10,10); /画出线 g.drawLine(x+9, y+15, x+9, y); break; case 1: /炮筒向右 /画出上面的矩形 g.fill3DRect(
7、x,y,30, 5, false); g.fill3DRect(x, y+15, 30, 5, false); g.fill3DRect(x+5, y+5, 20, 10, false); g.fillOval(x+10, y+5, 10, 10); g.drawLine(x+15, y+10, x+30, y+10); break; case 2: /向下 g.fill3DRect(x,y,5,30,false); g.fill3DRect(x+15,y,5,30,false); g.fill3DRect(x+5, y+5, 10, 20,false); g.fillOval(x+4, y+
8、10,10,10); g.drawLine(x+10, y+15, x+10, y+30); break; case 3: /向左 g.fill3DRect(x,y,30, 5, false); g.fill3DRect(x, y+15, 30, 5, false); g.fill3DRect(x+5, y+5, 20, 10, false); g.fillOval(x+10, y+5, 10, 10); g.drawLine(x+15, y+10, x, y+10); break; public MyPanel() hero=new Hero(100,100); /初始化敌人的坦克 for(
9、int i=0;ienSize;i+) /创建敌人的坦克对象 EnemyTank et=new EnemyTank(i+1)*50,0); et.setColor(0); et.setDirect(2); ets.add(et); /键按下处理a表示左,s表示向下,w表示向上,d表示向右 public void keyPressed(KeyEvent e) if(e.getKeyCode()=KeyEvent.VK_W) / 设置我的坦克的方向 this.hero.setDirect(0); this.hero.moveUp(); else if (e.getKeyCode()=KeyEven
10、t.VK_S) this.hero.setDirect(2); this.hero.moveDown(); else if (e.getKeyCode()=KeyEvent.VK_D) this.hero.setDirect(1); this.hero.moveRight(); else if (e.getKeyCode()=KeyEvent.VK_A) this.hero.setDirect(3); this.hero.moveLeft(); if (e.getKeyCode()=KeyEvent.VK_J) this.hero.shotEnemy(); /必须重新绘制Panel this.
11、repaint(); public void keyReleased(KeyEvent e) public void keyTyped(KeyEvent e) public void run() /每个一百毫秒去重画子弹 while(true) try Thread.sleep(100); catch (InterruptedException e) / TODO Auto-generated catch block e.printStackTrace(); for(int i=0;ihero.ss.size();i+) Shot myShot=hero.ss.get(i); /判断子弹是否有
12、效 if(myShot.isLive) /取出每个坦克,与它判断 for(int j=0;jets.size();j+) /取出坦克 EnemyTank et=ets.get(j); if(et.isLive) this.hitTank(myShot,et); this.repaint(); /package MyTankGame4;/import java.util.Vector;/import MyTankGame4.Shot;/import MyTankGame4.Tank;class Tank /设置坦克的速度 int speed=1; public int getSpeed() re
13、turn speed; public void setSpeed(int speed) this.speed = speed; /表示坦克的横坐标 int x=0; /坦克的纵坐标 int y=0; int direct=0; int color; /坦克方向,0表示上,1表示右,2表示下,3表示左 public int getColor() return color; public void setColor(int color) this.color = color; public int getDirect() return direct; public void setDirect(i
14、nt direct) this.direct = direct; public Tank(int x,int y) this.x=x; this.y=y; 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; class EnemyTank extends Tank boolean isLive=true; public EnemyTank(int x,int y) super(x,y); /我
15、的坦克class Hero extends Tank /子弹 /Shot s=null; Vector ss=new Vector(); Shot s=null; public Hero(int x, int y) super(x,y); /坦克向上移动 /坦克的开火的能力和动作 public void shotEnemy() switch(this.direct) case 0: s=new Shot(x+9,y-1,0); ss.add(s); break; case 1: s=new Shot(x+30,y+10,1); ss.add(s); break; case 2: s=new S
16、hot(x+9,y+30,2); ss.add(s); break; case 3: s=new Shot(x-1,y+9,3); ss.add(s); ss.add(s); break; Thread t=new Thread(s); t.start(); public void moveUp() this.y-=speed; public void moveRight() this.x+=speed; public void moveDown() this.y+=speed; public void moveLeft() this.x-=speed; class Shot implemen
17、ts Runnable int x; int y; int direct; int speed=1; /是否活着 boolean isLive=true; public Shot(int x,int y,int direct) this.x=x; this.y=y; this.direct=direct; public void run() while(true) try Thread.sleep(50); catch (InterruptedException e) e.printStackTrace(); switch(direct) case 0: /向上 y-=speed;break; case 1: x+=speed;break; case 2: y+=speed;break; case 3: x-=speed;break; /子弹何时死亡? /判断该子弹是否碰到边缘 if(x400|y300) this.isLive=false; break;
限制150内