1 packagetankGame;2 import java.awt.*;3 import javax.swing.*;4 import java.awt.event.*;5 import java.util.*;6 /**
7 * 功能:我方坦克可以发子弹8 *9 **/
10 public class MyTankGame1 extendsJFrame {11
12 public static voidmain(String[] args) {13 //TODO Auto-generated method stub
14 MyTankGame1 mtg =newMyTankGame1();15 }16 //构造函数
17 publicMyTankGame1(){18 MyPanel mp=newMyPanel();19
20 //线程启动
21 Thread t=newThread(mp);22 t.start();23
24 this.add(mp);25 this.addKeyListener(mp);//注册监听
26 this.setSize(400,300);27 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);28 this.setVisible(true);29 }30
31 }32
33 //我的面板
34 class MyPanel extends JPanel implementsKeyListener,Runnable{35 Hero hero=null;//定义我的坦克
36 Vector enermy=new Vector() ;//定义敌人的坦克组
37 int enSize=3;38 //构造函数
39 publicMyPanel(){40 hero=new Hero(100,100);41
42 for(int i=0;i
44 Enermy en=new Enermy((i+1)*50, 0);45 en.setColor(0);46 en.setDerect(3);47 //加入
48 enermy.add(en);49 }50
51 }52 public void paint(Graphics g){//重新paint
53 super.paint(g);54 g.fillRect(0, 0, 400, 300);55 //画出我的坦克
56 this.drawTank(hero.getX(), hero.getY(), g,this.hero.derect, 1);57 //画出子弹
58 if(hero.s!=null&&hero.s.isLive==true){59 g.fillOval(hero.s.x ,hero.s.y, 2, 2);60 }61
62 //画出敌人的坦克
63 for(int i=0;i
66 }67 }68 public void drawTank(int x,int y,Graphics g,int direct,inttype){69 //判断是什么类型的坦克,敌人的还是我方的
70 switch(type){71 case 0:72 g.setColor(Color.CYAN);73 break;74 case 1:75 g.setColor(Color.yellow);76 break;77 }78 //判断方向
79 switch(direct){80 //坦克头向上
81 case 0:82 //画出我的坦克,到时再封装为一个函数83 //画出左边的矩形
84 g.fill3DRect(x, y, 5, 30,false);85 //画出有右边的矩形
86 g.fill3DRect(x+15, y, 5, 30,false);87 //画出中间的矩形
88 g.fill3DRect(x+5, y+5, 10, 20,false);89 //画出圆形
90 g.fillOval(x+5, y+10, 10, 10);91 //画炮头
92 g.drawLine(x+10, y-5, x+10, y+15);93 break;94
95 //坦克头向右
96 case 1:97
98 //画出上边的矩形
99 g.fill3DRect(x, y, 30, 5,false);100 //画出下边的矩形
101 g.fill3DRect(x, y+15, 30, 5,false);102 //画出中间的矩形
103 g.fill3DRect(x+5, y+5, 20, 10,false);104 //画出圆形
105 g.fillOval(x+10, y+5, 10, 10);106 //画炮头
107 g.drawLine(x+15, y+10, x+35, y+10);108 break;109
110 //坦克头向下
111 case 2:112
113 g.fill3DRect(x, y, 5, 30,false);114 //画出有右边的矩形
115 g.fill3DRect(x+15, y, 5, 30,false);116 //画出中间的矩形
117 g.fill3DRect(x+5, y+5, 10, 20,false);118 //画出圆形
119 g.fillOval(x+5, y+10, 10, 10);120 //画炮头
121 g.drawLine(x+10, y+15,x+10, y+35);122 break;123
124 //坦克头向左
125 case 3:126
127 //画出上边的矩形
128 g.fill3DRect(x, y, 30, 5,false);129 //画出下边的矩形
130 g.fill3DRect(x, y+15, 30, 5,false);131 //画出中间的矩形
132 g.fill3DRect(x+5, y+5, 20, 10,false);133 //画出圆形
134 g.fillOval(x+10, y+5, 10, 10);135 //画炮头
136 g.drawLine( x-5, y+10,x+15, y+10);137 break;138 }139
140
141 }142 @Override143 public voidkeyPressed(KeyEvent e) {144 //TODO Auto-generated method stub
145 if(e.getKeyCode()==KeyEvent.VK_W){146 //设置我的坦克方向
147 this.hero.setDerect(0);148 this.hero.moveUp();149 }else if (e.getKeyCode()==KeyEvent.VK_D){150 this.hero.setDerect(1);151 this.hero.moveRight();152 }else if (e.getKeyCode()==KeyEvent.VK_S){153 this.hero.setDerect(2);154 this.hero.moveDown();155 }else if (e.getKeyCode()==KeyEvent.VK_A){156 this.hero.setDerect(3);157 this.hero.moveLeft();158 }159 this.repaint();160 //判断玩家是否按下j
161 if(e.getKeyCode()==KeyEvent.VK_J){162 //开火,调用shotEnermy
163 this.hero.shotEnermy();164 }165
166 }167 @Override168 public voidkeyReleased(KeyEvent e) {169 //TODO Auto-generated method stub
170
171 }172 @Override173 public voidkeyTyped(KeyEvent e) {174 //TODO Auto-generated method stub
175
176 }177 @Override178 public void run() {//实现每隔100ms去重绘179 //TODO Auto-generated method stub
180 while(true){181 try{182 Thread.sleep(100);183 } catch(InterruptedException e) {184 //TODO Auto-generated catch block
185 e.printStackTrace();186 }187 this.repaint();188 }189 }190 }