心血来潮练练技术用Java编写了一个简单的坦克大战,实现单人攻关模式和双人对战模式。
游戏操作实现的主要功能有:
单人攻关模式:
1.玩家坦克可以上下左右、左上、左下、右上、右下八个方向移动。
2.击中坦克时,敌方坦克立即消失(玩家坦克掉血),显示爆炸效果,子弹击中鹰碉堡时,游戏结束,子弹击中墙壁时,子弹消失
3.移动过程中坦克与坦克,坦克与草地、河流、墙壁会根据实际通过或停止。
4.菜单处理:(1)暂停/继续游戏、背景音乐的开启/关闭、重新开始、返回开始界面。(2)查看玩家得分记录、查看最高分。(3)选择难度改变敌方坦克颜色、子弹颜色、地图。
双人对战模式:
1.初始血量和攻击力一样,获得补给后增加血量和攻击力并改变子弹颜色(但累加后血量不能超过200,攻击力不超过40)
其他的也没有什么好说的,在Java文件中都有注释,编程小白格式不是那样标准,仅供学习交流,望多多海涵。
先帖效果图:
1、欢迎界面图(点击开始游戏后):
(界面上的紫色、绿色、咖啡色、黄色坦克用线程实现的轮播动画右出右进,不是瞎贴的~~~)
2、游戏界面图1(默认关卡):
(下方黑条是玩家血条)
3、游戏界面2(选择游戏难度1后):
4、游戏界面3(双人对战模式):
(中间降落伞可以增加玩家血量和攻击力)
其他界面图就不一一贴出来啦~~~
主要贴出主界面、坦克类、子弹类Java文件。
主界面Java文件:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class GameFrame extends JFrame implements ActionListener, Runnable {
static int enemyNumber = 1;// 敌人坦克初始数量
static int round = 1;//战斗轮数
static int difficulty = 0; //初始地图
public void setDifficulty(int difficulty1){
difficulty = difficulty1;
}
// 初始化地图和设置地图
{
Map.changeMap(difficulty);
}
static List tanks = new ArrayList();// 装坦克的容器
{
tanks.add(new Tank(80 , 50, false, Tank.Direction.D));//初始化一个坦克
}
static List missiles = new ArrayList();// 装子弹的容器
static List explodes = new ArrayList();// 装爆炸的容器
static List walls = new ArrayList();// 装普通墙的容器
static List hWalls = new ArrayList();// 装金属墙的容器
static List trees = new ArrayList();// 装树的容器
static List rivers = new ArrayList();// 装河的容器
public static boolean play = true;// 决定音乐的开启与关闭
public static boolean printable = true;// 决定线程的开启与关闭
static Home home = new Home();// 实例化一个老家
static Tank hero = new Tank(220, 480, true, Tank.Direction.STOP);// 玩家坦克位置
public GameFrame() {
addKeyListener(hero);
createMenu();// 创建菜单
setTitle("坦克大战");
setVisible(true);
setSize(800, 600);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
Thread th = new Thread(this);
th.start();
// 在面板上绘制坦克、子弹、墙、老家
JPanel jp = new JPanel() {
{
setBackground(Color.gray);// 设置面板背景颜色
}
public void paint(Graphics g) {
super.paint(g);
g.drawString("你的分数:" + Missile.getCount(), 10, 20);
g.drawString("你的生命值:" + hero.getLife(), 10, 40);
g.drawString("敌人对你的伤害:" + Missile.getHurt(), 10, 60);
g.drawString("第:" + round + " 轮战斗" + ",敌人总数:"+ tanks.size(), 10, 80);
g.drawRect(45, 515, 700, 15);
g.fillRect(45, 515, hero.getLife()*7, 15);
hero.draw(g);// 画出玩家的坦克
home.draw(g);//画出自己的家
hero.collideWithTanks(tanks);//玩家坦克撞上敌方坦克
// 把容器里面的子弹都画出来
for (int i = 0; i < missiles.size(); i++) {
Missile m = missiles.get(i);
m.draw(g);// 画子弹
m.hitTanks(tanks);// 玩家子弹攻击敌方
m.hitTank(hero);// 敌方子弹攻击玩家
m.hitHome();// 敌方子弹攻击老家
for (int j = 0; j < walls.size(); j++) {// 每一个子弹打到其他墙上
Wall w = walls.get(j);
m.hitWalls(w);
}
for (int j = 0; j < hWalls.size(); j++) {// 每一个子弹打到金属墙上
HardWall hw = hWalls.get(j);
m.hitWalls(hw);
}
}
// 把容器里面的坦克都画出来
for (int i = 0; i < tanks.size(); i++) {
Tank t = tanks.get(i);
t.draw(g);
//画出普通墙
for (int j = 0; j < walls.size(); j++) {
Wall w = walls.get(j);
w.draw(g);
t.collideWall(w);// 每一个坦克撞到普通墙
hero.collideWall(w);
}
// 画出金属墙
for (int n = 0; n < hWalls.size(); n++) {
HardWall hw = hWalls.get(n);
hw.draw(g);
t.collideHardWall(hw);// 每一个坦克撞到金属墙
hero.collideHardWall(hw);
}
// 画出河
for (int n = 0; n < rivers.size(); n++) {
River river = rivers.get(n);
river.draw(g);
t.collideRiver(river);// 每一个坦克撞到金属墙
hero.collideRiver(river);
}
// 画出树
for (int n = 0; n < trees.size(); n++) {
Tree tree = trees.get(n);
tree.draw(g);
}
t.collideWithTanks(tanks); // 撞到自己的人
}
// 把容器里面的爆炸全部画出来
for (int i = 0; i < explodes.size(); i++) {
Explode e = explodes.get(i);
e.draw(g);
}
//每关卡的敌人设置
if(tanks.size()==0 && round<6){
for (int i = 0; i < enemyNumber*2; i++) {
Tank t=null;
if(i<2){
t=new Tank(100 + 70 * i, 50, false, Tank.Direction.L);
}else if(i>3){
t=new Tank(450, i * 50 + 20, false, Tank.Direction.R);
}else{
t=new Tank(50+i * 50 , 500, false, Tank.Direction.U);
}
tanks.add(t);
}
enemyNumber++;
round++;
}
}
};
add(jp);
}
// 创建菜单栏
public void createMenu() {
// 菜单栏
JMenuBar jmb = new JMenuBar();
// 菜单项
JMenu jm1 = new JMenu("游戏");
JMenu jm2 = new JMenu("历史记录");
JMenu jm3 = new JMenu("帮助");
JMenu jm4 = new JMenu("游戏难度");
// 菜单项按钮
JMenuItem jmi1 = new JMenuItem("暂停/继续");
JMenuItem jmi2 = new JMenuItem("继续");
JMenuItem jmi10 = new JMenuItem("重新开始");
JMenuItem jmi3 = new JMenuItem("背景音乐开/关");
JMenuItem jmi11 = new JMenuItem("返回到主界面");
JMenuItem jmi4 = new JMenuItem("最高分");
JMenuItem jmi5 = new JMenuItem("玩家得分记录");
JMenuItem jmi6 = new JMenuItem("关于游戏");
JMenuItem jmi7 = new JMenuItem("普通模式");
JMenuItem jmi8 = new JMenuItem("人间模式");
JMenuItem jmi9 = new JMenuItem("地狱模式");
// 添加菜单
jmb.add(jm1);
jmb.add(jm2);
jmb.add(jm4);
jmb.add(jm3);
// 设置游戏菜单项
jm1.add(jmi1);
// jm1.add(jmi2);
jm1.add(jmi10);
jm1.add(jmi3);
jm1.add(jmi11);
jm2.add(jmi4);
jm2.add(jmi5);
jm3.add(jmi6);
jm4.add(jmi7);
jm4.add(jmi8);
jm4.add(jmi9);
// 为按钮添加监听事件
jmi1.addActionListener(this);
jmi1.setActionCommand("stop");
jmi2.addActionListener(this);
jmi2.setActionCommand("continue");
jmi3.addActionListener(this);
jmi3.setActionCommand("music");
jmi4.addActionListener(this);
jmi4.setActionCommand("rank");
jmi5.addActionListener(this);
jmi5.setActionCommand("history");
jmi6.addActionListener(this);
jmi6.setActionCommand("help");
jmi7.addActionListener(this);
jmi7.setActionCommand("difficulty1");
jmi8.addActionListener(this);
jmi8.setActionCommand("difficulty2");
jmi9.addActionListener(this);
jmi9.setActionCommand("difficulty3");
jmi10.addActionListener(this);
jmi10.setActionCommand("restart");
jmi11.addActionListener(this);
jmi11.setActionCommand("back");
// 将菜单放在窗体上
this.setJMenuBar(jmb);
}
public static void main(String[] args) {
new GameFrame();
}
public void run() {
// 每隔20毫秒 重新画图
while (printable) {
try {// 记录玩家分数
if (Missile.getCount()==31 || !hero.isLive()) {
printable = false;// 停止线程
if(Missile.getCount()==31){
JOptionPane.showMessageDialog(null, "您赢得了比赛!");
//判断是否是最高分
maxScore();
}
if(!hero.isLive()){
JOptionPane.showMessageDialog(null, "您输掉了比赛!");
//判断是否是最高分
maxScore();
}
if(!play)Music.aau.stop();//判断是否关闭音乐
try {
FileWriter fw = new FileWriter("txt/score.txt", true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("\t\t" + StartFrame.getUserName() + "获得的分数为:" + Missile.getCount() + "\n");// 往已有的文件上添加字符串
bw.close();
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Thread.sleep(20);
} catch (Exception e) {
e.printStackTrace();
}
repaint();
}
}
//最高分判定与写入
public void maxScore(){
String str = MaxScore.readTxt();
String[] newstr=str.split(":");
int b = Integer.parseInt(newstr[1]);
if(Missile.getCount()>b) {
b=Missile.getCount();
MaxScore.write(StartFrame.getUserName(),b);
if(StartFrame.getUserName().equals("匿名玩家")){
JOptionPane.showMessageDialog(null, "恭喜您获得了最高分"+b);
}else{
JOptionPane.showMessageDialog(null, StartFrame.getUserName()+","+"恭喜您获得了最高分"+b);
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("stop")) {
if(printable){
printable = false;// 停止线程
}else{
printable = true;
new Thread(this).start(); // 启动线程
}
}
if (e.getActionCommand().equals("continue")) {
if (!printable) {
printable = true;
new Thread(this).start(); // 线程启动
}
}
if (e.getActionCommand().equals("music")) {
Music.musicBack(play);
play = !play;//点击播放后置为false
}
if (e.getActionCommand().equals("restart")) {
printable = false;
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(this, "您确认要开始新游戏!", "",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,options, options[0]);
if (response == 0) {
//初始化所有变量
printable = true;
Home.setLive(true);// 重新激活老家
GameFrame.hero.setLife(100);
GameFrame.hero.setLive(true);// 重新激活玩家坦克
Missile.count=0;// 重置得分
round=1;// 重置轮数
enemyNumber = 1;// 重置敌人数量
Tank.setNspeed(3);// 敌人坦克速度
Tank.setTankColor(0);// 坦克颜色
Home.setHomeLocation(290, 250);// 重置老家位置
Missile.setMissileColor(0);// 子弹颜色
Missile.setNspeed(4);// 敌人坦克子弹速度
Missile.setHurt(20);// 敌人坦克伤害
tanks.clear();// 坦克清空
missiles.clear();// 子弹清空
walls.clear();// 墙清空
hWalls.clear();// 金属墙清空
rivers.clear();// 河流清空
trees.clear();// 树清空
setDifficulty(0);// 重置地图
this.dispose();// 关闭当前窗口
new GameFrame();
} else {
printable = true;
new Thread(this).start(); // 线程启动
}
}
if(e.getActionCommand().equals("back")){
printable = false;
Object[] options = { "确定", "取消" };
int response = JOptionPane.showOptionDialog(this, "您确认要返回到主界面!", "",JOptionPane.YES_OPTION, JOptionPane.QUESTION_MESSAGE, null,options, options[0]);
if(response == 0){
tanks.clear();
GameFrame.hero.setLife(100);
this.dispose();
new StartFrame();
printable = true;
}else {
printable = true;
new Thread(this).start(); // 线程启动
}
}
if (e.getActionCommand().equals("difficulty1")) {
tanks.clear();
missiles.clear();
walls.clear();
Tank.setNspeed(7);// 改变敌人坦克速度
Tank.setTankColor(1);// 改变敌人坦克颜色
Missile.setNspeed(8);// 改变敌人子弹速度
Missile.setMissileColor(1);// 子弹颜色
setDifficulty(1);// changeMap根据设置的值改变地图
Home.setHomeLocation(370,500);// 设置老家位置
this.dispose();
new GameFrame();
}
if (e.getActionCommand().equals("difficulty2")) {
tanks.clear();
missiles.clear();
walls.clear();
Tank.setNspeed(9);
Tank.setTankColor(2);// 改变敌人坦克颜色
Missile.setNspeed(10);
Missile.setHurt(30);// 敌人坦克伤害
Missile.setMissileColor(2);// 子弹颜色
Home.setHomeLocation(370,250);// 设置老家位置
setDifficulty(2);// changeMap根据设置的值改变地图
this.dispose();
new GameFrame();
}
if (e.getActionCommand().equals("difficulty3")) {// 没清除墙,相当于两堵墙叠加在一起要打两次才消失
tanks.clear();
missiles.clear();
Tank.setNspeed(11);
Tank.setTankColor(3);// 改变敌人坦克颜色
Missile.setNspeed(12);
Missile.setMissileColor(3);// 子弹颜色
Missile.setHurt(40);
Home.setHomeLocation(390,250);// 设置老家位置
setDifficulty(3);// changeMap根据设置的值改变地图
this.dispose();
new GameFrame();
}
if (e.getActionCommand().equals("history")) {
try {
new ScoreFrame();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
if (e.getActionCommand().equals("rank")) {
String str = MaxScore.readTxt();
String[] newstr=str.split(":");
int b = Integer.parseInt(newstr[1]);
if(newstr[0].equals("")){
JOptionPane.showMessageDialog(null, "匿名玩家"+"获得了最高分:"+b);
}else{
JOptionPane.showMessageDialog(null, newstr[0]+"获得了最高分:"+b);
}
}
if (e.getActionCommand().equals("help")) {
printable = false;// 停止线程
JOptionPane.showMessageDialog(null, "W、向上,A、向下,S、向下,D、向上,J、发射炮弹,R、加血"+"\n"+"开发者:机械熊猫侠"+"\n"+"联系作者(qq):[email protected]","提示",JOptionPane.INFORMATION_MESSAGE);
printable = true;
new Thread(this).start(); // 线程启动
}
}
}
坦克类:
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.List;
import javax.swing.ImageIcon;
public class Tank implements KeyListener {
private int x, y;// 坦克坐标
private int oldX, oldY;// 坦克上一步坐标
static int SPEED = 3;// 好坦克速度
static int NSPEED = 3;// 坏坦克速度
private int life = 100;// 生命值
private boolean live = true;// 坦克存活
private boolean good;// 区分坦克
private static Random r = new Random();// 随机数产敌人数
private int step; // 产生一个随机数,随机模拟坦克的移动路径
boolean bU = false, bD = false, bL = false, bR = false;
private Direction direction = Direction.STOP;// 坦克初始状态
private Direction kdirection = Direction.U;// 坦克初始方向
enum Direction {// 方向的枚举
U, D, L, R, LU, LD, RU, RD, STOP
};
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void setY(int y) {
this.y = y;
}
public int getY() {
return y;
}
public void setLife(int life) {
this.life = life;
}
public int getLife() {
return life;
}
public void setLive(boolean live) {
this.live = live;
}
public boolean isLive() {
return live;
}
public void setGood(boolean good) {
this.good = good;
}
public boolean isGood() {
return good;
}
public static void setNspeed(int NSPEED1){
NSPEED = NSPEED1;
}
// 判断范围(子弹进入坦克)
public Rectangle getRect() {
return new Rectangle(x, y, 40, 40);
}
static Image[] tanksImages = null;
static {
tanksImages = new Image[] {
// 玩家坦克8个方向的图片
new ImageIcon("images/tankU.gif").getImage(),
new ImageIcon("images/tankD.gif").getImage(),
new ImageIcon("images/tankL.gif").getImage(),
new ImageIcon("images/tankR.gif").getImage(),
new ImageIcon("images/tankLU.gif").getImage(),
new ImageIcon("images/tankLD.gif").getImage(),
new ImageIcon("images/tankRU.gif").getImage(),
new ImageIcon("images/tankRD.gif").getImage(),
// 敌方4个方向的图片
new ImageIcon("images/EtankU.png").getImage(),
new ImageIcon("images/EtankD.png").getImage(),
new ImageIcon("images/EtankL.png").getImage(),
new ImageIcon("images/EtankR.png").getImage(),
};
}
// 坦克的构造方法
public Tank(int x, int y, boolean good) {
this.x = x;
this.y = y;
this.good = good;
this.oldX = x;
this.oldY = y;
}
public Tank(int x, int y, boolean good, Direction direction) {
this(x, y, good);
this.direction = direction;
}
// 画坦克的方法
public void draw(Graphics g) {
if (live && good ) {
switch (kdirection) {
case U:
g.drawImage(tanksImages[0], x, y, null);
break;
case D:
g.drawImage(tanksImages[1], x, y, null);
break;
case L:
g.drawImage(tanksImages[2], x, y, null);
break;
case R:
g.drawImage(tanksImages[3], x, y, null);
break;
case LU:
g.drawImage(tanksImages[4], x, y, null);
break;
case LD:
g.drawImage(tanksImages[5], x, y, null);
break;
case RU:
g.drawImage(tanksImages[6], x, y, null);
break;
case RD:
g.drawImage(tanksImages[7], x, y, null);
break;
}
}else if(live&&!good){
switch (kdirection) {
case U:
g.drawImage(tanksImages[8], x, y, null);
break;
case D:
g.drawImage(tanksImages[9], x, y, null);
break;
case L:
g.drawImage(tanksImages[10], x, y, null);
break;
case R:
g.drawImage(tanksImages[11], x, y, null);
break;
}
}
move();
}
void move() {
this.oldX = x;
this.oldY = y;
if(!good){
SPEED = NSPEED;
}else{
SPEED = 3;
}
switch (direction) {
case U:
y -= SPEED;
break;
case D:
y += SPEED;
break;
case L:
x -= SPEED;
break;
case R:
x += SPEED;
break;
case LU:
x -= SPEED;
y -= SPEED;
break;
case LD:
x -= SPEED;
y += SPEED;
break;
case RU:
x += SPEED;
y -= SPEED;
break;
case RD:
x += SPEED;
y += SPEED;
break;
case STOP:
break;
}
if (this.direction != Direction.STOP)// 判断停止方向与行驶方向是否一致
this.kdirection = this.direction;
if (x < 0)x = 0;
if (y < 0)y = 0;
if (x > 750)x = 750;
if (y > 500)y = 500;
if (!good) {
Direction[] directons = Direction.values();
if (step == 0) {
step = r.nextInt(20) + 20;// 产生随机路径
int rn = r.nextInt(4);
direction = directons[rn];// 产生随机方向
}
step--;
if (r.nextInt(40) > 38)// 产生随机数,控制敌人开火
this.fire();
}
}
// 发出一发子弹的方法,向missiles里面添加子弹
public Missile fire() {
int x = this.x + 15;
int y = this.y + 15;
Missile m = new Missile(x, y,good,kdirection);
GameFrame.missiles.add(m);
return m;
}
public void locateDirection() {
if(bL && !bU && !bR && !bD) direction = Direction.L;
else if(bL && bU && !bR && !bD) direction = Direction.LU;
else if(!bL && bU && !bR && !bD) direction = Direction.U;
else if(!bL && bU && bR && !bD) direction = Direction.RU;
else if(!bL && !bU && bR && !bD) direction = Direction.R;
else if(!bL && !bU && bR && bD) direction = Direction.RD;
else if(!bL && !bU && !bR && bD) direction = Direction.D;
else if(bL && !bU && !bR && bD) direction = Direction.LD;
else if(!bL && !bU && !bR && !bD) direction = Direction.STOP;
}
// 存放撞击后前的坐标
private void changToOldDirection() {
this.x = oldX;
this.y = oldY;
}
// 判断坦克是否撞普通墙
public boolean collideWall(Wall w) {
if (this.live && this.getRect().intersects(w.getRect())) {
this.changToOldDirection();// 转换到原来的坐标上
return true;
}
return false;
}
// 判断坦克是否撞金属墙
public boolean collideHardWall(HardWall hw) {
if (this.live && this.getRect().intersects(hw.getRect())) {
this.changToOldDirection();// 转换到原来的坐标上
return true;
}
return false;
}
// 判断敌方坦克是否相撞
public boolean collideWithTanks(List tanks) {
for (int i = 0; i < tanks.size(); i++) {
Tank t = tanks.get(i);
if (this != t) {
if (this.live && t.isLive()&& this.getRect().intersects(t.getRect())) {
this.changToOldDirection();// 玩家坦克转换到原来的坐标上
t.changToOldDirection();// 敌方坦克转换到原来的坐标上
return true;
}
}
}
return false;
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_W:
bU = true;
break;
case KeyEvent.VK_S:
bD = true;
break;
case KeyEvent.VK_A:
bL = true;
break;
case KeyEvent.VK_D:
bR = true;
break;
case KeyEvent.VK_J:
fire();
// new Thread(new Music(Music.PLAY_FIRE)).start();// 开火音效
break;
case KeyEvent.VK_R:
setLife(200);
setLive(true);
break;
}
locateDirection();
}
@Override
public void keyReleased(KeyEvent e) {
int key = e.getKeyCode();
switch (key) {
case KeyEvent.VK_W:
bU = false;
break;
case KeyEvent.VK_S:
bD = false;
break;
case KeyEvent.VK_A:
bL = false;
break;
case KeyEvent.VK_D:
bR = false;
break;
}
locateDirection();
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
子弹类:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.List;
import java.awt.Rectangle;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Missile {
int x,y;//子弹位置
static int hurt = 20;//子弹伤害
static int SPEED =4,NSPEED=4;
static int count =0;//杀死敌人数
static int WIDTH = 5;//子弹的大小
static int HEIGHT = 5;
boolean good;//区分坦克好坏
boolean live = true;//子弹的存活
Tank.Direction kdirection;
public static int getCount(){
return count;
}
public boolean isLive(){
return live;
}
public static void setNspeed(int NSPEED1){
NSPEED=NSPEED1;
}
public static void setHurt(int hurt1){
hurt = hurt1;
}
public Missile(int x,int y,Boolean good,Tank.Direction kdirection){
this.x=x;
this.y=y;
this.good = good;
this.kdirection=kdirection;
}
public void draw(Graphics g) {
if(!live) {
return;
}
else if(good){//根据好坏来设置子弹颜色
g.setColor(Color.white);
g.fillOval(x, y, WIDTH, HEIGHT);
}else{
g.setColor(Color.yellow);
g.fillOval(x, y, WIDTH, HEIGHT);
}
move();//画子弹的时候就要移动
}
void move() {
if(!good){//根据好坏来设置子弹速度
SPEED=NSPEED;
}else{
SPEED=4;
}
switch(kdirection) {//通过方位判断速度
case L:
x -= SPEED;
break;
case LU:
x -= SPEED;
y -= SPEED;
break;
case U:
y -= SPEED;
break;
case RU:
x += SPEED;
y -= SPEED;
break;
case R:
x += SPEED;
break;
case RD:
x += SPEED;
y += SPEED;
break;
case D:
y += SPEED;
break;
case LD:
x -= SPEED;
y += SPEED;
break;
case STOP:
break;
}
//子弹越界了就要死
if(x < 0 || y < 0 || x > 800 || y > 600) {
live = false;
}
}
public Rectangle getRect() {
return new Rectangle(x, y, WIDTH, HEIGHT);
}
public boolean hitTank(Tank t) {
if(this.live && this.getRect().intersects(t.getRect()) && t.isLive() && this.good != t.isGood() ){
if(t.isGood()){//玩家被击中减血
// new Thread(new Music(Music.PLAY_EXPLODE)).start();// 爆炸音效
t.setLife(t.getLife()- hurt);
if(t.getLife()<=0){
t.setLife(0);
t.setLive(false);
}
}else{//敌方直接死
// new Thread(new Music(Music.PLAY_EXPLODE)).start();
t.setLive(false);
count++;
}
this.live = false;
Explode e = new Explode(x, y);
GameFrame.explodes.add(e);
return true;
}
return false;
}
public void hitTanks(java.util.List tanks) {
for(int i=0; i
本项目代码编写极其不规范,后来也没管它了,不知道还跑不跑得起来,如果你执着要看那就附上码云链接:https://gitee.com/xj1988/tank-battle.git