贪吃蛇VS坦克大战:
package com.tank;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.util.List;
/**
-
炮弹类
-
@author LEON
*/
public class Missile {
// 炮弹位置
private int x;
private int y;
// 炮弹速度
private static final int speed = 10;
// 炮弹长宽
private int width = 12;
private int height = 12;
//炮弹是否存活
private boolean isLive = true;
//炮弹是好是坏
private boolean isFriend;
// 坦克实体
private TankClient tankClient;
private int direction = 4;
private boolean isCheat = false;
//获取系统工具,读取系统文件
private static Toolkit toolkit = Toolkit.getDefaultToolkit();
private static Image[] missileImages = {
toolkit.getImage(Boom.class.getClassLoader().getResource(“img/missile1.gif”)),
toolkit.getImage(Boom.class.getClassLoader().getResource(“img/missile2.gif”)),
toolkit.getImage(Boom.class.getClassLoader().getResource(“img/missile3.gif”)),
toolkit.getImage(Boom.class.getClassLoader().getResource(“img/missile4.gif”))
};/**
- 画炮弹
- @param g
*/
public void drowMissile(Graphics g) {
if (isLive == false) {
return;
}
if (isCheat) {
// 设置炮弹颜色
g.setColor(Color.BLACK);
// 设置位置和大小
g.fillOval(x, y, width, height);
} else {
switch (direction) {
case 1:
g.drawImage(missileImages[0], x, y, null);
break;
case 2:
g.drawImage(missileImages[1], x, y, null);
break;
case 3:
g.drawImage(missileImages[2], x, y, null);
break;
case 4:
g.drawImage(missileImages[3], x, y, null);
break;
default:
break;
}
}
moveMissile();
}
/**
- 移动炮弹,当超过窗口的时候消除子弹
*/
private void moveMissile() {
switch (direction) {
case 1:
y -= speed;
break;
case 2:
y += speed;
break;
case 3:
x -= speed;
break;
case 4:
x += speed;
break;
default:
break;
}
if (x < 0 || x > 1000 || y < 0 || y > 600) {
this.isLive = false;
this.tankClient.missiles.remove(this);
}
}
/**
- 当攻击到坦克时,子弹和坦克都消失
- @param tanks
*/
public void hitTank(List tanks) {
for (int i = 0; i < tanks.size(); i++) {
Tank tank = tanks.get(i);
if (getRect().intersects(tank.getRect()) && tank.isLive() && this.isFriend != tank.isFriend()) {
tank.setLive(false);
tanks.set(i, tank);
Boom boom = new Boom(x, y, this.tankClient);
this.tankClient.booms.add(boom);
this.tankClient.missiles.remove(this);
}
}
}
/**
- 当攻击到墙时
- @param wall 撞击到墙时的动作
*/
public void hitWall(Wall wall) {
if (this.isLive && getRect().intersects(wall.getRect()) && !this.isFriend) {
this.tankClient.missiles.remove(this);
}
}
/**
- 敌方坦克攻击到我方时
- @param tank
*/
public void hitTank(Tank tank) {
if (getRect().intersects(tank.getRect()) && tank.isLive() && this.isFriend != tank.isFriend()) {
tank.setLive(false);
Boom boom = new Boom(x, y, this.tankClient);
this.tankClient.booms.add(boom);
this.tankClient.missiles.remove(this);
}
}
/**
- 得到子弹的方框
- @return
*/
private Rectangle getRect() {
return new Rectangle(x, y, width, height);
}
/**
- 炮弹是否存活的getter和setter
- @return
*/
public boolean isLive() {
return isLive;
}
public void setLive(boolean isLive) {
this.isLive = isLive;
}public Missile() {
}public Missile(int x, int y, int direction, TankClient tankClient, boolean isFriend) {
this.x = x;
this.y = y;
this.direction = direction;
this.tankClient = tankClient;
this.isFriend = isFriend;
}public Missile(int x, int y, int dir, TankClient tankClient, boolean isFriend, int width, int height, boolean isCheat) {
this.x = x;
this.y = y;
this.direction = dir;
this.tankClient = tankClient;
this.isFriend = isFriend;
this.width = width;
this.height = height;
this.isCheat = isCheat;
}
}