`
本来打算在GitHub上分享代码,但是发现有的时候并不需要那么复杂的操作,所以讲自己的原创代码分享给大家,希望可以对同仁的学习得到帮助!
1. 构建坦克大战游戏规则类
“
package com.yifanjia;
import java.io.*;
import java.util.*;
import javax.sound.*;
import javax.sound.sampled.*;
//回复点
class Node {
int x;
int y;
int direct;
public Node(int x, int y, int z) {
this.x = x;
this.y = y;
this.direct = z;
}
}
//播放声音的类
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();
}
}
}
//记录类,同时也可以保存玩家的设置
class Record {
//记录每关有多少敌人
private static int enimynum = 3;
//我有多少可用的坦克
private static int mylife = 3;
//记录总共消灭了多少敌人
private static int alldead = 0;
//从文件中回复记录点
static Vector nodes = new Vector<>();
private static FileWriter fw = null;
private static BufferedWriter bw = null;
private static FileReader fr = null;
private static BufferedReader br = null;
private Vector ets = new Vector<>();
public Vector getNodes() {
try {
fr = new FileReader(“E:\record.txt”);
br = new BufferedReader(fr);
String n = “”;
while((n = br.readLine()) != null) {
String[] xyz = n.split(” “);
Node node = new Node(Integer.parseInt(xyz[0]), Integer.parseInt(xyz[1]), Integer.parseInt(xyz[2]));
nodes.add(node);
}
} catch(IOException e) {
e.printStackTrace();
}
finally {
// TODO: handle finally clause
try {
br.close();
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return nodes;
}
public void setEts(Vector ets) {
this.ets = ets;
}
//记录退出时的信息
public void savegame() {
try {
fw = new FileWriter(“E:\record.txt”);
bw = new BufferedWriter(fw);
//保存敌人的坦克数量、坐标、方向
for(int i=0;i
2. 实现坦克大战的测试类
/**
* 坦克游戏
* 1、画出坦克
* 2、坦克可以上下左右行走
* 3、坦克可以发子弹(最多五颗)
* 4、实敌方坦克的自主移动和坦克被击中死亡
* 5、防止敌人坦克重叠
* 6、可以分关
* 7、可以暂停和继续
* 8、可以记录玩家的成绩
* 9、有音乐
*/
package com.yifanjia;
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
public class MyTankGame extends JFrame implements ActionListener {
MyPanel mp = null;
//定义一个开始面板
MyStartPanel msp = null;
//做出我需要的菜单
JMenuBar jmb = null;
//开始游戏
JMenu jm1 = null;
JMenuItem jmi1 = null;
JMenuItem jmi2 = null;
JMenuItem jmi3 = null;
public static void main(String[] args) {
// TODO Auto-generated method stub
MyTankGame mt = new MyTankGame();
}
public MyTankGame() {
msp = new MyStartPanel();
new Thread(msp).start();
//创建菜单选项
jmb = new JMenuBar();
jm1 = new JMenu(“游戏”);
jmi1 = new JMenuItem(“开始新游戏”);
jmi1.addActionListener(this);
jmi1.setActionCommand(“new game”);
jmi2 = new JMenuItem(“退出并保存游戏”);
jmi2.addActionListener(this);
jmi2.setActionCommand(“exit”);
jmi3 = new JMenuItem(“继续游戏”);
jmi3.addActionListener(this);
jmi3.setActionCommand(“regame”);
jm1.add(jmi1);
jm1.add(jmi2);
jm1.add(jmi3);
jmb.add(jm1);
this.setJMenuBar(jmb);
this.add(msp);
this.setSize(1000,800);
this.setLocation(100,100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
//对用户不同的点击做出不同的处理
if(e.getActionCommand().equals("new game")) {
//创建战场面板
mp = new MyPanel("newGame");
new Thread(mp).start();
//先删除旧的面板
this.remove(msp);
//加入监听
this.addKeyListener(mp);
this.add(mp);
//显示新面板,刷新
this.setVisible(true);
}
else if(e.getActionCommand().equals("exit")) {
//用户点击了退出,保存数据
Record rd = new Record();
rd.setEts(mp.entank);
rd.savegame();
System.exit(0);
}
else if(e.getActionCommand().equals("regame")) {
//创建战场面板
mp = new MyPanel("continue");
new Thread(mp).start();
//先删除旧的面板
this.remove(msp);
//加入监听
this.addKeyListener(mp);
this.add(mp);
//显示新面板,刷新
this.setVisible(true);
}
}
}
//就是一个提示的作用
class MyStartPanel extends JPanel implements Runnable{
int times = 0;
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.black);
g.fillRect(0, 0, 800, 600);
//提示信息
if(times % 2 == 0) {
g.setColor(Color.yellow);
Font myfont = new Font(“楷体”, Font.BOLD, 60);
g.setFont(myfont);
g.drawString(“这是第一关”, 230, 300);
}
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
//休眠1s
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
times++;
this.repaint();
}
}
}
//我的面板
class MyPanel extends JPanel implements KeyListener,Runnable{
//定义一个MyTank
MyTank me = null;
//定义一个炸弹向量
Vector bombs = new Vector<>();
//定义爆炸图片
Image im1 = null;
Image im2 = null;
Image im3 = null;
//定义敌人的坦克
Vector entank = new Vector();
Vector nodes = new Vector<>();
int enimysize = 3;
private Object enimyshot;
public MyPanel(String flag) {
me = new MyTank(100,300);
if(flag.equals(“newGame”)) {
//初始化敌人坦克
for(int i=0;i