首页>代码>java swing实现俄罗斯方块游戏源代码下载>/russia/src/russia/game/square/FileHelpers.java
package russia.game.square;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import com.lyj.string.MyString;


/**
 * 保存游戏的类
 * @author Administrator
 *
 */
public class FileHelpers {
	
	/**
	 * 存方块
	 * 方块数组
	 * @param shapeMap
	 */
	public static final void wirte(int [][] shapeMap,int row,int column){
		try {
			File fl=new File("save/shape.txt");
			if(!fl.exists()){
				return ;
			}
			BufferedWriter bft=new BufferedWriter(new FileWriter(fl));
			  for(int i=0;i<row;i++){
			    	for(int j=0;j<column;j++){
			    		bft.append(shapeMap[i][j]+"");
			    	}
			    	bft.append("\n");
			    }
			  bft.flush();
			  bft.close();
		} catch (Exception e) {
		}
		
		
	}
	
	/**
	 * 写方块
	 * @return
	 * 方块数组
	 */
	public static final int [][]read(int row,int column) {
      		int [][]shapeMap=new int[row][column];
      		try {
      			
      			File fl=new File("save/shape.txt");
      			if(!fl.exists()){
      				fl.mkdir();
      			}
      		   BufferedReader bfd=new BufferedReader(new FileReader(fl));
      		   String read=null;
      		   int index=0;
      		   while((read=bfd.readLine())!=null){
      			  char[]ary= read.toCharArray();
      			   for(int i=0,size=ary.length;i<size;i++){
      				   char ch=ary[i];
      				   shapeMap[index][i]=Integer.parseInt(ch+"");
      			   }
      			   index++;
      		   }
      		   bfd.close();
      		}
      		catch (Exception e) {
      			System.out.println(e.getCause());
			}
		return shapeMap;
	}
	
	
	/**
	 * 存颜色
	 * @param map
	 */
	public static final void write(Map<Points, Color> map,int score){
		try {
			File fl=new File("save/color.txt");
			if(!fl.exists()){
				return ;
			}
			BufferedWriter bft=new BufferedWriter(new FileWriter(fl));
			Iterator<Entry<Points, Color>>  it=map.entrySet().iterator(); 
			for (; it.hasNext();) {
				Entry<Points, Color> kv = it.next();
				bft.append(kv.getKey().getX()+"=");
				bft.append(kv.getKey().getY()+"");
				bft.append("&"+kv.getValue().getRed());
				bft.append("="+kv.getValue().getGreen());
				bft.append("="+kv.getValue().getBlue()+"");
				bft.append("\n");
			}
			//存入游戏得分
			    bft.append("score="+score+"\n");
		     
			  bft.flush();
			  bft.close();
		} catch (Exception e) {
		}
	}
	/**
	 * 读颜色
	 * @return
	 */
	public static final Map<Points, Color> readColor(){
		 Map<Points, Color>  map=new HashMap<Points, Color>();
		 try {
   			
   			File fl=new File("save/color.txt");
   			if(!fl.exists()){
   				fl.mkdir();
   			}
   		   BufferedReader bfd=new BufferedReader(new FileReader(fl));
   		   String read=null;
   		   while((read=bfd.readLine())!=null){
   			   if(!read.startsWith("score")){
   			   String []all=MyString.split(read, '&');
   			   String[] first=MyString.split(all[0], '=');
   			   String[] sec=MyString.split(all[1], '=');
   			   for(int i=0;i<first.length;i++){
   				   int x=Integer.parseInt(first[0]);
   				   int y=Integer.parseInt(first[1]);
   				   int red=Integer.parseInt(sec[0]);
   				   int green=Integer.parseInt(sec[1]);
   				   int blue=Integer.parseInt(sec[2]);
   				   Points p=new Points(x,y);
   				   map.put(p, new Color(red,green,blue));
   				   
   			   }
   			   }
   			   else
   			   scores=new Integer(MyString.split(read, '=')[1]);
   			   
   		   }
   		   bfd.close();
   		}
   		catch (Exception e) {
			}
		 return map;
		
	}
	
	private  static int scores=0;
	public static int getScores(){
		
		return scores;
	}
	

}
最近下载更多
solotoo  LV2 2021年11月13日
heiqiang198603  LV1 2021年9月18日
睡前亲口月亮  LV1 2020年10月2日
小学期gogo  LV2 2020年7月6日
chen519168  LV9 2020年6月1日
GG思密达da  LV9 2020年1月15日
爱心12345  LV1 2020年1月9日
我就笑笑  LV12 2019年12月16日
矿泉水  LV30 2019年8月26日
sagdsshhs  LV2 2019年6月26日
最近浏览更多
微信网友_6775263012638720  LV1 2023年12月12日
微信网友_6699076084797440  LV7 2023年10月30日
小安同学  LV7 2023年5月11日
微信网友_6268131861106688  LV5 2022年12月21日
3272047233  LV1 2022年12月14日
balabalawuyu  LV6 2022年11月26日
迷迭香  LV10 2022年9月21日
tree1112666  LV1 2022年8月19日
siriuswolf  LV1 2022年6月23日
冰封111111  LV1 2022年6月23日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友