第一个代码:package game.tank;import java.util.Vector;class Tank
{
// 1、确定tank的坐标,也就是起始位置;
int x = 0;
int y = 0;
// 方向
int direct ;
// 速度 
int speed = 2;
// 颜色
int color; public int getColor()
{
return color;
} public void setColor(int color)
{
this.color = color;
} public int getSpeed()
{
return speed;
} public void setSpeed(int speed)
{
this.speed = speed;
} public int getDirect()
{
return direct;
} public void setDirect(int direct)
{
this.direct = direct;
} 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;
} // 2、要想使坦克可以动起来,需要一个构造函
public Tank(int x, int y)
{
this.x = x;
this.y = y;
}
}// 定义子弹
class Bullet implements Runnable
{
int x;
int y;
int direct;
int speed = 2;
boolean live = true;
public Bullet(int x,int y,int direct)
{
this.x = x;
this.y = y;
this.direct = direct;
}
@Override
public void run()
{

while(true)
{
try
{
Thread.sleep(30);
}
catch (Exception e)
{
}
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>500||y<0||y>400)
{
this.live = false;
}
}
}
}// 定义我自己的坦克
class Mytank extends Tank
{
Bullet b = null;
Vector<Bullet> bb = new Vector<Bullet>();
public Mytank(int x, int y)
{
super(x, y);
}
// 开火
public void fire()
{
switch(this.direct)
{
case 0:
b = new Bullet(x+10, y, 0);
bb.add(b);
break;
case 1:
b = new Bullet(x+30, y+10, 1);
bb.add(b);
break;
case 2:
b = new Bullet(x+10, y+30, 2);
bb.add(b);
break;
case 3:
b = new Bullet(x, y+10, 3);
bb.add(b);
break;
}
Thread t = new Thread(b);
t.start();
}

public void moveup()
{
y -= speed;
} public void movedown()
{
y += speed;
} public void moveright()
{
x += speed;
} public void moveleft()
{
x -= speed;
}
}class Enemytank extends Tank
{
boolean live = true; public Enemytank(int x, int y)
{
super(x, y);
}

}
//定义一个炸弹类
class Bomb
{
int x;
int y;
// 炸弹的生命周期
int life = 9;
boolean live = true;
public Bomb(int x,int y)
{
this.x = x;
this.y = y;
}
// 生命值减少
public void lifedown()
{
if(life>0)
{
life--;
}
else
{
live = false;
}
}
}
第二个代码:package game.tank;import javax.swing.*;
import javax.swing.border.EmptyBorder;import java.awt.*;
import java.awt.event.*;
import java.util.Vector;public class DrawTank extends JFrame
{
Mypanel mypanel; public DrawTank()
{
mypanel = new Mypanel();
mypanel.setBackground(Color.black); Thread t = new Thread(mypanel);
t.start(); this.add(mypanel);
this.addKeyListener(mypanel);
this.setSize(500, 400);
this.setLocation(500, 350);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args)
{
new DrawTank();
}
}// 定义我的面板
class Mypanel extends JPanel implements KeyListener, Runnable
{
Mytank mytank;
// 定义敌人的tank数组
Vector<Enemytank> enemytank = new Vector<Enemytank>();
// 定义敌人tank出现的数量
int etSize = 3;
// 定义炸弹的集合
Vector<Bomb> bombs = new Vector<Bomb>();
// 定义三张图片
Image image1 = null;
Image image2 = null;
Image image3 = null; public Mypanel()
{
mytank = new Mytank(200, 340);
mytank.setColor(0);
for (int i = 0; i < etSize; i++)
{
Enemytank et = new Enemytank((i + 1) * 110, 0);
et.setColor(1);
et.setDirect(2);
enemytank.add(et);
}
image1 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/images/bomb_1.gif"));
image2 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/images/bomb_2.gif"));
image3 = Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/images/bomb_3.gif"));
} public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.cyan);
this.draw(mytank.getX(), mytank.getY(), g, this.mytank.direct, 0);
// 画出子弹
for (int i = 0; i < mytank.bb.size(); i++)
{
Bullet myb = mytank.bb.get(i);
if (myb != null && myb.live == true)
{
g.draw3DRect(myb.x, myb.y, 1, 1, false);
}
if (myb.live == false)
{
mytank.bb.remove(myb);
}
}
// 画出炸弹
for(int i = 0;i<bombs.size();i++)
{
Bomb bom = bombs.get(i);
if(bom.life==6)
{
g.drawImage(image1, bom.x, bom.y, 30, 30, this);
}else if(bom.life == 3)
{
g.drawImage(image3, bom.x, bom.y, 30, 30, this);
}else
{
g.drawImage(image3, bom.x, bom.y, 30, 30, this);
}
bom.lifedown();
if(bom.life==0)
{
bombs.remove(bom);

}
}
// 画出敌人的坦克
for (int i = 0; i < enemytank.size(); i++)
{
Enemytank et = enemytank.get(i);
if (et.live)
this.draw(et.getX(), et.getY(), g, et.getDirect(), 1);
} } public void hittank(Bullet b, Enemytank et)
{
switch (et.direct)
{
case 0:
case 2:
{
if (b.x > et.x && b.x < et.x + 20 && b.y > et.y && b.y < et.y + 30)
{
// 坦克死亡
et.live = false;
// 子弹死亡
b.live = false;
// 创建一颗炸弹
Bomb bom = new Bomb(et.x, et.y);
bombs.add(bom);
}
}
case 1:
case 3:
{
if (b.x > et.x && b.x < et.x + 30 && b.y > et.y && b.y < et.y + 20)
{
// 坦克死亡
et.live = false;
// 子弹死亡
b.live = false;
// 创建一颗炸弹
Bomb bom = new Bomb(et.x, et.y);
bombs.add(bom);
}
}
}
} public void draw(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, y, 5, 30, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 5, y + 10, 10, 10);
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);
g.fill3DRect(x + 5, y + 5, 20, 10, false);
g.fillOval(x + 10, y + 4, 10, 10);
g.drawLine(x + 15, y + 10, x + 30, y + 10);
break;
// 向下
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 + 4, 10, 10);
g.drawLine(x + 15, y + 10, x, y + 10);
break; }
} @Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_UP)
{
this.mytank.setDirect(0);
this.mytank.moveup();
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT)
{
this.mytank.setDirect(1);
this.mytank.moveright();
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN)
{
this.mytank.setDirect(2);
this.mytank.movedown();
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT)
{
this.mytank.setDirect(3);
this.mytank.moveleft();
}
if (e.getKeyCode() == KeyEvent.VK_J)
{
if (mytank.bb.size() < 5)
{
this.mytank.fire();
}
}
this.repaint();
} @Override
public void keyReleased(KeyEvent e)
{
// TODO Auto-generated method stub } @Override
public void keyTyped(KeyEvent e)
{
// TODO Auto-generated method stub } @Override
public void run()
{
while (true)
{
try
{
Thread.sleep(100);
}
catch (Exception e)
{
e.printStackTrace();
}
// 判断是否击中
// 先取出子弹
for (int i = 0; i < mytank.bb.size(); i++)
{
Bullet mybullet = mytank.bb.get(i);
if (mybullet.live)
{
for (int j = 0; j < enemytank.size(); j++)
{
Enemytank et = enemytank.get(j);
if (et.live)
{
this.hittank(mybullet, et);
}
}
}
}
this.repaint();
} }
}问题如下:
当我打第一个坦克时,没有爆炸效果,第二个第三个才有那里出了问题,请详细指教!!!

解决方案 »

  1.   

    没看出打第一个坦克和第2,3个有什么区别。
    如果没看到效果,推测有可能跟爆炸图片由于需要加载图片的时间赶不上线程运行的速度,导致图片来不及显示有关,第二次爆炸由于图片已经加载所以没有问题。
    你可以在游戏开始前在某个不可见的地方draw一下这几个Image,就可以验证是否这个问题。另外
                if(bom.life==6)
                {
                    g.drawImage(image1, bom.x, bom.y, 30, 30, this);
                }else if(bom.life == 3)
                {
                    g.drawImage(image3, bom.x, bom.y, 30, 30, this);
                }else
                {
                    g.drawImage(image3, bom.x, bom.y, 30, 30, this);
                }
    肯定有问题,image2没用上hittank()中的switch也有问题,由于没使用break语句,导致一次击中可能产生两个炸弹
      

  2.   

    楼主啊 ,你是不是看了   ——>   韩顺平java视频教程啊。 
    我是看他的视频教程学java 的。 
    本人自学(初学者) 。 
      

  3.   

    以下代码是我看了视频教程后 ,用自己的思路编写的,保留了相当不少的远思路。
    package com.Text5;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.Vector;
    public class Demo_1 extends JFrame{ Mypanel mp=null;

    /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Demo_1 demo_1=new Demo_1(); }
    public Demo_1()
    {
    mp=new Mypanel();
    this.add(mp);
    this.addKeyListener(mp);
    this.setSize(450,350);
    this.setTitle("坦克大战");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
    this.setLocation(100,100);
    }}
    //我的panel
    class Mypanel extends JPanel implements KeyListener
    {
    //定义我的坦克
    Hero hero=null;
    //定义敌人坦克
    Vector<Enemytank> ss=new Vector<Enemytank>();
    Enemytank et=null;
    //敌人坦克的数量
    int etsize=3;
    //构造函数
    public Mypanel()
    {
    //创建我坦克
    hero=new Hero(200,250);
    hero.setF(1);
    //创建敌人坦克
    for(int i=0;i<etsize;i++)
    {
    et=new Enemytank(i*150,100);
    et.setF(2);
    et.setColor(1);
    Thread t=new Thread(et);
    t.start();
    ss.add(et);
    }
    }
    //重写paint
    public void paint(Graphics g)
    {
    super.paint(g);
    g.fillRect(0, 0, 400, 300);
    //画我的坦克
    this.drawtank(hero.getX(), hero.getY(), hero.getF(), hero.getColor(), g);
    //画敌人坦克
    for(int i=0;i<ss.size();i++)
    {
    Enemytank et=ss.get(i);
    this.drawtank(et.getX(), et.getY(), et.getF(), et.getColor(), g);
    }
    }
    //画坦克的函数
    public void drawtank(int x,int y,int f,int color,Graphics g)
    {
    //判断颜色
    switch(color)
    {
    case 0:
    g.setColor(Color.yellow);
    break;
    case 1:
    g.setColor(Color.cyan);
    break;
    }
    //判断方向
    switch(f)
    {
    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+4, 10, 10);
    g.drawLine(x+11, y+9, x+30, y+9);
    break;
    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+4, 10, 10);
    g.drawLine(x+11, y+9, x, y+9);
    break;

    }
    this.repaint();
    }
    @Override
    public void keyTyped(KeyEvent e) {
    // TODO Auto-generated method stub

    }
    @Override
    public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    switch(e.getKeyCode())
    {
    case KeyEvent.VK_UP:
    hero.setF(0);
    if(hero.getY()>0)
    hero.up();
    break;
    case KeyEvent.VK_RIGHT:
    hero.setF(1);
    if(hero.getX()<400-30)
    hero.right();
    break;
    case KeyEvent.VK_DOWN:
    hero.setF(2);
    if(hero.getY()<300-30)
    hero.down();
    break;
    case KeyEvent.VK_LEFT:
    hero.setF(3);
    if(hero.getX()>0)
    hero.left();
    break;
    }

    }
    @Override
    public void keyReleased(KeyEvent e) {
    // TODO Auto-generated method stub

    }

    }
    //坦克类
    class Tank
    {
    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 getF() {
    return f;
    }
    public void setF(int f) {
    this.f = f;
    }
    public int getColor() {
    return color;
    }
    public void setColor(int color) {
    this.color = color;
    }
    public int getSd() {
    return sd;
    }
    public void setSd(int sd) {
    this.sd = sd;
    }
    public boolean isLive() {
    return isLive;
    }
    public void setLive(boolean isLive) {
    this.isLive = isLive;
    }
    int x=0;
    int y=0;
    int f=0;
    int color=0;
    int sd=0;
    public Tank(int x,int y)
    {
    this.x=x;
    this.y=y;
    this.sd=3;
    }
    boolean isLive=true;
    public void up()
    {
    y-=sd;
    }
    public void right()
    {
    x+=sd;
    }
    public void down()
    {
    y+=sd;
    }
    public void left()
    {
    x-=sd;
    }
    }//我的坦克
    class Hero extends Tank
    { public Hero(int x, int y) {
    super(x, y);
    // TODO Auto-generated constructor stub
    }

    }
    //敌人坦克
    class Enemytank extends Tank implements Runnable
    { public Enemytank(int x, int y) {
    super(x, y);
    // TODO Auto-generated constructor stub
    } @Override
    public void run() {
    // TODO Auto-generated method stub
    while(true)
    {
    int n=(int)(Math.random()*4);
    this.setF(n);
    switch(this.getF())
    {
    case 0:
    for(int i=0;i<(n+1)*10;i++)
    {
    if(this.getY()>0)
    this.up();
    else
    break;
    try {
    Thread.sleep(50);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    break;
    case 1:
    for(int i=0;i<(n+1)*10;i++)
    {
    if(this.getX()<400-30)
    this.right();
    else
    break;
    try {
    Thread.sleep(50);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    break;
    case 2:
    for(int i=0;i<(n+1)*10;i++)
    {
    if(this.getY()<300-30)
    this.down();
    else
    break;
    try {
    Thread.sleep(50);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    break;
    case 3:
    for(int i=0;i<(n+1)*10;i++)
    {
    if(this.getX()>0)
    this.left();
    else
    break;
    try {
    Thread.sleep(50);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }
    break;
    } }
    }

    }
    class Shot
    {
    int x,y;
    public Shot(int x,int y)
    {
    this.x=x;
    this.y=y;
    }
    }