弹球游戏分步解析(四)——保存读取游戏_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 弹球游戏分步解析(四)——保存读取游戏

弹球游戏分步解析(四)——保存读取游戏

 2013/7/22 21:40:22  香煎马鲛鱼  程序员俱乐部  我要评论(0)
  • 摘要:弹球游戏分步解析(四)——保存读取游戏接下来我们要说的是保存读取游戏,弹球游戏作为一个小游戏,加入保存和读取操作似乎有些画蛇添足,不过作为学习,如果我们实现了在弹球游戏中保存,读取,那么以后的很多项目我们都可以使用该操作方法对文件进行保存读取,所以,我们就着这个项目,对这个弹球游戏进行保存吧:首先我们要明白要保存什么:每个小球都有部分属性是独一无二的那些属性必须被存起来:privateintXdirection=West
  • 标签:游戏 解析

class="MsoNormal" style="text-align: center; margin: 0cm 0cm 0pt;">弹球游戏分步解析(四)——保存读取游戏

接下来我们要说的是保存读取游戏,弹球游戏作为一个小游戏,加入保存和读取操作似乎有些画蛇添足,不过作为学习,如果我们实现了在弹球游戏中保存,读取,那么以后的很多项目我们都可以使用该操作方法对文件进行保存读取,所以,我们就着这个项目,对这个弹球游戏进行保存吧:

首先我们要明白要保存什么:

每个小球都有部分属性是独一无二的那些属性必须被存起来:

privateintXdirection = West;//记录小球在X轴上的方向

privateintYdirection = North;//记录小球在y轴上的方向

privateintradios;//半径

private Color color;//颜色

privateintXspeed;//x方向速度

privateintYspeed;//y方向速度

privateintX;//当前位置x

privateintY;//当前位置y

privateintLastX;//最后位置x

privateintLastY;//最后位置y

privateintxband;

privateintyband;

privatebooleanpauseFlag = false;//暂停标志(默认值否)

privatebooleanstopFlag = false;//停止标志(false——表示存在)

privatebooleanberead = false;//是否被储存在文件里过(默认值否)

?

?

一、我们先进行另存为操作:

?

建一个类将另存为封装起来:

文件名savefile

package jumpingBall;

?

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

?

?

import javax.swing.JFileChooser;

import javax.swing.JPanel;

import javax.swing.filechooser.FileNameExtensionFilter;

?

?

publicclass savefile {

??? static String getExtName(String s,char split) {

?????? int i = s.indexOf(split);

?????? int leg = s.length();

??????

?????? return (i>0?(i+1)==leg?" ":s.substring(i,s.length()):" ");

??? }

??? publicvoid save() {

?????? try {//try{}catch(Exception e){}的作用:检测并抛出异常catch

?????????? savefile test = new savefile();//建立一个savefile

?????????? String abc = test.getExtName("abc.txt", '.');//获得文件的格式

?????????? FileOutputStream file_Writer;//输出流

?????????? DataOutputStream out;//

?????????? FileNameExtensionFilter filter = new FileNameExtensionFilter(abc,abc);//过滤器

?????????? JFileChooser jFileChooser = new JFileChooser("F:\\覃翔宇\\软件工程\\Java\\workplace\\Ballmoving\\save");

?????????? jFileChooser.setFileFilter(filter);//添加txt文件的过滤器

?????????? jFileChooser.setDialogTitle("保存文件");//更改对话框按钮的文字

?????????? jFileChooser.setSelectedFile(new File("player"));//默认文件名

?????????? int result = jFileChooser.showSaveDialog(jFileChooser);//

?????????? if(result==jFileChooser.showSaveDialog(jFileChooser)){

????????????? File file = jFileChooser.getSelectedFile();//如果将文件选择器设置为允许选择多个文件,则返回选中文件的列表。

????????????? file_Writer = new FileOutputStream(file); //flie_writer申请空间

????????????? file = new File(file.getPath()+abc);//file申请内存空间

????????????? out = new DataOutputStream(file_Writer );

????????????? ? out.writeInt(Date.allBall.size());//记录要存贮多少个小球的信息

?????????? ??????? for(int i = 0;i<Date.allBall.size();i++){

?????????? ??????? ??? JPanel jp = new JPanel();//

?????????? ??????? ??? BallThread ball = new BallThread(jp);

?????????? ??????? ??? ball = Date.allBall.get(i);//从列表中获取小球

?????????? ??????? ??? out.writeInt(ball.getRadios());//将信息依次存入

?????????? ??????? ??? out.writeInt(ball.getLastX());//

?????????? ??????? ??? out.writeInt(ball.getLastY());

?????????? ??????? ??? out.writeInt(ball.getX());? ??????? ???

?????????? ??????? ??? out.writeInt(ball.getXband());

?????????? ??????? ??? out.writeInt(ball.getXdirection());

?????????? ??????? ??? out.writeInt(ball.getXspeed());

?????????? ??????? ??? out.writeInt(ball.getY());

?????????? ??????? ??? out.writeInt(ball.getYband());

?????????? ??????? ??? out.writeInt(ball.getYdirection());

?????????? ??? ??????? out.writeInt(ball.getYspeed());

?????????? ??????? ??? out.writeInt(ball.getColor().getRGB());

?????????? ??????? ??? out.writeBoolean(ball.savePauseFlag());

?????????? ??????? ??? out.writeBoolean(ball.isStopFlag());

?????????? ??????? ??? out.writeBoolean(ball.saveBeread());

?????????? ??????? ??? //

?????????? ??????? }??

????????????? out.close();//关闭文件

????????????? file_Writer.close();//关闭文件

?????????? }

??????????

?????? } catch (Exception e) {

?????????? // TODO: handle exception

?????? }

??? }

}

二、读取和另存为差不多,不同在于吧输出流改为输入流,将文件中的小球一一读出:

package jumpingBall;

?

import java.awt.Color;

import java.io.DataInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.util.ArrayList;

import java.util.List;

?

?

import javax.swing.JFileChooser;

import javax.swing.JPanel;

import javax.swing.filechooser.FileNameExtensionFilter;

?

publicclass Loadfile {

??? private JPanel jp;

??? public Loadfile(JPanel jp){

?????? this.jp = jp;

??? }

??? privatestatic String getExtName(String s,char split) {

?????? int i = s.indexOf(split);

?????? int leg = s.length();

?

?????? return (i>0?(i+1)==leg?" ":s.substring(i,s.length()):" ");

??? }

??? publicvoid load() {

?????? try {

?????????? String abc = this.getExtName("abc.dat", '.');//

?????????? String abc2 = this.getExtName("abc.txt", '.');

?????????? FileInputStream file_Writer;//

?????????? DataInputStream dis;//

?????????? FileNameExtensionFilter filter = new FileNameExtensionFilter(abc,abc);//添加过滤器

?????????? FileNameExtensionFilter filter2 = new FileNameExtensionFilter(abc2,abc2);//过滤器可以添加多个

?????????? JFileChooser jFileChooser = new JFileChooser("F:\\覃翔宇\\软件工程\\Java\\workplace\\Ballmoving\\save");//

?????????? jFileChooser.setFileFilter(filter);//

?????????? jFileChooser.setFileFilter(filter2);//

?????????? jFileChooser.setDialogTitle("读取文件");//

?????????? int result = jFileChooser.showOpenDialog(jFileChooser);

?????????? if(result==jFileChooser.showOpenDialog(jFileChooser)){

????????????? File file = jFileChooser.getSelectedFile();

????????????? file_Writer = new FileInputStream(file);

????????????? file = new File(file.getPath()+abc);

????????????? dis = new DataInputStream(file_Writer );

????????????? ? int size = dis.readInt();//读取存档中场上小球个数

?????? ??????? ??? List<BallThread> allBall = new ArrayList<BallThread>();

?????? ??????? ???

????????????????? ?System.out.println("场上小球个数变为:"+Date.allBall.size());

?????????? ??????? for(int i = 0;i<size;i++){

?????????? ??????? ??? //注意:读取小球属性的顺序要和存储时一致

?????????? ??????? ??? //依次读取存档中小球的信息

?????????? ??????? ??? BallThread ball = new BallThread(jp);

?????????? ??????? ??? ball.setRadios(dis.readInt());

?????????? ??????? ??? ball.setLastX(dis.readInt());

?????????? ??????? ??? ball.setLastY(dis.readInt());

?????????? ??????? ??? ball.setX(dis.readInt());

?????????? ??????? ??? ball.setXband(dis.readInt());

?????????? ??????? ??? ball.setXdirection(dis.readInt());

?????????? ??????? ??? ball.setXspeed(dis.readInt());

?????????? ??????? ??? ball.setY(dis.readInt());

?????????? ??????? ??? ball.setYband(dis.readInt());

?????????? ??????? ??? ball.setYdirection(dis.readInt());

?????????? ??????? ??? ball.setYspeed(dis.readInt());

?????????? ??????? ??? ball.setColor(new Color(dis.readInt()));

?????????? ??????? ??? ball.setPauseFlag(dis.readBoolean());

?????????? ??????? ??? ball.setStopFlag(dis.readBoolean());

?????????? ??????? ??? ball.setBeread(dis.readBoolean());

?????????? ??????? ??? ball.start();

?????????? ??????? ??? allBall.add(ball);

?????????? ??????? }

?????????? ??????? Date.allBall = allBall;

?????????? ??????? ??? ?dis.close();

?????????? ??????? ??? ?

?????????? ??????? ??? ?file_Writer.close();

??????????

?????? }

?????? }catch(Exception e){

?????????? e.printStackTrace();

?????? }

??? }

}

三、存储和读取类做好后,我们需要做的是分别给他们加上监听器,这里我们使用的监听器是ActionListener

储存文件监听器:

文件名:foListener

package jumpingBall;

?

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileOutputStream;

?

import javax.swing.JFileChooser;

import javax.swing.JLabel;

import javax.swing.JPanel;

?

public class foListener implements ActionListener {

?

???????? @Override

???????? public void actionPerformed(ActionEvent e) {

?????????????????? // TODO Auto-generated method stub

??????????????????

?????????????????? savefile savefile1 = new savefile();

?????????????????? savefile1.save();??????

?????????????????? System.out.println("保存成功");

???????? }

?

}

读取文件监听器:

文件名:ioListener

package jumpingBall;

?

import java.awt.Color;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.util.ArrayList;

import java.util.List;

?

import javax.swing.JFileChooser;

import javax.swing.JLabel;

import javax.swing.JPanel;

?

public class ioListener implements ActionListener{

?

???????? private JPanel jp;

???????? public ioListener(JPanel jp) {

?????????????????? // TODO Auto-generated constructor stub

?????????????????? this.jp = jp;

???????? }

???????? @Override

???????? public void actionPerformed(ActionEvent e) {

?????????????????? // TODO Auto-generated method stub

?????????????????? for(int i = 0;i<Date.allBall.size();i++){//在读取文件之前,先把场上的小球全删光

??????????? System.out.println(Date.allBall.get(i).isStopFlag());

??? ?????????????????? Date.allBall.get(i).exit(true);

??? ???????? }

??????????????????

?????????????????? Date.allBall.clear();

?????????????????? Loadfile laLoadfile = new Loadfile(jp);

?????????????????? laLoadfile.load();

?????????????????? System.out.println("读取成功");

???????? }

}

????????

四、将监听器加在菜单栏里的保存、读取按钮中;

这步我就不多说了,和之前的方法一样,嘿嘿;

?

?

就这样,我们的另存为和读取操作就弄完了,唉,累死了,我先去洗个澡哈,七月的长沙没空调真的可以热死人呐。

发表评论
用户名: 匿名