日期:2014-05-20  浏览次数:20669 次

大家帮忙看一下这个程序这个为甚么发射不出子弹?多谢谢了
正宗韩顺平代码,但是就是发射不了子弹
请高手多多指教
小弟十分感谢



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class MyTankGame3 extends JFrame {

  Mypanel mp = null;

  public static void main(String[] args) {
  MyTankGame3 mtg = new MyTankGame3();

  }

  // 构造函数
  public MyTankGame3() {
  mp = new Mypanel();
  //启动mp线程
  Thread t=new Thread(mp);
  t.start();
  this.add(mp);
  // 注册监听
  this.addKeyListener(mp);
  this.setSize(400, 300);
  //this.setLocation(50,50);

  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;

  // 构造函数
  public Mypanel() {

  hero = new Hero(150, 150);

// 初始化敌人的坦克
  for (int i = 1; i <= enSize; i++)
  {
// 创建一辆敌人的坦克
  EnemyTank et = new EnemyTank((i + 1) * 50, 15);
  et.setColor(0);
  et.setDirect(2);
// 加入到敌人坦克组中
  ets.add(et);
  }
  }
  //重新paint
  public void paint(Graphics g) {

  super.paint(g);
  g.fillRect(0, 0, 400, 300);
  // 画出自己的坦克
  this.drawTank(hero.getX(), hero.getY(), g, hero.direct, 1);
  //画出子弹
  if(hero.s!=null&&hero.s.isLive==true)
  {
  g.draw3DRect(hero.s.x, hero.s.y, 2,2,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:
  // 画出我的坦克 (到时再封装成一个函数)
  // 1.画出左边的矩形
  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);
  // 4.画出圆形
  g.fillOval(x+5, y+10, 10, 10);
  // 5.画出线
  g.drawLine(x+10, y+15, x+10, y);
  break;
  // 向右
  case 1:
  //炮筒向右  
  //画出上边矩形
  g.fill3DRect(x, y , 30, 5, false);
  //画出下边的矩形
  g.fill3DRect(x, y + 15, 30, 5, false);
  //画出中间矩形