java坦克大战(源码)

来源:互联网 发布:vpn软件排行 编辑:程序博客网 时间:2024/05/16 07:19

这是Java编译的部分坦克大战





import java.awt.Graphics;

import java.awt.Image;
import java.awt.Toolkit;


public class BombTank {
private int x, y;
private boolean live = true; // 初始状态为活着的
private TankClient tc;
private static Toolkit tk = Toolkit.getDefaultToolkit();


private static Image[] imgs = { // 存储爆炸图片 从小到大的爆炸效果图
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/1.gif")),
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/2.gif")),
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/3.gif")),
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/4.gif")),
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/5.gif")),
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/6.gif")),
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/7.gif")),
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/8.gif")),
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/9.gif")),
tk.getImage(BombTank.class.getClassLoader().getResource(
"images/10.gif")), };
int step = 0;


public BombTank(int x, int y, TankClient tc) { // 构造函数
this.x = x;
this.y = y;
this.tc = tc;
}


public void draw(Graphics g) { // 画出爆炸图像


if (!live) { // 坦克消失后删除爆炸图
tc.bombTanks.remove(this);
return;
}
if (step == imgs.length) {
live = false;
step = 0;
return;
}


g.drawImage(imgs[step], x, y, null);
step++;
}

}



import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Bullets {
public static  int speedX = 20;
public static  int speedY = 20; // 子弹的全局静态速度


public static final int width = 10;
public static final int length = 10;


private int x, y;
Direction diretion;


private boolean good;
private boolean live = true;


private TankClient tc;


private static Toolkit tk = Toolkit.getDefaultToolkit();
private static Image[] bulletImages = null;
private static Map<String, Image> imgs = new HashMap<String, Image>(); // 定义Map键值对,是不同方向对应不同的弹头


static {
bulletImages = new Image[] { // 不同方向的子弹
tk.getImage(Bullets.class.getClassLoader().getResource(
"images/bulletL.gif")),


tk.getImage(Bullets.class.getClassLoader().getResource(
"images/bulletU.gif")),


tk.getImage(Bullets.class.getClassLoader().getResource(
"images/bulletR.gif")),


tk.getImage(Bullets.class.getClassLoader().getResource(
"images/bulletD.gif")),


};


imgs.put("L", bulletImages[0]); // 加入Map容器


imgs.put("U", bulletImages[1]);


imgs.put("R", bulletImages[2]);


imgs.put("D", bulletImages[3]);


}


public Bullets(int x, int y, Direction dir) { // 构造函数1,传递位置和方向
this.x = x;
this.y = y;
this.diretion = dir;
}


// 构造函数2,接受另外两个参数
public Bullets(int x, int y, boolean good, Direction dir, TankClient tc) {
this(x, y, dir);
this.good = good;
this.tc = tc;
}


private void move() {


switch (diretion) {
case L:
x -= speedX; // 子弹不断向左进攻
break;


case U:
y -= speedY;
break;


case R:
x += speedX; // 字段不断向右
break;


case D:
y += speedY;
break;


case STOP:
break;
}


if (x < 0 || y < 0 || x > TankClient.Fram_width
|| y > TankClient.Fram_length) {
live = false;
}
}


public void draw(Graphics g) {
if (!live) {
tc.bullets.remove(this);
return;
}


switch (diretion) { // 选择不同方向的子弹
case L:
g.drawImage(imgs.get("L"), x, y, null);
break;


case U:
g.drawImage(imgs.get("U"), x, y, null);
break;


case R:
g.drawImage(imgs.get("R"), x, y, null);
break;


case D:
g.drawImage(imgs.get("D"), x, y, null);
break;


}


move(); // 调用子弹move()函数
}


public boolean isLive() { // 判读是否还活着
return live;
}


public Rectangle getRect() {
return new Rectangle(x, y, width, length);
}


public boolean hitTanks(List<Tank> tanks) {// 当子弹打到坦克时
for (int i = 0; i < tanks.size(); i++) {
if (hitTank(tanks.get(i))) { // 对每一个坦克,调用hitTank
return true;
}
}
return false;
}


public boolean hitTank(Tank t) { // 当子弹打到坦克上


if (this.live && this.getRect().intersects(t.getRect()) && t.isLive()
&& this.good != t.isGood()) {


BombTank e = new BombTank(t.getX(), t.getY(), tc);
tc.bombTanks.add(e);
if (t.isGood()) {
t.setLife(t.getLife() - 10); // 受一粒子弹寿命减少50,接受4枪就死,总生命值200
if (t.getLife() <= 0)
t.setLive(false); // 当寿命为0时,设置寿命为死亡状态
} else {
t.setLive(false); 
}


this.live = false;


return true; // 射击成功,返回true
}
return false; // 否则返回false
}


public boolean hitWall(CommonWall w) { // 子弹打到CommonWall上
if (this.live && this.getRect().intersects(w.getRect())) {
this.live = false;
this.tc.otherWall.remove(w); // 子弹打到CommonWall墙上时则移除此击中墙
this.tc.homeWall.remove(w);
return true;
}
return false;
}


public boolean hitWall(MetalWall w) { // 子弹打到金属墙上
if (this.live && this.getRect().intersects(w.getRect())) {
this.live = false;
//this.tc.metalWall.remove(w); //子弹不能穿越金属墙
return true;
}
return false;
}


public boolean hitHome() { // 当子弹打到家时
if (this.live && this.getRect().intersects(tc.home.getRect())) {
this.live = false;
this.tc.home.setLive(false); // 当家接受一枪时就死亡
return true;
}
return false;
}


}

import java.awt.*;


public class CommonWall {
public static final int width = 20; //设置墙的固定参数
public static final int length = 20;
int x, y;


TankClient tc;
private static Toolkit tk = Toolkit.getDefaultToolkit();
private static Image[] wallImags = null;
static {
wallImags = new Image[] { // 储存commonWall的图片
tk.getImage(CommonWall.class.getResource("Images/commonWall.gif")), };
}


public CommonWall(int x, int y, TankClient tc) { // 构造函数
this.x = x;
this.y = y;
this.tc = tc; // 获得界面控制
}


public void draw(Graphics g) {// 画commonWall
g.drawImage(wallImags[0], x, y, null);
}


public Rectangle getRect() {  //构造指定参数的长方形实例
return new Rectangle(x, y, width, length);
}
}




原创粉丝点击