/** * 坦克大战3.0 * 坦克能自由移动,发射子弹 */ package cn.jmy.test4; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.util.*; public class MyTankGame3 extends JFrame{ //定义组件 MyPanel mp=null; private static final long serialVersionUID = 1L; public static void main(String[] args) { MyTankGame3 mtg=new MyTankGame3(); } public MyTankGame3(){ //创建组件 mp=new MyPanel(); Thread t=new Thread(mp); t.start(); this.add(mp); //注册监听 this.addKeyListener(mp); this.setTitle("my tankgame2.0"); this.setIconImage(new ImageIcon("1031.png").getImage()); this.setSize(400,300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); } } class MyPanel extends JPanel implements KeyListener,Runnable{ Hero hero=null; Vector<EnemyTank> ets=new Vector<EnemyTank>(); int enSize=3; private static final long serialVersionUID = 1L; public MyPanel(){ hero=new Hero(90,90); //定义敌军坦克组 for(int i=0;i<enSize;i++){ EnemyTank et=new EnemyTank((i+1)*50,20); et.setColor(0); 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, this.hero.direct, 1); //画出子弹 if(hero.s!=null){ g.draw3DRect(hero.s.x, hero.s.y, 1, 1, false); } //画出敌军坦克 for(int i=0;i<ets.size();i++){ this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).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: g.fill3DRect(x-10, y-15, 5,30, false); g.fill3DRect(x+5, y-15, 5, 30, false); g.fill3DRect(x-5, y-10, 10, 20, false); g.fillOval(x-5, y-5, 10, 10); g.drawLine(x, y, x, y-15); break; case 1: g.fill3DRect(x-15, y-10, 30,5, false); g.fill3DRect(x-15, y+5, 30, 5, false); g.fill3DRect(x-10, y-5, 20, 10, false); g.fillOval(x-5, y-5, 10, 10); g.drawLine(x, y, x+15, y); break; case 2: g.fill3DRect(x-10, y-15, 5,30, false); g.fill3DRect(x+5, y-15, 5, 30, false); g.fill3DRect(x-5, y-10, 10, 20, false); g.fillOval(x-5, y-5, 10, 10); g.drawLine(x, y, x, y+15); break; case 3: g.fill3DRect(x-15, y-10, 30,5, false); g.fill3DRect(x-15, y+5, 30, 5, false); g.fill3DRect(x-10, y-5, 20, 10, false); g.fillOval(x-5, y-5, 10, 10); g.drawLine(x, y, x-15, y); break; } } @Override public void keyTyped(KeyEvent e) { } @Override public void keyPressed(KeyEvent e) { if(e.getKeyCode()==KeyEvent.VK_W){ this.hero.setDirect(0); this.hero.moveUp(); }else if(e.getKeyCode()==KeyEvent.VK_D) { this.hero.setDirect(1); this.hero.moveRight(); }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(); }if(e.getKeyCode()==KeyEvent.VK_J){ this.hero.shotEnemy(); } this.repaint(); } @Override public void keyReleased(KeyEvent e) { } @Override public void run() { while(true){ try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } this.repaint(); } } } package cn.jmy.test4; //坦克类 class Tank{ int direct=0; int speed=5; int color; public int getSpeed() { return speed; } public void setSpeed(int speed) { this.speed = speed; } public int getColor() { return color; } public void setColor(int color) { this.color = color; } public void moveUp(){ y-=speed; } public void moveRight(){ x+=speed; } public void moveDown(){ y+=speed; }public void moveLeft(){ x-=speed; } public int getDirect() { return direct; } public void setDirect(int direct) { this.direct = direct; } int x=0; public Tank(int x, int y) { super(); this.x = x; this.y = y; } int y=0; 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 Shot implements Runnable{ int x; int y; int direct; int speed=4; public Shot(int x,int y,int direct){ this.x=x; this.y=y; this.direct=direct; } @Override 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; } System.out.println("当前坦克的坐标x="+x+"y="+y); if(x>400||x<0||y>300||y<0){ break; } } } } //我的坦克 class Hero extends Tank{ Shot s=null; public Hero(int x, int y) { super(x, y); } public void shotEnemy(){ switch(this.direct){ case 0: s=new Shot(x,y-15,0); break; case 1: s=new Shot(x+10,y,1); break; case 2: s=new Shot(x,y+15,2); break; case 3: s=new Shot(x-10,y,3); break; } Thread t=new Thread(s); t.start(); } } //敌方坦克 class EnemyTank extends Tank{ public EnemyTank(int x, int y) { super(x, y); } }