【重叠判断】【在敌人坦克类新增一个判断重叠的方法,并加入所有的坦克的数据,且在Mypanel传入数据给每个敌人坦克类】
?
?【一个加载音乐的API】
package com.hspedu.tankgame05;
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
/**
* @author 林然
* @version 1.0
*/
public class AePlayWave extends Thread {
private String filename;
public AePlayWave(String wavfile) {
filename = wavfile;
}
public void run() {
File soundFile = new File(filename);
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (Exception e1) {
e1.printStackTrace();
return;
}
AudioFormat format = audioInputStream.getFormat();
SourceDataLine auline = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (Exception e) {
e.printStackTrace();
return;
}
auline.start();
int nBytesRead = 0;
//这是缓冲
byte[] abData = new byte[512];
try {
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0)
auline.write(abData, 0, nBytesRead);
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
auline.drain();
auline.close();
}
}
}
【播放音乐的一个类,我用于让他是一个线程,游戏的全程可播放音乐】
package com.hspedu.tankgame05;
/**
* @author 林然
* @version 1.0
*/
public class bofang implements Runnable{
@Override
public void run() {
while (true){
new AePlayWave("src\\111.wav").start();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
【子弹类】?
package com.hspedu.tankgame05;
/**
* @author 林然
* @version 1.0
* 炸弹
*/
public class Bomb {
int x,y;//炸弹坐标
int life = 9;//炸弹的生命周期
boolean isLife =true;//是否还存活
public Bomb(int x, int y) {
this.x = x;
this.y = y;
}
//减少生命值
public void lifeDown(){
if(life>0){
life--;
}else{
isLife=false;
}
}
}
【敌人坦克类】?
package com.hspedu.tankgame05;
import java.util.Vector;
/**
* @author 韩顺平
* @version 1.0
* 敌人的坦克
*/
public class EnemyTank extends Tank implements Runnable{
Vector <Shot> shots=new Vector<>();
//增加成员,EnemyTank可以得到敌人坦克的Vector
Vector<EnemyTank> enemyTanks =new Vector<>();
//1.<Vector<EnemyTank>
boolean isLive =true;
public EnemyTank(int x, int y) {
super(x, y);
}
//这里提供一个方法可以将MyPanel的成员添加到Vector<EnemyTank> enemyTanks =new Vector<>();
//设置到EnemyTank的成员
public void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
this.enemyTanks = enemyTanks;
}
//编写方法,判断当前的这个坦克是否和enemyTanks中的其他坦克发生的重叠或者碰撞
public boolean isTouchEnemyTank(){
//判断当前坦克的方向
switch (getDirect()){
//让当前敌人坦克和其他所有的敌人坦克比较,不和自己比较
case 0://上
for(int i=0;i<enemyTanks.size();i++){
//从Vector取出一个敌人坦克
EnemyTank enemyTank =enemyTanks.get(i);
if(this!=enemyTank){
//如果敌人坦克是上下
/*
如果敌人坦克是上下,其x和y的坐标范围
1.如果敌人坦克时上下 x=[enemyTank.getX(),enemyTank.getX()+40]
y=[enemyTank.getY(),enemyTank.getY()+60]
*/
if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
//2.当前坦克左上角坐标【this.getX(),this.getY()】
if(this.getX()>=enemyTank.getX()
&&this.getX()<=enemyTank.getX()+40
&&this.getY()>=enemyTank.getY()
&&this.getY()<=enemyTank.getY()+60)
{
return true;
}
//3.当前坦克左上角坐标【this.getX()+40,this.getY()】
if(this.getX()+40>=enemyTank.getX()
&&this.getX()+40<=enemyTank.getX()+40
&&this.getY()>=enemyTank.getY()
&&this.getY()<=enemyTank.getY()+60)
{
return true;
}
}else{
//如果敌人坦克是左右
/*
如果敌人坦克是左右,其x和y的坐标范围
1.如果敌人坦克时上下 x=[enemyTank.getX(),enemyTank.getX()+60]
y=[enemyTank.getY(),enemyTank.getY()+40]
*/
//2.当前坦克左上角坐标【this.getX(),this.getY()】
if(this.getX()>=enemyTank.getX()
&&this.getX()<=enemyTank.getX()+60
&&this.getY()>=enemyTank.getY()
&&this.getY()<=enemyTank.getY()+40)
{
return true;
}
//3.当前坦克左上角坐标【this.getX()+40,this.getY()】
if(this.getX()+40>=enemyTank.getX()
&&this.getX()+40<=enemyTank.getX()+60
&&this.getY()>=enemyTank.getY()
&&this.getY()<=enemyTank.getY()+40)
{
return true;
}
}
}
}
break;
case 1://右
for(int i=0;i<enemyTanks.size();i++){
//从Vector取出一个敌人坦克
EnemyTank enemyTank =enemyTanks.get(i);
if(this!=enemyTank){
//如果敌人坦克是上下
/*
如果敌人坦克是上下,其x和y的坐标范围
1.如果敌人坦克时上下 x=[enemyTank.getX(),enemyTank.getX()+40]
y=[enemyTank.getY(),enemyTank.getY()+60]
*/
if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
//2.当前坦克右上角坐标【this.getX()+60,this.getY()】
if(this.getX() + 60>=enemyTank.getX()
&&this.getX() + 60 <=enemyTank.getX()+40
&&this.getY()>=enemyTank.getY()
&&this.getY()<=enemyTank.getY()+60)
{
return true;
}
//3.当前坦克右下角坐标【this.getX()+60,this.getY()+40】
if(this.getX()+60>=enemyTank.getX()
&&this.getX()+60<=enemyTank.getX()+40
&&this.getY()+40>=enemyTank.getY()
&&this.getY()+40<=enemyTank.getY()+60)
{
return true;
}
}else{
//如果敌人坦克是左右
/*
如果敌人坦克是左右,其x和y的坐标范围
1.如果敌人坦克时上下 x=[enemyTank.getX(),enemyTank.getX()+60]
y=[enemyTank.getY(),enemyTank.getY()+40]
*/
//2.当前坦克右上角坐标【this.getX()+60,this.getY()】
if(this.getX()+60>=enemyTank.getX()
&&this.getX()+60<=enemyTank.getX()+60
&&this.getY()>=enemyTank.getY()
&&this.getY()<=enemyTank.getY()+40)
{
return true;
}
//3.当前坦克右下角坐标【this.getX()+60,this.getY()+40】
if(this.getX()+60>=enemyTank.getX()
&&this.getX()+60<=enemyTank.getX()+60
&&this.getY()+40>=enemyTank.getY()
&&this.getY()+40<=enemyTank.getY()+40)
{
return true;
}
}
}
}
break;
case 2://下
for(int i=0;i<enemyTanks.size();i++){
//从Vector取出一个敌人坦克
EnemyTank enemyTank =enemyTanks.get(i);
if(this!=enemyTank){
//如果敌人坦克是上下
/*
如果敌人坦克是上下,其x和y的坐标范围
1.如果敌人坦克时上下 x=[enemyTank.getX(),enemyTank.getX()+40]
y=[enemyTank.getY(),enemyTank.getY()+60]
*/
if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
//2.当前坦克左下角坐标【this.getX(),this.getY()+60】
if(this.getX() >=enemyTank.getX()
&&this.getX() <=enemyTank.getX()+40
&&this.getY()+60>=enemyTank.getY()
&&this.getY()+60<=enemyTank.getY()+60)
{
return true;
}
//3.当前坦克右下角坐标【this.getX()+40,this.getY()+60】
if(this.getX()+40>=enemyTank.getX()
&&this.getX()+40<=enemyTank.getX()+40
&&this.getY()+60>=enemyTank.getY()
&&this.getY()+60<=enemyTank.getY()+60)
{
return true;
}
}else{
//如果敌人坦克是左右
/*
如果敌人坦克是左右,其x和y的坐标范围
1.如果敌人坦克时上下 x=[enemyTank.getX(),enemyTank.getX()+60]
y=[enemyTank.getY(),enemyTank.getY()+40]
*/
//2.当前坦克左下角坐标【this.getX(),this.getY()+60】
if(this.getX()>=enemyTank.getX()
&&this.getX()<=enemyTank.getX()+60
&&this.getY()+60>=enemyTank.getY()
&&this.getY()+60<=enemyTank.getY()+40)
{
return true;
}
//3.当前坦克右下角坐标【this.getX()+40,this.getY()+60】
if(this.getX()+40>=enemyTank.getX()
&&this.getX()+40<=enemyTank.getX()+60
&&this.getY()+60>=enemyTank.getY()
&&this.getY()+60<=enemyTank.getY()+40)
{
return true;
}
}
}
}
break;
case 3://左
for(int i=0;i<enemyTanks.size();i++){
//从Vector取出一个敌人坦克
EnemyTank enemyTank =enemyTanks.get(i);
if(this!=enemyTank){
//如果敌人坦克是上下
/*
如果敌人坦克是上下,其x和y的坐标范围
1.如果敌人坦克时上下 x=[enemyTank.getX(),enemyTank.getX()+40]
y=[enemyTank.getY(),enemyTank.getY()+60]
*/
if(enemyTank.getDirect()==0||enemyTank.getDirect()==2){
//2.当前坦克左上角坐标【this.getX(),this.getY()】
if(this.getX() >=enemyTank.getX()
&&this.getX() <=enemyTank.getX()+40
&&this.getY()>=enemyTank.getY()
&&this.getY()<=enemyTank.getY()+60)
{
return true;
}
//3.当前坦克左下角坐标【this.getX(),this.getY()+40】
if(this.getX()>=enemyTank.getX()
&&this.getX()<=enemyTank.getX()+40
&&this.getY()+40>=enemyTank.getY()
&&this.getY()+40<=enemyTank.getY()+60)
{
return true;
}
}else{
//如果敌人坦克是左右
/*
如果敌人坦克是左右,其x和y的坐标范围
1.如果敌人坦克时上下 x=[enemyTank.getX(),enemyTank.getX()+60]
y=[enemyTank.getY(),enemyTank.getY()+40]
*/
//2.当前坦克左上角坐标【this.getX(),this.getY()】
if(this.getX()>=enemyTank.getX()
&&this.getX()<=enemyTank.getX()+60
&&this.getY()>=enemyTank.getY()
&&this.getY()<=enemyTank.getY()+40)
{
return true;
}
//3.当前坦克左下角坐标【this.getX(),this.getY()+40】
if(this.getX()>=enemyTank.getX()
&&this.getX()<=enemyTank.getX()+60
&&this.getY()+40>=enemyTank.getY()
&&this.getY()+40<=enemyTank.getY()+40)
{
return true;
}
}
}
}
break;
}
return false;
}
//射击
public void shotHeroTank(){
//创建 Shou 对象,根据当前EnemyTank对象的位置和方向来创建
Shot shot =null;
switch (getDirect()){//得到EnemyTank方向
case 0://向上
shot =new Shot(getX()+20,getY(),0);break;
case 1://向右
shot=new Shot(getX()+60,getY()+20,1);break;
case 2://向下
shot=new Shot(getX()+20,getY()+60,2);break;
case 3://向左
shot=new Shot(getX(),getY()+20,3);break;
}
//加入子弹集合
shots.add(shot);
//启动子弹线程
new Thread(shot).start();
}
@Override
public void run() {
int count=0;
while (isLive){
if(shots.size()<5){
shotHeroTank();
}
count++;
//根据坦克方向来继续移动[前提是没有重叠]
switch (getDirect()){
case 0://向上
if(!isTouchEnemyTank()){
moveUp();
}
break;
case 1://向右
if(!isTouchEnemyTank()){
moveRight();
}
break;
case 2://向下
if(!isTouchEnemyTank()){
moveDown();
}
break;
case 3://向左
if(!isTouchEnemyTank()){
moveLeft();
}
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//然后随机的改变坦克方向[0-4).连续走10步后换方向
if(count%10==0)
{setDirect((int)(Math.random()*4));count=0;}
//写并发程序一定要考虑线程什么时候结束
}
}
}
【我方坦克类】?
package com.hspedu.tankgame05;
import java.util.Vector;
/**
* @author linran
* @version 1.0
* 自己的坦克
*/
public class Hero extends Tank {
//定义一个Shot对象
Shot shot =null;
boolean isLife =true;
//可以发送多颗子弹
Vector<Shot> shots =new Vector<>();
public Hero(int x, int y) {
super(x, y);
}
//射击
public void shotEnemyTank(){
//创建 Shou 对象,根据当前Hero对象的位置和方向来创建
switch (getDirect()){//得到hero方向
case 0://向上
shot =new Shot(getX()+20,getY(),0);break;
case 1://向右
shot=new Shot(getX()+60,getY()+20,1);break;
case 2://向下
shot=new Shot(getX()+20,getY()+60,2);break;
case 3://向左
shot=new Shot(getX(),getY()+20,3);break;
}
//加入子弹集合
shots.add(shot);
//启动子弹线程
new Thread(shot).start();
}
}
【游戏入口】
package com.hspedu.tankgame05;
import javax.swing.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Scanner;
/**
* @author Linran
* @version 1.0
*/
public class HspTankGame05 extends JFrame {
Scanner sc =new Scanner(System.in);
//定义MyPanel
MyPanel mp = null;
public static void main(String[] args) {
HspTankGame05 hspTankGame01 = new HspTankGame05();
}
public HspTankGame05() {
System.out.println("请输入选择1:新游戏 2:继续上局");
String key =sc.next();
mp = new MyPanel(key);
Thread thread =new Thread(mp);
thread.start();
this.add(mp);//把面板(就是游戏的绘图区域)
this.setSize(1400, 750);
this.addKeyListener(mp);//让JFrame 监听mp的键盘事件
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
//在JFrame中增加相应的关闭窗口的处理
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
Recoder.keepRecoder();
System.out.println("监听到关闭窗口");
System.exit(0);
}
});
}
}
【面板类】
package com.hspedu.tankgame05;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.util.Vector;
/**
* @author linran
* @version 1.0
* 坦克大战的绘图区域
*/
//为了监听 键盘事件, 实现KeyListener
//为了让Panel 不停地重绘子弹,需要将MyPanelRunnable,当做一个线程使用
public class MyPanel extends JPanel implements KeyListener,Runnable {
//定义我的坦克
Hero hero = null;
//定义敌人坦克,放入到Vector
Vector<EnemyTank> enemyTanks = new Vector<>();
int enemyTankSize = 3;
//定义一个Vector用于存放炸弹
//当子弹击中坦克时,就加入
Vector<Bomb> bombs =new Vector<>();
//定义三张图片用于显示爆炸效果
Image image1 = null;
Image image2 =null;
Image image3 =null;
//定义一个存放Node的Vector用于恢复敌人坦克坐标和方向
Vector<Node> nodes=null;
public MyPanel(String key) {
//判断记录的文件是否存在
//如果存在,就正常执行,如果不存在,提示只能开新游戏,key=1
File file =new File(Recoder.getRecoderFile());
if(file.exists()){
nodes=Recoder.getNodesAndEnemyTankRec();
}else {
key="1";
System.out.println("无上局记录,只能开启新游戏");
}
hero = new Hero(500, 200);//初始化自己坦克
//初始化敌人坦克
switch (key){
case "1":
Recoder.setAllEnemyNum(0);
Recoder.setHero_life(3);
for (int i = 0; i < enemyTankSize; i++) {
//创建一个敌人的坦克
EnemyTank enemyTank = new EnemyTank((100 * (i + 1)), 0);
enemyTank.setEnemyTanks(enemyTanks);
//设置方向
enemyTank.setDirect(2);
//启动坦克线程,让他动起来
new Thread(enemyTank).start();
//加入一颗子弹
Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
//加入enemyTank的Vector成员进行管理
enemyTank.shots.add(shot);
//启动shot对象
new Thread(shot).start();
//加入
enemyTanks.add(enemyTank);
Recoder.setEnemyTanks(enemyTanks);
}
break;
case "2":
for (int i = 0; i < nodes.size(); i++) {
Node node =nodes.get(i);
//创建一个敌人的坦克
EnemyTank enemyTank = new EnemyTank(node.getX(), node.getY());
enemyTank.setEnemyTanks(enemyTanks);
//设置方向
enemyTank.setDirect(node.getDirect());
//启动坦克线程,让他动起来
new Thread(enemyTank).start();
//加入一颗子弹
Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
//加入enemyTank的Vector成员进行管理
enemyTank.shots.add(shot);
//启动shot对象
new Thread(shot).start();
//加入
enemyTanks.add(enemyTank);
Recoder.setEnemyTanks(enemyTanks);
}
break;
default:System.out.println("你的输入有误");
}
//初始化炸弹图片
image1=Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/bomb_1.gif"));
image2=Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/bomb_2.gif"));
image3=Toolkit.getDefaultToolkit().getImage(MyPanel.class.getResource("/bomb_3.gif"));
bofang bofang = new bofang();
new Thread(bofang).start();
}
//编写方法,显示我方击毁敌方坦克数量
public void showInfo(Graphics g){
//画出玩家总成绩
g.setColor(Color.BLACK);
Font font = new Font("宋体", Font.BOLD, 25);
g.setFont(font);
g.drawString("您累积击毁敌方坦克",1020,30);
drawTank(1020,60,g,0,0);//画出敌方坦克
//这里需要重新设置成黑色,因为在画坦克时,被设置了
g.setColor(Color.BLACK);
g.drawString(Recoder.getAllEnemyNum()+"",1080,100);
g.drawString("您的剩余游戏次数",1020,160);
drawTank(1020,200,g,0,1);//画出敌方坦克
g.setColor(Color.BLACK);
g.drawString(Recoder.getHero_life()+"",1080,240);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.fillRect(0, 0, 1000, 750);//填充矩形,默认黑色
showInfo(g);
//画出自己坦克-封装方法
if(hero.isLife){
drawTank(hero.getX(), hero.getY(), g, hero.getDirect(), 1);
}
//画出hero射击的子弹
for (int i=0;i<hero.shots.size();i++){
Shot shot = hero.shots.get(i);
if(shot!=null&&shot.isLive!=false){
g.draw3DRect(shot.x,shot.y,5,5,false);
}else {//从vector移除
hero.shots.remove(shot);
}
}
for(int i=0;i<bombs.size();i++){
Bomb bomb = bombs.get(i);
//根据当前对象的life值去画图片
if(bomb.life>6){
g.drawImage(image1,bomb.x,bomb.y,60,60,this);
}else if(bomb.life>3){
g.drawImage(image2,bomb.x,bomb.y,60,60,this);
}else {
g.drawImage(image3,bomb.x,bomb.y,60,60,this);
}
//让炸弹生命值减少
bomb.lifeDown();
if(bomb.life==0)
{
bombs.remove(i);//如果生命为0就删除
}
}
//画出敌人的坦克, 遍历Vector
for (int i = 0; i < enemyTanks.size(); i++) {
//取出坦克
EnemyTank enemyTank = enemyTanks.get(i);
if(enemyTank.isLive){//当子弹存活才画出坦克
drawTank(enemyTank.getX(), enemyTank.getY(), g, enemyTank.getDirect(), 0);
//画出enemyTank的所有子弹
for (int j=0;j<enemyTank.shots.size();j++){
//取出子弹
Shot shot = enemyTank.shots.get(j);
if(shot.isLive!=false){
g.draw3DRect(shot.x,shot.y,5,5,false);
}else {
enemyTank.shots.remove(shot);
}
}
}
}
}
//编写方法,画出坦克
/**
* @param x 坦克的左上角x坐标
* @param y 坦克的左上角y坐标
* @param g 画笔
* @param direct 坦克方向(上下左右)
* @param type 坦克类型
*/
public void drawTank(int x, int y, Graphics g, int direct, int type) {
//根据不同类型坦克,设置不同颜色
switch (type) {
case 0: //敌人的坦克
g.setColor(Color.cyan);
break;
case 1: //我的坦克
g.setColor(Color.yellow);
break;
}
//根据坦克方向,来绘制对应形状坦克
//direct 表示方向(0: 向上 1 向右 2 向下 3 向左 )
//
switch (direct) {
case 0: //表示向上
g.fill3DRect(x, y, 10, 60, false);//画出坦克左边轮子
g.fill3DRect(x + 30, y, 10, 60, false);//画出坦克右边轮子
g.fill3DRect(x + 10, y + 10, 20, 40, false);//画出坦克盖子
g.fillOval(x + 10, y + 20, 20, 20);//画出圆形盖子
g.drawLine(x + 20, y + 30, x + 20, y);//画出炮筒
break;
case 1: //表示向右
g.fill3DRect(x, y, 60, 10, false);//画出坦克上边轮子
g.fill3DRect(x, y + 30, 60, 10, false);//画出坦克下边轮子
g.fill3DRect(x + 10, y + 10, 40, 20, false);//画出坦克盖子
g.fillOval(x + 20, y + 10, 20, 20);//画出圆形盖子
g.drawLine(x + 30, y + 20, x + 60, y + 20);//画出炮筒
break;
case 2: //表示向下
g.fill3DRect(x, y, 10, 60, false);//画出坦克左边轮子
g.fill3DRect(x + 30, y, 10, 60, false);//画出坦克右边轮子
g.fill3DRect(x + 10, y + 10, 20, 40, false);//画出坦克盖子
g.fillOval(x + 10, y + 20, 20, 20);//画出圆形盖子
g.drawLine(x + 20, y + 30, x + 20, y + 60);//画出炮筒
break;
case 3: //表示向左
g.fill3DRect(x, y, 60, 10, false);//画出坦克上边轮子
g.fill3DRect(x, y + 30, 60, 10, false);//画出坦克下边轮子
g.fill3DRect(x + 10, y + 10, 40, 20, false);//画出坦克盖子
g.fillOval(x + 20, y + 10, 20, 20);//画出圆形盖子
g.drawLine(x + 30, y + 20, x, y + 20);//画出炮筒
break;
default:
System.out.println("暂时没有处理");
}
}
//判断我方子弹是否击中敌人坦克
public void hittank(Vector<Shot> shots,EnemyTank enemyTank){
//判断s击中坦克
for (int i=0;i<shots.size();i++){
Shot s = shots.get(i);
switch (enemyTank.getDirect()){
case 0://上
case 2://下
if(s.x>enemyTank.getX()&&s.x<enemyTank.getX()+40&&s.y>enemyTank.getY()&&s.y<enemyTank.getY()+60)
{ s.isLive=false;
enemyTank.isLive=false;
//创建Bomb对象加入集合中
Bomb bomb =new Bomb(enemyTank.getX(),enemyTank.getY());
bombs.add(bomb);
enemyTanks.remove(enemyTank);
Recoder.addAllEnemyNum();
}
break;
case 1: //左右
case 3:
if(s.x>enemyTank.getX()&&s.x<enemyTank.getX()+60&&s.y>enemyTank.getY()&&s.y<enemyTank.getY()+40)
{ s.isLive=false;
enemyTank.isLive=false;
//创建Bomb对象加入集合中
Bomb bomb =new Bomb(enemyTank.getX(),enemyTank.getY());
bombs.add(bomb);
enemyTanks.remove(enemyTank);
Recoder.addAllEnemyNum();
}
break;
}
}
}
//判断我方子弹是否击中敌人坦克
public void hitHero(Vector<Shot> shots,Hero hero){
//判断s击中坦克
for (int i=0;i<shots.size();i++){
Shot s = shots.get(i);
if(s.isLive&&hero.isLife){
switch (hero.getDirect()){
case 0://上
case 2://下
if(s.x>hero.getX()&&s.x<hero.getX()+40&&s.y>hero.getY()&&s.y<hero.getY()+60)
{ s.isLive=false;
//创建Bomb对象加入集合中
Bomb bomb =new Bomb(hero.getX(),hero.getY());
bombs.add(bomb);
hero.isLife=false;
Recoder.Minus_Hero_life();
}
break;
case 1: //左右
case 3:
if(s.x>hero.getX()&&s.x<hero.getX()+60&&s.y>hero.getY()&&s.y<hero.getY()+40)
{ s.isLive=false;
hero.isLife=false;
//创建Bomb对象加入集合中
Bomb bomb =new Bomb(hero.getX(),hero.getY());
bombs.add(bomb);
Recoder.Minus_Hero_life();
}
break;
}
}
}
}
@Override
public void keyTyped(KeyEvent e) {
}
//处理wdsa 键按下的情况
@Override
public void keyPressed(KeyEvent e) {
System.out.println(e.getKeyCode());
if (e.getKeyCode() == KeyEvent.VK_W) {//按下W键
//改变坦克的方向
hero.setDirect(0);//
//修改坦克的坐标 y -= 1
hero.moveUp();
} else if (e.getKeyCode() == KeyEvent.VK_D) {//D键, 向右
hero.setDirect(1);
hero.moveRight();
} else if (e.getKeyCode() == KeyEvent.VK_S) {//S键
hero.setDirect(2);
hero.moveDown();
} else if (e.getKeyCode() == KeyEvent.VK_A) {//A键
hero.setDirect(3);
hero.moveLeft();
}
if(e.getKeyCode()==KeyEvent.VK_J){
//if(!(hero.shot!=null&&hero.shot.isLive)) {发射一颗子弹
if(hero.shots.size()<5)
hero.shotEnemyTank();//发射多颗子弹,在面板最多有5g颗子弹
//}
}
//让面板重绘
this.repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void run() {
while (true){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
//判断是否击中坦克
if(hero.shots.size()>0){//当前我的子弹存活
//遍历敌人所有坦克
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank =enemyTanks.get(i);
hittank(hero.shots,enemyTank);
}
}
//判断是否击中我方坦克
for (int i=0;i<enemyTanks.size();i++){
hitHero(enemyTanks.get(i).shots,hero);
}
if(enemyTanks.size()<3){
//创建一个敌人的坦克
int i= (int) (Math.random()*10);
EnemyTank enemyTank = new EnemyTank((100 * (i )), 0);
enemyTank.setEnemyTanks(enemyTanks);
//设置方向
enemyTank.setDirect(2);
//启动坦克线程,让他动起来
new Thread(enemyTank).start();
//加入一颗子弹
Shot shot = new Shot(enemyTank.getX() + 20, enemyTank.getY() + 60, enemyTank.getDirect());
//加入enemyTank的Vector成员进行管理
enemyTank.shots.add(shot);
//启动shot对象
new Thread(shot).start();
//加入
enemyTanks.add(enemyTank);
Recoder.setEnemyTanks(enemyTanks);
}
if(hero.isLife==false&&Recoder.getHero_life()>0){
hero.isLife=true;
}
if(Recoder.getHero_life()<=0){
System.exit(0);
}
this.repaint();
}
}
}
【从文件中读取存储坦克的信息】?
package com.hspedu.tankgame05;
/**
* @author 林然
* @version 1.0
* 一个Node对象,就表示一个敌人坦克信息
*/
public class Node {
private int x;
private int y;
private int direct;
public Node(int x, int y, int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
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 int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
}
【文件的读写操作】
package com.hspedu.tankgame05;
import java.io.*;
import java.util.Vector;
/**
* @author 林然
* @version 1.0
* 用于记录相关的信息,和文件交互
*/
public class Recoder {
//定义变量,我方击毁敌人坦克数量
private static int allEnemyNum =0;
private static int hero_life =3;
public static int getHero_life() {
return hero_life;
}
public static void setHero_life(int hero_life) {
Recoder.hero_life = hero_life;
}
public static void Minus_Hero_life() {
hero_life--;
}
//定义IO对象
private static Vector<EnemyTank> enemyTanks =null;
private static BufferedWriter bw =null;
private static BufferedReader br =null;
private static String recoderFile ="src\\myrecode.txt";
//定义一个Node的集合,用于存储信息
public static Vector<Node> nodes =new Vector<>();
//增加一个方法,用于读取文件,用于恢复信息
public static String getRecoderFile() {
return recoderFile;
}
public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
Recoder.enemyTanks = enemyTanks;
}
//增加一个方法,当游戏退出时,保存到文件中
public static Vector<Node> getNodesAndEnemyTankRec()//该方法在启动位置进行
{
try {
br=new BufferedReader(new FileReader(recoderFile));
allEnemyNum=Integer.parseInt(br.readLine());
hero_life=Integer.parseInt(br.readLine());
//循环读取
String line ="";
while((line=br.readLine())!=null){
String [] s= line.split(" ");
Node node = new Node(Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2]));
nodes.add(node);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br!=null)
{
br.close();
}
} catch (IOException e) {
}
}
return nodes;
}
public static void keepRecoder(){
try {
bw=new BufferedWriter(new FileWriter(recoderFile));
bw.write(allEnemyNum+"");
bw.newLine();
bw.write(hero_life+"");
bw.newLine();
//遍历敌人坦克Vector,然后根据情况保存即可
//OOP,定义一个属性获取敌人坦克
for (int i = 0; i < enemyTanks.size(); i++) {
EnemyTank enemyTank =enemyTanks.get(i);
if(enemyTank.isLive){//建议判断下
//保存信息
String record =enemyTank.getX()+" "+enemyTank.getY()+" "+enemyTank.getDirect();
//写入文件
bw.write(record);
bw.newLine();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bw!=null)
{
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static int getAllEnemyNum() {
return allEnemyNum;
}
public static void setAllEnemyNum(int allEnemyNum) {
Recoder.allEnemyNum = allEnemyNum;
}
public static void addAllEnemyNum(){
allEnemyNum++;
}
}
【射击类】
package com.hspedu.tankgame05;
/**
* @author 林然
* @version 1.0
*/
public class Shot implements Runnable{
//设计子弹
int x;//子弹x坐标
int y;//子弹y坐标
int direction =0;//子弹方向
int speed = 2;//子弹宿舍
boolean isLive = true;//是否存活
//构造器
public Shot(int x, int y, int direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
@Override
public void run() {
while (true){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
//根据方向来改变
switch (direction){
case 0:y-=speed;break;//上
case 1:x+=speed;break;//右
case 2:y+=speed;break;//下
case 3:x-=speed;break;//左
}
System.out.println("子弹x="+x+"子弹y="+y);
//当子弹碰到面板边界时应该被销毁
//当子弹碰到敌人坦克时,也应该结束进程
if(!(x>=0&&x<=1000&&y>=0&&y<=750&&isLive)){
isLive=false;
break;
}
}
}
}
【坦克类】
package com.hspedu.tankgame05;
/**
* @author 韩顺平
* @version 1.0
*/
public class Tank {
private int x;//坦克的横坐标
private int y;//坦克的纵坐标
private int direct = 0;//坦克方向 0 上1 右 2下 3左
private int speed = 5;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
//上右下左移动方法
public void moveUp() {
if(y>0)//判断边界条件,个人感觉在这里设置的话我方和敌人坦克都可以直接控制
{
y -= speed;
}
}
public void moveRight() {
if(x<1000&&(x+80)<1000)
{
x += speed;
}
}
public void moveDown() {
if(y<750&&(y+100)<750)
{
y += speed;
}
}
public void moveLeft() {
if(x>0)
{x -= speed;}
}
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
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;
}
}
记得把相关图片和音频等文件放入对应的文件夹?
链接:https://pan.baidu.com/s/1anOddH3cE47HuAUHyAGiVg?pwd=msmz?
提取码:msmz?
坦克大战