最近跟着马士兵的java教学视频,做了下坦克大战的小项目
在做到添加敌方坦克的时候,发现当TankClient里只创建一个tank对象时,可以正常使用。
多添加一个tank对象时便会只显示最先生成的tank对象,而且无法进行键盘控制显示的坦克。
一直调不出bug在哪。
希望可以得到些帮助。相关代码如下:
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.ArrayList;// 产生一个窗口
public class TankClient extends Frame { // 继承自Frame更具灵活性

// 代码重构
public static final int GAME_WIDTH = 800, GAME_HEIGHT = 600;

private Tank myTank = new Tank(50, 50, true, this);
private Tank enemyTank = new Tank(100,100,false,this);
List<Missile> missiles = new ArrayList<Missile>();

Image offScreeImage = null; // 双缓冲 // 绘制圆
public void paint(Graphics g) {

g.drawString("missiles count : " + missiles.size(), 10, 50);

for(int i = 0; i < missiles.size(); i++){
Missile m = missiles.get(i);
m.draw(g);
}
//myTank.draw(g);
enemyTank.draw(g);
}

// 将所有东西画在虚拟的画布上
public void update(Graphics g) {
if(offScreeImage == null){
offScreeImage = this.createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreeImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.GREEN);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreeImage, 0, 0, null);
} public void lauchFrame() {
this.setLocation(400,300);
this.setSize(GAME_WIDTH, GAME_HEIGHT);
this.setResizable(false);
this.setBackground(Color.GREEN);

this.addKeyListener(new KeyMonitor());

this.setVisible(true);

// awt创建关闭按钮
this.addWindowListener(new WindowAdapter() {

/*
 *  匿名类:适用于类短小,不涉及将来的拓展,
 *  不涉及重要的业务逻辑
 */

public void windowClosing(WindowEvent e) {
System.exit(0);
}

});

new Thread(new PaintThread()).start();

} public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
} private class PaintThread implements Runnable {

public void run() {
while(true){
// 每重画一次,移动一次
repaint();
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
} private class KeyMonitor extends KeyAdapter { public void keyPressed(KeyEvent e) {
myTank.KeyPressed(e);
} public void keyReleased(KeyEvent e) {
myTank.KeyReleased(e);
}


}
}import java.awt.Color;
import java.awt.Graphics;
public class Missile {

public static final int WIDTH = 10, HEIGHT = 10;

// 子弹位置
int x, y;

public static final int XSPEED = 10, YSPEED = 10;

// 方向
Tank.Direction dir;

private boolean live = true;

private TankClient tc; public boolean isLive() {
return live;
} public Missile(int x, int y, Tank.Direction dir) {
this.x = x;
this.y = y;
this.dir = dir;
}

public Missile(int x, int y,Tank.Direction dir, TankClient tc){
this(x,y,dir);
this.tc = tc;
} public void draw(Graphics g){
Color c = g.getColor();
g.setColor(Color.BLACK);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);
this.move();
} private void move() {
switch(dir){
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}

if(x < 0 || y < 0 || x > TankClient.GAME_WIDTH || y > TankClient.GAME_HEIGHT ){
this.live = false;
tc.missiles.remove(this);
}
}
}import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class Tank {

public static final int XSPEED = 5, YSPEED = 5;
public static final int WIDTH = 30, HEIGHT = 30;

TankClient tc;

private boolean good; private int x, y; // 位置坐标

private boolean bL = false, bU = false, bR = false, bD = false; // 按键是否按下 enum Direction{L, LU, U, RU, R, RD, D, LD, STOP};

private Direction dir;
private Direction barrelDir = Direction.D;

public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
}

public Tank(int x, int y, boolean good, TankClient tc){
this(x,y,good);
this.tc = tc;
} 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 void draw(Graphics g) {
Color c = g.getColor();
if(good) g.setColor(Color.RED);
else g.setColor(Color.BLUE);
g.setColor(Color.RED);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c);

switch(barrelDir){
case L:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT / 2);
break;
case LU:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y);
break;
case U:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y);
break;
case RU:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH , y);
break;
case R:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH , y + Tank.HEIGHT / 2);
break;
case RD:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH, y + Tank.HEIGHT);
break;
case D:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x + Tank.WIDTH / 2, y + Tank.HEIGHT);
break;
case LD:
g.drawLine(x + Tank.WIDTH / 2, y + Tank.HEIGHT / 2, x, y + Tank.HEIGHT);
break;
}

this.move();
}

private void move(){
switch(dir){
case L:
x -= XSPEED;
break;
case LU:
x -= XSPEED;
y -= YSPEED;
break;
case U:
y -= YSPEED;
break;
case RU:
x += XSPEED;
y -= YSPEED;
break;
case R:
x += XSPEED;
break;
case RD:
x += XSPEED;
y += YSPEED;
break;
case D:
y += YSPEED;
break;
case LD:
x -= XSPEED;
y += YSPEED;
break;
case STOP:
break;
}

if(this.dir != Direction.STOP){
this.barrelDir = this.dir;
}

if(x < 0) x = 0;
if(y < 25) y = 25;
if(x + Tank.WIDTH > TankClient.GAME_WIDTH) x = TankClient.GAME_WIDTH - Tank.WIDTH;
if(y + Tank.HEIGHT > TankClient.GAME_HEIGHT) y = TankClient.GAME_HEIGHT - Tank.HEIGHT; 
}

public Missile fire() {
int x = this.x + Tank.WIDTH / 2 - Missile.WIDTH / 2;
int y = this.y + Tank.WIDTH / 2 - Missile.WIDTH / 2;
Missile m = new Missile(x, y, barrelDir, this.tc);
tc.missiles.add(m);
return m;
}

public void KeyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
}
this.locateDirection();
}

public void KeyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_CONTROL:
this.fire();
break;
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
}
this.locateDirection();
} void locateDirection() {
if(bL && !bU && !bR && !bD){
dir = Direction.L;
}
else if(bL && bU && !bR && !bD){
dir = Direction.LU;
}
else if(!bL && bU && !bR && !bD){
dir = Direction.U;
}
else if(!bL && bU && bR && !bD){
dir = Direction.RU;
}
else if(!bL && !bU && bR && !bD){
dir = Direction.R;
}
else if(!bL && !bU && bR && bD){
dir = Direction.RD;
}
else if(!bL && !bU && !bR && bD){
dir = Direction.D;
}
else if(bL && !bU && !bR && bD){
dir = Direction.LD;
}
else if(!bL && !bU && !bR && !bD){
dir = Direction.STOP;
}
}

}

解决方案 »

  1.   

    你把Tank类 KeyPressed和KeyReleased 方法里面的 this.locateDirection();  去掉  把他放到move()里面去
      

  2.   

    @mjdd1989我试了你的方法还是不行啊?
    不过还是谢谢你
      

  3.   

    在class Tank中的成员private Direction dir;
    应该初始化为private Direction dir = Direction.STOP;
    否则在执行 draw(Graphics g)方法时出现空指针错误。
    另外语句if(good) g.setColor(Color.RED);
            else g.setColor(Color.BLUE);    
            g.setColor(Color.RED);
    最后一个是多余的。