坦克大战

来源:互联网 发布:电话自动接听软件 编辑:程序博客网 时间:2024/06/02 13:32
package myTankTest_test;import java.awt.Color;import java.awt.Graphics;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Vector;import javax.swing.JFrame;import javax.swing.JPanel;public class MyTankGame_test extends JFrame {//MyPanel mp = null;    public static void main(String[] args) {    MyTankGame_test mtg = new MyTankGame_test();     }public  MyTankGame_test(){MyPanel mp = new MyPanel();Thread t = new Thread(mp);t.start();this.add(mp);this.addKeyListener(mp);this.setSize(400,300);this.setVisible(true);}}class MyPanel extends JPanel implements KeyListener,Runnable{Hero hero = null;EnemyTank et = null;Vector<EnemyTank> ets = new Vector<EnemyTank>();int enSize = 4;public MyPanel(){    //初始化我方坦克hero = new Hero(100,100);//加上Tank就不对了,这是为什么?    //初始化敌人坦克  for(int i=0;i<enSize;i++){  et = new EnemyTank((i+1)*50,20);  et.setDirect(2);  ets.add(et);  }}public void paint(Graphics g){super.paint(g);    g.fillRect(0,0,400,300);    //画出我的坦克    this.drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);       for(int i=0;i<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;i<ets.size();i++){    EnemyTank et = ets.get(i);    this.drawTank(et.getX(), et.getY(), g, et.getDirect(), 0);    }        }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://画出我的坦克(到时再封装成一个方法)//1.画出左边的矩形g.fill3DRect(x-10,y-15,5,30,false);//2.画出右边的矩形g.fill3DRect(x+5,y-15,5,30,false);//3.画出中间矩形g.fill3DRect(x-5,y-10,10,20,false);//4.画出圆形g.fillOval(x-5,y-5,10,10);//5.画出线g.drawLine(x,y,x,y-15);break;//向右case 1://1.画出上边的矩形g.fill3DRect(x-15,y-10,30,5,false);//2.画出下边的矩形g.fill3DRect(x-15,y+5,30,5,false);//3.画出中间矩形g.fill3DRect(x-10,y-5,20,10,false);//4.画出圆形g.fillOval(x-5,y-5,10,10);//5.画出线g.drawLine(x,y,x+15,y);break;//向下case 2://1.画出左边的矩形g.fill3DRect(x-10,y-15,5,30,false);//2.画出右边的矩形g.fill3DRect(x+5,y-15,5,30,false);//3.画出中间矩形g.fill3DRect(x-5,y-10,10,20,false);//4.画出圆形g.fillOval(x-5,y-5,10,10);//5.画出线g.drawLine(x,y,x,y+15);break;//向左case 3://1.画出上边的矩形g.fill3DRect(x-15,y-10,30,5,false);//2.画出下边的矩形g.fill3DRect(x-15,y+5,30,5,false);//3.画出中间矩形g.fill3DRect(x-10,y-5,20,10,false);//4.画出圆形g.fillOval(x-5,y-5,10,10);//5.画出线g.drawLine(x,y,x-15,y);break;}}@Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stubif(e.getKeyCode()== KeyEvent.VK_W){this.hero.setDirect(0);this.hero.moveUp();}else if(e.getKeyCode()== KeyEvent.VK_S){this.hero.setDirect(2);this.hero.moveDown();}else if(e.getKeyCode()== KeyEvent.VK_A){this.hero.setDirect(3);this.hero.moveLeft();}else if(e.getKeyCode()== KeyEvent.VK_D){this.hero.setDirect(1);this.hero.moveRight();}if(e.getKeyCode()==KeyEvent.VK_J){this.hero.shotEnemy();}this.repaint();}@Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}@Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stub}public void run() {// TODO Auto-generated method stub//每隔100毫秒重绘子弹while(true){try {Thread.sleep(100);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}   this.repaint();}}class Tank{int direct;int x;int y;int color;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;}public int getColor() {return color;}public void setColor(int color) {this.color = color;}public int getDirect() {return direct;}public void setDirect(int direct) {this.direct = direct;}}//本方坦克class Hero extends Tank{int speed = 2;Shot s = null;Vector<Shot> ss = new Vector<Shot>();public Hero(int x, int y) {super(x, y);}//坦克移动public void moveUp(){     y = y - speed;}public void moveRight(){x = x + speed;}public void moveDown(){y = y + speed;}public void moveLeft(){x = x - speed;}//开火public void shotEnemy(){switch(this.direct){case 0://创建子弹,并加入向量s=new Shot(x,y-15,0);ss.add(s);break;case 1:s=new Shot(x+15,y,1);ss.add(s);break;case 2:s=new Shot(x,y+15,2);ss.add(s);break;case 3:s=new Shot(x-15,y,3);ss.add(s);break;}//启动子弹线程Thread t=new Thread(s);t.start();}}//敌方坦克class EnemyTank extends Tank{    int speed = 2;  public EnemyTank(int x, int y) {super(x, y);}}class Shot implements Runnable{int x;int y;int direct;boolean isLive;int speed = 2;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 (Exception e) {// TODO: handle exception}switch(direct){case 0:y-=speed;break;case 1:x+=speed;break;case 2:y+=speed;break;case 3:x-=speed;break;}//子弹何时死亡?//判断子弹是否碰到边缘if(x<0||x>400||y<0||y>300){this.isLive=false;break;}}}  }}

0 0
原创粉丝点击