子弹类Missile
1.首先在src目录下新建一个文件夹Images,将图片拷贝到该文件夹下
2.在子弹类中添加以下代码:
private static Image[] missileImages=null;
private static Toolkit tk=Toolkit.getDefaultToolkit();
private static Map
//创建静态数组和静态Map 是为了不用没次新建类对象的时候都从硬盘将图片调到内存,一次调入后,以后直接从内存访问,提高访问速度
static{
missileImages=new Image[]{
tk.getImage(Missile.class.getClassLoader().getResource("Images/missileL.gif")),
tk.getImage(Missile.class.getClassLoader().getResource("Images/missileLU.gif")),
tk.getImage(Missile.class.getClassLoader().getResource("Images/missileU.gif")),
tk.getImage(Missile.class.getClassLoader().getResource("Images/missileRU.gif")),
tk.getImage(Missile.class.getClassLoader().getResource("Images/missileR.gif")),
tk.getImage(Missile.class.getClassLoader().getResource("Images/missileRD.gif")),
tk.getImage(Missile.class.getClassLoader().getResource("Images/missileD.gif")),
tk.getImage(Missile.class.getClassLoader().getResource("Images/missileLD.gif")),
};
map.put("L", missileImages[0]);
map.put("LU", missileImages[1]);
map.put("U", missileImages[2]);
map.put("RU", missileImages[3]);
map.put("R", missileImages[4]);
map.put("RD", missileImages[5]);
map.put("D", missileImages[6]);
map.put("LD", missileImages[7]);
}
//画子弹的方法
public void draw(Graphics g) {
if(!live) {
tc.missiles.remove(this);
return;
}
//根据子弹的方向画子弹
switch(dir) {
case L:
g.drawImage(map.get("L"),x,y,null);
break;
case LU:
g.drawImage(map.get("LU"),x,y,null);
break;
case U:
g.drawImage(map.get("D"),x,y,null);
break;
case RU:
g.drawImage(map.get("RU"),x,y,null);
break;
case R:
g.drawImage(map.get("R"),x,y,null);
break;
case RD:
g.drawImage(map.get("RD"),x,y,null);
break;
case D:
g.drawImage(map.get("U"),x,y,null);
break;
case LD:
g.drawImage(map.get("LD"),x,y,null);
break;
}
//每画完一次,子弹移动一次
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) {
live = false;
}
}