做坦克大战某一步出现问题了
用Java做坦克大战的爆炸效果时,运行出来发现子弹击中第一个坦克时没有效果,而后面的都正常,调试时又发现击中第一个时也有效果,只是很快,这到底是怎么回事呢?哪位高手帮忙看下?谢谢啦!附代码:
package com.tank3;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class Tank3 extends JFrame{
MyPanel mp=null;
public static void main(String[] args) {
// TODO Auto-generated method stub
Tank3 tk=new Tank3();
}
public Tank3()
{
mp=new MyPanel();
Thread t=new Thread(mp);
t.start();
this.add(mp);
this.addKeyListener(mp);
this.setSize(500, 400);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
class MyPanel extends JPanel implements KeyListener,Runnable
{
//定义一个自己的坦克
MyTank mt=null;
//定义一组敌人的坦克
Vector<EnemyTank> ets=new Vector<EnemyTank>();
Vector<Bomb> bombs=new Vector<Bomb>();
int etSize=3;
Image image1=null;
Image image2=null;
Image image3=null;
public MyPanel(){
mt=new MyTank(200,300);
//定义敌人坦克
for(int i=0;i<etSize;i++){
EnemyTank et=new EnemyTank((i+1)*50,0);
et.setColor(0);
et.setDirect(2);
ets.add(et);
}
//初始化图片
image1=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bz1.jpg"));
image2=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bz2.jpg"));
image3=Toolkit.getDefaultToolkit().getImage(Panel.class.getResource("/bz3.jpg"));
}
// 画图函数
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 480, 385);
// 画出自己的坦克
this.DrawTank(mt.getX(), mt.getY(), g, this.mt.getDirect(), 1);
for (int i = 0; i < mt.ss.size(); i++) {
Shot myShot = mt.ss.get(i);
if (myShot != null && myShot.isLive == true) {
g.draw3DRect(myShot.x, myShot.y, 1, 1, false);
}
if (!myShot.isLive) {
mt.ss.remove(myShot);
}
}
for(int i=0;i<bombs.size();i++){
Bomb b=bombs.get(i);
if(b.life>6){
System.out.print("**");
g.drawImage(image1, b.x, b.y, 30, 30, this);
}else if(b.life>3){
System.out.print("**");
g.drawImage(image2, b.x, b.y, 30, 30, this);
}else{
System.out.print("**");
g.drawImage(image3, b.x, b.y, 30, 30, this);
}
b.lifeDown();
if(b.life==0){
bombs.remove(b);
}
}
// 画出敌人的坦克
for (int i = 0; i < ets.size(); i++) {
EnemyTank et = ets.get(i);
if (et.isLive) {
this.DrawTank(et.getX(), et.getY(), g, et.getDirect(), 0);
}
if(et.isLive==false){
ets.remove(et);
}
}
}
// 判断是否有子弹击中坦克
public void hitTank(Shot st, EnemyTank et) {
switch (et.direct) {
case 2:
case 8:
if (st.x > et.x && st.x < et.x + 20 && st.y > et.y
&& st.y < et.y + 30) {
Bomb b = new Bomb(et.x, et.y);
bombs.add(b);
st.isLive = false;
et.isLive = false;
}
case 4:
case 6:
if (st.x > et.x && st.x < et.x + 30 && st.y > et.y
&& st.y < et.y + 20) {
Bomb b = new Bomb(et.x, et.y);
bombs.add(b);
st.isLive = false;
et.isLive = false;
}
}
}
// 画坦克
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 8:
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y, x + 10, y + 15);
break;
case 2:
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 30, x + 10, y + 15);
break;
case 4:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x + 5, y + 5, 20, 10, false);
g.fill3DRect(x, y + 15, 30, 5, false);
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x + 15, y + 10, x, y + 10);
break;
case 6:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x + 5, y + 5, 20, 10, false);
g.fill3DRect(x, y + 15, 30, 5, false);
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x + 15, y + 10, x + 30, y + 10);
break;
}
}
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
if (e.getKeyCode() == KeyEvent.VK_W) {
this.mt.setDirect(8);
this.mt.MoveUp();
} else if (e.getKeyCode() == KeyEvent.VK_A) {
this.mt.setDirect(4);
this.mt.MoveLeft();
} else if (e.getKeyCode() == KeyEvent.VK_S) {
this.mt.setDirect(2);
this.mt.MoveDown();
} else if (e.getKeyCode() == KeyEvent.VK_D) {
this.mt.setDirect(6);
this.mt.MoveRight();
}
if (e.getKeyCode() == KeyEvent.VK_J) {
if (mt.ss.size() <= 5) {
this.mt.ShotEnemy();
}
}
this.repaint();
}
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
for (int i = 0; i < mt.ss.size(); i++) {
Shot myShot = mt.ss.get(i);
if (myShot.isLive) {
for (int j = 0; j < ets.size(); j++) {
EnemyTank et = ets.get(j);
hitTank(myShot, et);
}
}
}
this.repaint();
}
}
}
class Tank
{
public boolean isLive=true;
int x=0;
int y=0;
int direct=8;
int speed=5;
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;
}
public Tank(int x,int y){
this.x=x;
this.y=y;
}
}
class EnemyTank extends Tank
{
public EnemyTank(int x, int y) {
super(x, y);
}
}
class MyTank extends Tank
{
//Shot st=null;
Vector<Shot>ss=new Vector<Shot>();
Shot st=null;
public MyTank(int x,int y){
super(x,y);
}
public void ShotEnemy(){
switch(this.direct){
case 8:
st=new Shot(x+10,y,8);
ss.add(st);
break;
case 2:
st=new Shot(x+10,y+30,2);
ss.add(st);
break;
case 4:
st=new Shot(x,y+10,4);
ss.add(st);
break;
case 6:
st=new Shot(x+30,y+10,6);
ss.add(st);
break;
}
Thread td=new Thread(st);
td.start();
}
public void MoveUp(){
y-=speed;
}
public void MoveDown(){
y+=speed;
}
public void MoveRight(){
x+=speed;
}
public void MoveLeft(){
x-=speed;
}
}
class Shot implements Runnable
{
int x;
int y;
int direct;
int speed=1;
boolean isLive=true;
public Shot(int x,int y,int direct){
this.x=x;
this.y=y;
this.direct=direct;
}
public void run() {
while(true){
try{
Thread.sleep(50);
}catch(Exception e){
}
switch(direct){
case 8:
y-=speed;
break;
case 2:
y+=speed;
break;
case 4:
x-=speed;
break;
case 6:
x+=speed;
break;
}
//System.out.print("+");
if(x<0||x>480||y<0||y>385)
{
isLive=false;
break;
}
}
}
}
class Bomb
{
int x;
int y;
int life=9;
boolean isLive=true;
public Bomb(int x, int y) {
this.x = x;
this.y = y;
}
public void lifeDown(){
if (life>0){
life--;
}else{
isLive=false;
}
}
}