java吧 关注:1,276,643贴子:12,790,243
  • 3回复贴,共1

求大神帮忙,为何我写坦克大战,子弹发射后,没有流动呢?

只看楼主收藏回复

先谢谢了!代码放二楼。帮忙看看bug.
我已经在线程中加了repaint();


来自Android客户端1楼2016-05-26 01:19回复
    package gui;
    import java.awt.*;
    import java.util.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class MyTankGames extends JFrame{
    MyPanel3 mp=null;
    public static void main(String[] args) {
    MyTankGames mtg=new MyTankGames();
    }
    public MyTankGames(){
    mp=new MyPanel3();
    this.add(mp);
    Thread t1=new Thread(mp);
    t1.start();
    this.addKeyListener(mp);
    this.setTitle("坦克大战");
    this.setSize(900, 900);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    }
    }
    class MyPanel3 extends JPanel implements KeyListener,Runnable{
    Hero hero=null;
    Vector<Enermy> ets=new Vector<Enermy>();
    int size=4;
    public MyPanel3()
    {
    hero=new Hero(50,50);
    for(int i=0;i<size;i++)
    {
    Enermy et=new Enermy((i+1)*50,0);
    et.setColor(1);
    et.setDirection(2);
    ets.add(et);
    }
    }
    public void paint(Graphics g){
    super.paint(g);
    this.drawTank(hero.getX(), hero.getY(), g, hero.getDirection(), 0);
    for(int i=0;i<ets.size();i++)
    {
    this.drawTank(ets.get(i).getX(), ets.get(i).getY(), g, ets.get(i).getDirection(), 1);
    }
    if(hero.s!=null&&hero.s.isLive==true)
    {
    g.setColor(Color.BLACK);
    g.draw3DRect(hero.s.x, hero.s.y, 1, 1, false);
    }
    }
    public void drawTank(int x,int y,Graphics g,int direction,int type)
    {
    switch(type)
    {
    case 0:
    g.setColor(Color.CYAN);
    break;
    case 1:
    g.setColor(Color.YELLOW);
    break;
    }
    switch(direction)
    {
    case 0:
    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+10, 10,10);
    g.drawLine(x+9, y+15,x+9,y);
    break;
    case 1:
    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+30,y+10);


    来自Android客户端2楼2016-05-26 01:19
    回复
      2025-09-03 15:05:42
      广告
      不感兴趣
      开通SVIP免广告
      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+10, 10,10);
      g.drawLine(x+9, y+15,x+9,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;
      }
      }
      @Override
      public void keyTyped(KeyEvent e) {
      // TODO Auto-generated method stub
      }
      @Override
      public void keyPressed(KeyEvent e) {
      // TODO Auto-generated method stub
      if(e.getKeyCode()==KeyEvent.VK_W){
      this.hero.moveup();
      //x的值按照速度值递减
      this.hero.setDirection(0);//设置坦克方向
      }else if(e.getKeyCode()==KeyEvent.VK_D){
      this.hero.moveright();
      this.hero.setDirection(1);
      }else if(e.getKeyCode()==KeyEvent.VK_S){
      //this.hero.setSpeed(2);
      this.hero.movedown();
      this.hero.setDirection(2);
      }else if(e.getKeyCode()==KeyEvent.VK_A){
      //this.hero.setSpeed(3);
      this.hero.moveleft();
      this.hero.setDirection(3);
      }
      if(e.getKeyCode()==KeyEvent.VK_J){
      //this.hero.setSpeed(3);
      this.hero.shotEnemy();
      }
      this.repaint();
      }
      @Override
      public void keyReleased(KeyEvent e) {
      // TODO Auto-generated method stub
      }
      @Override
      public void run() {
      // TODO Auto-generated method stub
      while(true)
      {
      try{
      Thread.sleep(100);
      }catch(Exception e)
      {
      e.printStackTrace();
      }
      this.repaint();
      }
      }
      }
      class Shot implements Runnable{
      int x;
      int direction;
      int speed=1;
      boolean isLive=true;
      public Shot(int x,int y,int direction)
      {
      this.x=x;
      this.y=y;
      this.direction=direction;
      }
      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;
      }
      int y;
      @Override
      public void run() {
      // TODO Auto-generated method stub
      try{
      Thread.


      来自Android客户端3楼2016-05-26 01:21
      回复
        .sleep(50);
        }catch(Exception e)
        {
        e.printStackTrace();
        }
        while(true)
        {
        switch(this.direction)
        {
        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<0||x>900||y<0||y>900)
        {
        this.isLive=false;
        break;
        }
        }
        }
        }
        class Tank{
        int x;
        int y;
        int speed=3;
        int direction=0;
        int color;
        public int getColor() {
        return color;
        }
        public void setColor(int color) {
        this.color = color;
        }
        public int getDirection() {
        return direction;
        }
        public void setDirection(int direction) {
        this.direction = direction;
        }
        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 getSpeed() {
        return speed;
        }
        public void setSpeed(int speed) {
        this.speed = speed;
        }
        }
        class Hero extends Tank
        {
        Shot s=null;
        public Hero(int x,int y){
        super(x,y);
        }
        public void shotEnemy()
        {
        switch(this.direction)
        {
        case 0:
        s=new Shot(x+10,y,0);
        break;
        case 1:
        s=new Shot(x+30,y+10,1);
        break;
        case 2:
        s=new Shot(x+10,y+30,2);
        break;
        case 3:
        s=new Shot(x,y+10,3);
        break;
        }
        Thread t=new Thread(s);
        t.start();
        }
        public void moveup(){
        y-=speed;
        }
        public void movedown(){
        y+=speed;
        }
        public void moveleft(){
        x-=speed;
        }
        public void moveright(){
        x+=speed;
        }
        }
        class Enermy extends Tank
        {
        public Enermy(int x,int y){
        super(x,y);
        }


        来自Android客户端5楼2016-05-26 01:27
        回复