使用开源代码做开源游戏,真香!
喜欢玩俄罗斯方块和MC的玩家都沉默了
源码给大家展示一下
资源免费下载
目录
- src
- bean
- Cell.java
- GLocale.java
- ImageMap.java
- ImagesMap.java
- JSONConfig.java
- JSONsConfig.java
- LastSave.java
- mLocale.java
- Music.java
- MusicJButton.java
- State.java
- TeXiaoMusic.java
- Version.java
- config
- Config.java
- imageConfig.java
- MusicConfig.java
- Main
- ConfigJPanel.java
- Main.java
- ModsJPanel.java
- NewGameJPanel.java
- service
- AllImage.java
- GameJPanelService.java
- JSONService.java
- LastSaveService.java
- SaveService.java
- xingzhuang
- I.java
- J.java
- L.java
- O.java
- S.java
- T.java
- Z.java
- log
- MyThread
- BackFontTH.java
- BackgroundMusic.java
- BooTH.java
- DestroyLineTH.java
- DownOverTH.java
- HeChengTH.java
- QianZou.java
- StartGameTH.java
- StopGameTH.java
- ZiHuiTh.java
- ZIPThread.java
Music 加载音乐文件到内存
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import log.WinLog;
import static cn.zhshipu.config.MusicConfig.*;
public class Music {
private static Music instance = null;
private static List<Map<String, ByteArrayOutputStream>> list_musics = null;
private WinLog log = new WinLog("Music");
private InputStream is;
private ByteArrayOutputStream baout;
public static Music getInstance() {
if(instance == null) {
instance = new Music();
}
return instance;
}
private Music(){
if(list_musics == null) {
list_musics = new ArrayList<Map<String,ByteArrayOutputStream>>();
File files = new File(MusicPath);
File[] music = files.listFiles();
if(music != null) {
for(File f : music) {
log.info("加载音乐文件 = "+f.getName());
try {
is = new FileInputStream(f);
baout = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) > -1) {
baout.write(buffer, 0, len);
}
baout.flush();
} catch (FileNotFoundException e) {
log.error("音乐文件加载错误");
e.printStackTrace();
} catch (IOException e) {
log.error("音乐文件转换错误");
e.printStackTrace();
}finally {
if(baout != null) {
Map<String , ByteArrayOutputStream> map = new HashMap<String, ByteArrayOutputStream>();
map.put(f.getName().substring(0, f.getName().indexOf(".")), baout);
list_musics.add(map);
}
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
public List<Map<String, ByteArrayOutputStream>> getListByteArray(){
return list_musics;
}
}
DownOverTH 方块下落线程
import bean.TeXiaoMusic;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import log.WinLog;
public class DownOverTH extends Thread{
WinLog log = new WinLog("DownOverTH");
@Override
public void run() {
super.run();
Player player = TeXiaoMusic.getInstance().getDownOver();
try {
player.play();
} catch (JavaLayerException e) {
log.error("下落方块播放失败");
log.error(e.getMessage());
}
}
}
BooTH 播放TNT爆炸音效
import bean.TeXiaoMusic;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import log.WinLog;
public class BooTH extends Thread{
WinLog log = new WinLog("BooTH");
@Override
public void run() {
Player player = TeXiaoMusic.getInstance().getBoo();
try {
player.play();
} catch (JavaLayerException e) {
log.error("爆炸播放失败");
log.error(e.getMessage());
}
}
}
DestroyLineTh 播放消除一行时的音效
import bean.TeXiaoMusic;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import log.WinLog;
public class DestroyLineTh extends Thread {
WinLog log = new WinLog("DestroyLineTh");
@Override
public void run() {
super.run();
Player player = TeXiaoMusic.getInstance().getRemoveHang();
try {
player.play();
} catch (JavaLayerException e) {
log.error("消除行播放失败");
log.error(e.getMessage());
}
}
}
HeChengTH 播放方块合成物品的音效
import bean.TeXiaoMusic;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import log.WinLog;
public class HeChengTH extends Thread{
WinLog log = new WinLog("HeChengTH");
@Override
public void run() {
super.run();
Player player = TeXiaoMusic.getInstance().getHeCheng();
try {
player.play();
} catch (JavaLayerException e) {
log.error("合成物品播放失败");
log.error(e.getMessage());
}
}
}
StartGameTH 游戏继续音效
import bean.TeXiaoMusic;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import log.WinLog;
public class StartGameTH extends Thread{
WinLog log = new WinLog("StartGameTH");
@Override
public void run() {
super.run();
Player player = TeXiaoMusic.getInstance().getStartGame();
try {
player.play();
} catch (JavaLayerException e) {
log.error("游戏继续播放失败");
log.error(e.getMessage());
}
}
}
StopGameTH 游戏暂停音效
import bean.TeXiaoMusic;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import log.WinLog;
public class StopGameTH extends Thread {
WinLog log = new WinLog("StopGameTH");
@Override
public void run() {
super.run();
Player player = TeXiaoMusic.getInstance().getStopGame();
try {
player.play();
} catch (JavaLayerException e) {
log.error("游戏暂停播放失败");
log.error(e.getMessage());
}
}
}
ZiHuiTh 叶子掉落物
import java.awt.Image;
import javax.swing.ImageIcon;
import com.alibaba.fastjson.JSONArray;
import bean.Cell;
import bean.ImageMap;
import cn.zhshipu.service.JSONService;
public class ZiHuiTh extends Thread{
private Cell c;
private Cell[][] wall;
public ZiHuiTh(Cell c,Cell[][] wall){
this.c = c;
this.wall = wall;
}
@Override
public void run() {
super.run();
int num = (int) (Math.random() * 10);
Cell[] tsg = new Cell[1];
if(num == 1) {
String name = (int)(Math.random()*2)==0?"mugun":"pingguo";
ImageIcon img = ImageMap.getInstance().getStrToImg().get(name);
img.setImage(img.getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT));
JSONArray type = JSONService.getInstance().getJSONType(ImageMap.getInstance().getImgMapName().get(img));
JSONArray tab = JSONService.getInstance().getJSONTab(ImageMap.getInstance().getImgMapName().get(img));
tsg[0] = new Cell(name,-1,-1,img,type,tab,0);
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
int row = c.getRow();
int col = c.getCol();
System.arraycopy(tsg, 0, wall[row], col, 1);
}
}
I 形状
import javax.swing.ImageIcon;
import com.alibaba.fastjson.JSONArray;
import bean.Cell;
import bean.State;
import cn.zhshipu.service.AllImage;
public class I extends AllImage{
public I(int row,int col,ImageIcon img,JSONArray type,JSONArray tab,String name,int index) {
if(row == -1 || col == -1) {
cells[0] = new Cell(name,0,7, img, type,tab,index);
cells[1] = new Cell(name,0,6, img, type,tab,index);
cells[2] = new Cell(name,0,8, img, type,tab,index);
cells[3] = new Cell(name,0,9, img, type,tab,index);
}else {
cells[0] = new Cell(name,row,col, img, type,tab,index);
cells[1] = new Cell(name,row,col-1, img, type,tab,index);
cells[2] = new Cell(name,row,col+1, img, type,tab,index);
cells[3] = new Cell(name,row,col+2, img, type,tab,index);
}
states =new State[2];
states[0]=new State(0,0,0,-1,0,1,0,2);
states[1]=new State(0,0,-1,0,1,0,2,0);
}
}
J 形状
import javax.swing.ImageIcon;
import com.alibaba.fastjson.JSONArray;
import bean.Cell;
import bean.State;
import cn.zhshipu.service.AllImage;
public class J extends AllImage{
public J(int row,int col,ImageIcon img,JSONArray type,JSONArray tab,String name,int index) {
if(row == -1 || col == -1) {
cells[0] = new Cell(name,0,7, img, type,tab,index);
cells[1] = new Cell(name,0,6, img, type,tab,index);
cells[2] = new Cell(name,0,8, img, type,tab,index);
cells[3] = new Cell(name,1,8, img, type,tab,index);
}else {
cells[0] = new Cell(name,row,col, img, type,tab,index);
cells[1] = new Cell(name,row,col-1, img, type,tab,index);
cells[2] = new Cell(name,row,col+1, img, type,tab,index);
cells[3] = new Cell(name,row+1,col+1, img, type,tab,index);
}
states=new State[4];
states[0]=new State(0,0,0,-1,0,1,1,1);
states[1]=new State(0,0,-1,0,1,0,1,-1);
states[2]=new State(0,0,0,1,0,-1,-1,-1);
states[3]=new State(0,0,1,0,-1,0,-1,1);
}
}
L 形状
import javax.swing.ImageIcon;
import com.alibaba.fastjson.JSONArray;
import bean.Cell;
import bean.State;
import cn.zhshipu.service.AllImage;
public class L extends AllImage{
public L(int row,int col,ImageIcon img,JSONArray type,JSONArray tab,String name,int index) {
if(row == -1 || col == -1) {
cells[0] = new Cell(name,0,7, img, type,tab,index);
cells[1] = new Cell(name,0,6, img, type,tab,index);
cells[2] = new Cell(name,0,8, img, type,tab,index);
cells[3] = new Cell(name,1,6, img, type,tab,index);
}else {
cells[0] = new Cell(name,row,col, img, type,tab,index);
cells[1] = new Cell(name,row,col-1, img, type,tab,index);
cells[2] = new Cell(name,row,col+1, img, type,tab,index);
cells[3] = new Cell(name,row+1,col-1, img, type,tab,index);
}
states=new State[4];
states[0]=new State(0,0,0,-1,0,1,1,-1);
states[1]=new State(0,0,-1,0,1,0,-1,-1);
states[2]=new State(0,0,0,1,0,-1,-1,1);
states[3]=new State(0,0,1,0,-1,0,1,1);
}
}
O 形状
import javax.swing.ImageIcon;
import com.alibaba.fastjson.JSONArray;
import bean.Cell;
import bean.State;
import cn.zhshipu.service.AllImage;
public class O extends AllImage{
public O(int row,int col,ImageIcon img,JSONArray type,JSONArray tab,String name,int index) {
if(row == -1 || col == -1) {
cells[0] = new Cell(name,0, 7, img, type,tab,index);
cells[1] = new Cell(name,0, 8, img, type,tab,index);
cells[2] = new Cell(name,1, 7, img, type,tab,index);
cells[3] = new Cell(name,1, 8, img, type,tab,index);
}else {
cells[0] = new Cell(name,row, col, img, type,tab,index);
cells[1] = new Cell(name,row, col+1, img, type,tab,index);
cells[2] = new Cell(name,row+1, col, img, type,tab,index);
cells[3] = new Cell(name,row+1, col+1, img, type,tab,index);
}
states = new State[0];
}
}
S 形状
import javax.swing.ImageIcon;
import com.alibaba.fastjson.JSONArray;
import bean.Cell;
import bean.State;
import cn.zhshipu.service.AllImage;
public class S extends AllImage{
public S(int row,int col,ImageIcon img,JSONArray type,JSONArray tab,String name,int index) {
if(row == -1 || col == -1) {
cells[0] = new Cell(name,0,7, img, type,tab,index);
cells[1] = new Cell(name,0,8, img, type,tab,index);
cells[2] = new Cell(name,1,6, img, type,tab,index);
cells[3] = new Cell(name,1,7, img, type,tab,index);
}else {
cells[0] = new Cell(name,row,col, img, type,tab,index);
cells[1] = new Cell(name,row,col+1, img, type,tab,index);
cells[2] = new Cell(name,row+1,col-1, img, type,tab,index);
cells[3] = new Cell(name,row+1,col, img, type,tab,index);
}
states =new State[2];
states[0]=new State(0,0,0,1,1,-1,1,0);
states[1]=new State(0,0,1,0,-1,-1,0,-1);
}
}
T 形状
import javax.swing.ImageIcon;
import com.alibaba.fastjson.JSONArray;
import bean.Cell;
import bean.State;
import cn.zhshipu.service.AllImage;
public class T extends AllImage{
public T(int row,int col,ImageIcon img,JSONArray type,JSONArray tab,String name,int index) {
if(row == -1 || col == -1) {
cells[0] = new Cell(name,0,7, img, type,tab,index);
cells[1] = new Cell(name,0,6, img, type,tab,index);
cells[2] = new Cell(name,0,8, img, type,tab,index);
cells[3] = new Cell(name,1,7, img, type,tab,index);
}else {
cells[0] = new Cell(name,row,col, img, type,tab,index);
cells[1] = new Cell(name,row,col-1, img, type,tab,index);
cells[2] = new Cell(name,row,col+1, img, type,tab,index);
cells[3] = new Cell(name,row+1,col, img, type,tab,index);
}
states=new State[4];
states[0]=new State(0,0,0,-1,0,1,1,0);
states[1]=new State(0,0,-1,0,1,0,0,-1);
states[2]=new State(0,0,0,1,0,-1,-1,0);
states[3]=new State(0,0,1,0,-1,0,0,1);
}
}
Z 形状
import javax.swing.ImageIcon;
import com.alibaba.fastjson.JSONArray;
import bean.Cell;
import bean.State;
import cn.zhshipu.service.AllImage;
public class Z extends AllImage{
public Z(int row,int col,ImageIcon img,JSONArray type,JSONArray tab,String name,int index) {
if(row == -1 || col == -1) {
cells[0] = new Cell(name,1,7, img, type,tab,index);
cells[1] = new Cell(name,0,6, img, type,tab,index);
cells[2] = new Cell(name,0,7, img, type,tab,index);
cells[3] = new Cell(name,1,8, img, type,tab,index);
}else {
cells[0] = new Cell(name,row,col, img, type,tab,index);
cells[1] = new Cell(name,row-1,col-1, img, type,tab,index);
cells[2] = new Cell(name,row-1,col, img, type,tab,index);
cells[3] = new Cell(name,row,col+1, img, type,tab,index);
}
states =new State[2];
states[0]=new State(0,0,-1,-1,-1,0,0,1);
states[1]=new State(0,0,-1,1,0,1,1,0);
}
}
JSONService 处理json和图片对应
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import bean.JSONConfig;
import log.WinLog;
public class JSONService {
private static JSONService instance = null;
private Map<String, JSONArray> yuan_json_type;
private Map<String, JSONArray> yuan_json_tab;
private WinLog log = new WinLog("JSONService");
public static JSONService getInstance() {
if(instance == null) {
instance = new JSONService();
}
return instance;
}
private JSONService() {
log.info("加载图片对应的配置");
yuan_json_type = JSONConfig.getInstance().getJSONType();
yuan_json_tab = JSONConfig.getInstance().getJSONTab();
log.info("yuan_json_type = "+yuan_json_type.toString());
log.info("yuan_json_tab = "+yuan_json_tab.toString());
}
public JSONArray getJSONType(String name){
return yuan_json_type.get(name);
}
public JSONArray getJSONTab(String name){
return yuan_json_tab.get(name);
}
}
LastSaveService 加载上一次数据
import java.awt.Image;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.ImageIcon;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import bean.Cell;
import bean.LastSave;
import cn.zhshipu.Main.ModsJPanel;
import cn.zhshipu.service.xingzhuang.I;
import cn.zhshipu.service.xingzhuang.J;
import cn.zhshipu.service.xingzhuang.L;
import cn.zhshipu.service.xingzhuang.O;
import cn.zhshipu.service.xingzhuang.S;
import cn.zhshipu.service.xingzhuang.T;
import cn.zhshipu.service.xingzhuang.Z;
import log.WinLog;
public class LastSaveService {
private static LastSaveService instance = null;
private Cell[][] wall = new Cell[23][15];
private AllImage one = null;
private AllImage nextOne = null;
private Double fenshu = 0.0;
private List<Map<String, Boolean>> list_mods = null;
private WinLog log = new WinLog("LastSaveService");
public static LastSaveService getInstance() {
if(instance == null) {
instance = new LastSaveService();
}
return instance;
}
private LastSaveService() {
log.info("加载上上一次数据");
JSONArray saveCell = LastSave.getInstance().getSaveCell();
JSONArray saveOne = LastSave.getInstance().getSaveOne();
JSONArray saveNextOne = LastSave.getInstance().getSaveNextOne();
JSONObject fenshuObj = LastSave.getInstance().getFenShu();
JSONArray saveMods = LastSave.getInstance().getSaveMods();
wall = cellService(saveCell);
one = oneService(saveOne);
nextOne = NextOneService(saveNextOne);
fenshu = FenShuService(fenshuObj);
list_mods = ListMap(saveMods);
}
private List<Map<String, Boolean>> ListMap(JSONArray saveMods) {
List<Map<String, Boolean>> list = new ArrayList<Map<String,Boolean>>();
Map<Integer, String> old = ModsJPanel.getInstance().getMapName();
for(int i=0;i<saveMods.size();i++) {
JSONObject js = (JSONObject)saveMods.get(i);
Map<String, Boolean> map = new HashMap<String, Boolean>();
map.put(old.get(i), Boolean.parseBoolean(js.get(old.get(i)).toString()));
list.add(map);
}
return list;
}
private Double FenShuService(JSONObject fenshuObj) {
Double s = Double.parseDouble(fenshuObj.getString("fenshu"));
return s;
}
private AllImage NextOneService(JSONArray saveNextOne) {
JSONObject cellObj = (JSONObject)((JSONObject)saveNextOne.get(0)).get("cell");
Cell cell = getCell(cellObj);
AllImage allImage = null;
switch(cell.getIndex()) {
case 0:
allImage = new I(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 1:
allImage = new J(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 2:
allImage = new L(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 3:
allImage = new O(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 4:
allImage = new S(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 5:
allImage = new T(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 6:
allImage = new Z(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
}
return allImage;
}
private AllImage oneService(JSONArray saveOne) {
JSONObject cellObj = (JSONObject)((JSONObject)saveOne.get(0)).get("cell");
Cell cell = getCell(cellObj);
log.info("上一次正在下落的方块 = "+cell);
AllImage allImage = null;
switch(cell.getIndex()) {
case 0:
allImage = new I(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 1:
allImage = new J(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 2:
allImage = new L(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 3:
allImage = new O(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 4:
allImage = new S(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 5:
allImage = new T(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
case 6:
allImage = new Z(cell.getRow(),cell.getCol(),cell.getImg(),cell.getType(),cell.getTab(),cell.getName(),cell.getIndex());
break;
}
return allImage;
}
private Cell getCell(JSONObject cellObj) {
String name = cellObj.getString("name");
int row = Integer.parseInt(cellObj.getString("row"));
int col = Integer.parseInt(cellObj.getString("col"));
ImageIcon img = new ImageIcon("images\\"+cellObj.getString("img")+"\\"+name+".png");
img.setImage(img.getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT));
JSONArray type = cellObj.getJSONArray("type");
JSONArray tab = cellObj.getJSONArray("tab");
int index_s = Integer.parseInt(cellObj.getString("index"));
Cell cell = new Cell(name,row,col,img,type,tab,index_s);
return cell;
}
private Cell[][] cellService(JSONArray saveCell) {
Cell[][] cells = new Cell[23][15];
Map<Integer, JSONObject> map = new HashMap<Integer, JSONObject>();
for(int s =0;s<saveCell.size();s++) {
map.put(s, (JSONObject) saveCell.get(s));
}
int index = 0;
for(int i=0;i<23;i++) {
for(int j=0;j<15;j++) {
String ss = map.get(index).getString("cell");
if("null".equals(ss) || null == ss) {
cells[i][j] = null;
}else {
JSONObject cellObj = map.get(index).getJSONObject("cell");
cells[i][j] = getCell(cellObj);
}
index++;
}
}
return cells;
}
public List<Map<String,Boolean>> getListMods(){
return list_mods;
}
public Cell[][] getWall(){
return wall;
}
public AllImage getOne() {
return one;
}
public AllImage getNextOne() {
return nextOne;
}
public Double getFenShu() {
return fenshu;
}
}
SaveService 加载当前数据,存放到文件中储存
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSONObject;
import bean.Cell;
import cn.zhshipu.Main.NewGameJPanel;
import log.WinLog;
import static cn.zhshipu.config.Config.savePath;
public class SaveService {
private static SaveService instance = null;
private JSONObject zhu_json,dang_json,xia_json,fenshu_json,mods_json;
private WinLog log = new WinLog("SaveService");
private OutputStream os;
public static SaveService getInstance() {
if(instance == null) {
instance = new SaveService();
}
return instance;
}
private SaveService() {
Cell[][] saveCell = NewGameJPanel.saveCell;
String s_saveCell = "\"saveCell\":[";
for(int i=0;i<23;i++) {
for(int j=0;j<15;j++) {
try {
if(null == saveCell[i][j]) {
s_saveCell+="{\"cell\":\"null\"},";
}else {
s_saveCell+="{"+saveCell[i][j].toString()+"},";
}
}catch (Exception e) {
s_saveCell+="{\"cell\":\"null\"},";
}
}
}
log.info("s_saveCell = "+s_saveCell);
s_saveCell = "{"+(s_saveCell.substring(0, s_saveCell.length()-1)+"]}");
zhu_json = (JSONObject)JSONObject.parse(s_saveCell);
AllImage saveOne = NewGameJPanel.saveCurrentOnt;
String saveOne_s = "{\"saveOne\":"+saveOne+"}";
dang_json = (JSONObject)JSONObject.parse(saveOne_s);
AllImage saveNextOne = NewGameJPanel.saveNextOne;
String saveNextOne_s = "{\"saveNextOne\":"+saveNextOne+"}";
xia_json = (JSONObject)JSONObject.parse(saveNextOne_s);
String fenshu_s = "{\"fenshu\":\""+NewGameJPanel.saveFenshu+"\"}";
fenshu_json = (JSONObject)JSONObject.parse(fenshu_s);
List<Map<String,Boolean>> list_mods = NewGameJPanel.save_mods;
String mods_s = "{\"list_mods\":[";
for(int list=0;list<list_mods.size();list++) {
Map<String,Boolean> mods_map = list_mods.get(list);
for(String s : mods_map.keySet()) {
mods_s += "{\""+s+"\":\""+mods_map.get(s)+"\"},";
}
}
mods_s = mods_s.substring(0, mods_s.length()-1) +"]}";
mods_json = (JSONObject)JSONObject.parse(mods_s);
}
public void getSave() {
String map = "{\"data\":["+zhu_json.toJSONString()+","+
dang_json.toJSONString()+","+
xia_json.toJSONString()+","+
fenshu_json.toJSONString()+","+
mods_json.toJSONString()+"]}";
log.info(map);
File f = new File(savePath);
File fp = new File(f.getParent());
if(!fp.exists()) {
try {
fp.createNewFile();
} catch (IOException e) {
log.error("创建父文件夹失败");
log.error(e.getMessage());
}
}
if(f.exists()) {
try {
if(f.delete()) {
f.createNewFile();
try {
os = new FileOutputStream(savePath,true);
byte[] by = map.getBytes();
os.write(by);
} catch (FileNotFoundException e) {
log.error("流连接文件失败");
log.error(e.getMessage());
} catch (IOException e) {
log.error("地图写入文件失败");
log.error(e.getMessage());
}finally {
if(null != os) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}else {
log.info("文件删除与失败");
}
} catch (IOException e) {
log.info("创建地图文件失败");
log.error(e.getMessage());
}
}
}
}
GLocale 鼠标拖动位置
public class GLocale {
private int X;
private int Y;
public GLocale(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;
}
}
ImageMap 加载游戏图片到内存
import static cn.zhshipu.config.imageConfig.*;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
import log.WinLog;
public class ImageMap {
private static ImageMap instance = null;
private Map<Integer, ImageIcon> map = new HashMap<Integer, ImageIcon>();
private Map<ImageIcon, String> map_name = new HashMap<ImageIcon, String>();
private Map<String, ImageIcon> name_map = new HashMap<String, ImageIcon>();
private WinLog log = new WinLog("ImageMap");
public static ImageMap getInstance() {
if(instance == null) {
instance = new ImageMap();
}
return instance;
}
private ImageMap(){
File yuan_img = new File(yuan);
File[] yuan_files = yuan_img.listFiles();
int x = 1;
if(yuan_files != null) {
for(int i=0;i<yuan_files.length;i++) {
String fileName = yuan_files[i].getName();
if(yuan_files[i].isFile()) {
log.info("图片名称 = "+fileName+" , 文件路径 = "+yuan_files[i].getPath() +" , x = "+x);
ImageIcon imgico = new ImageIcon(yuan_files[i].getPath());
map.put(x, imgico);
map_name.put(imgico, fileName.substring(0, fileName.indexOf(".")));
name_map.put(fileName.substring(0, fileName.indexOf(".")), imgico);
x++;
}
}
}
}
public Map<Integer, ImageIcon> getImgMap(){
return map;
}
public void updateImgMap(Map<Integer, ImageIcon> map) {
this.map = map;
}
public Map<ImageIcon, String> getImgMapName(){
return map_name;
}
public void updateImgMapName(Map<ImageIcon, String> map_name) {
this.map_name = map_name;
}
public Map<String, ImageIcon> getStrToImg(){
return name_map;
}
public void updateStrToImg(Map<String, ImageIcon> name_map) {
this.name_map = name_map;
}
}
ImagesMap 加载模组图片到内存
import java.io.File;
import java.util.Map;
import javax.swing.ImageIcon;
import log.WinLog;
import static cn.zhshipu.config.imageConfig.*;
public class ImagesMap {
private Map<Integer, ImageIcon> map = null;
private Map<ImageIcon, String> map_name = null;
private Map<String, ImageIcon> name_map = null;
private WinLog log = new WinLog("ImagesMap");
public ImagesMap(int index) {
map = ImageMap.getInstance().getImgMap();
map_name = ImageMap.getInstance().getImgMapName();
name_map = ImageMap.getInstance().getStrToImg();
int x = map.size()+1;
File f = new File(paths[index]);
if(f.exists()) {
File[] list = f.listFiles();
if(list != null) {
for(int i=0;i<list.length;i++) {
String fileName = list[i].getName();
if(list[i].isFile()) {
log.info("图片名称 = "+fileName+" , 文件路径 = "+list[i].getPath() +" , x = "+x);
ImageIcon imgico = new ImageIcon(list[i].getPath());
map.put(x, imgico);
map_name.put(imgico, fileName.substring(0, fileName.indexOf(".")));
name_map.put(fileName.substring(0, fileName.indexOf(".")), imgico);
x++;
}
}
}
}
}
public void updatee() {
ImageMap.getInstance().updateImgMap(map);
ImageMap.getInstance().updateImgMapName(map_name);
ImageMap.getInstance().updateStrToImg(name_map);
}
}
JSONConfig 获取方块属性和合成表
import static cn.zhshipu.config.imageConfig.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import log.WinLog;
public class JSONConfig {
private static JSONConfig instance = null;
private Map<String, JSONArray> yuan_json_type,yuan_json_tab;
private WinLog log = new WinLog("JSONConfig");
public static JSONConfig getInstance() {
if(instance == null) {
instance = new JSONConfig();
}
return instance;
}
private JSONConfig() {
log.info("加载json配置文件");
yuan_json_type = new HashMap<String, JSONArray>();
yuan_json_tab = new HashMap<String, JSONArray>();
File yuan = new File(yuan_g);
if(yuan.exists()) {
try {
JSONObject jsonobj = (JSONObject)JSONObject.parse(new String(Files.readAllBytes(Paths.get(yuan.getPath()))));
JSONArray arr = jsonobj.getJSONArray("data");
for(int i=0;i<arr.size();i++) {
JSONObject jsonarr = (JSONObject)JSONObject.parse(new String(arr.getString(i).getBytes()));
String name = jsonarr.getString("name");
JSONArray type = jsonarr.getJSONArray("type");
JSONArray tab = jsonarr.getJSONArray("tab");
if(type != null) {
yuan_json_type.put(name, type);
}else {
yuan_json_type.put(name, null);
}
if(tab != null) {
yuan_json_tab.put(name, tab);
}else {
yuan_json_tab.put(name, null);
}
}
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
}
}
}
public void updateTypeJson(Map<String, JSONArray> yuan_json_type ) {
this.yuan_json_type = yuan_json_type;
}
public void updateTabJson(Map<String, JSONArray> yuan_json_type) {
this.yuan_json_type = yuan_json_type;
}
public Map<String, JSONArray> getJSONType(){
return yuan_json_type;
}
public Map<String, JSONArray> getJSONTab(){
return yuan_json_tab;
}
}
LastSave 加载上一次游戏数据到内存
import static cn.zhshipu.config.Config.savePath;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import log.WinLog;
public class LastSave {
private static LastSave instance = null;
private static JSONArray saveCell = null;
private static JSONArray saveOne = null;
private static JSONArray saveNextOne = null;
private static JSONObject fenshu = null;
private static JSONArray saveMods = null;
private WinLog log = new WinLog("LastSave");
private InputStream is;
public static LastSave getInstance() {
if(instance == null) {
instance = new LastSave();
}
return instance;
}
public LastSave() {
File f = new File(savePath);
if(f.exists()) {
try {
is = new FileInputStream(f);
byte b[]=new byte[(int)f.length()];
is.read(b);
JSONObject dataJson = (JSONObject)JSONObject.parse(new String(b));
jiexi(dataJson);
} catch (FileNotFoundException e) {
log.error("读取文件流失败 ---- "+e.getMessage());
e.printStackTrace();
} catch (IOException e) {
log.error("打开流失败 ---- "+e.getMessage());
e.printStackTrace();
}finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
log.error("关闭流失败 ---- "+e.getMessage());
e.printStackTrace();
}
}
}
}
}
private void jiexi(JSONObject dataJson) {
saveCell = (JSONArray)((JSONObject)dataJson.getJSONArray("data").get(0)).get("saveCell");
saveOne = (JSONArray)((JSONObject)dataJson.getJSONArray("data").get(1)).get("saveOne");
saveNextOne = (JSONArray)((JSONObject)dataJson.getJSONArray("data").get(2)).get("saveNextOne");
fenshu = (JSONObject)dataJson.getJSONArray("data").get(3);
saveMods = (JSONArray)((JSONObject)dataJson.getJSONArray("data").get(4)).get("list_mods");
}
public JSONArray getSaveMods() {
return saveMods;
}
public JSONArray getSaveCell() {
return saveCell;
}
public JSONArray getSaveOne() {
return saveOne;
}
public JSONArray getSaveNextOne() {
return saveNextOne;
}
public JSONObject getFenShu() {
return fenshu;
}
}
mLocale 主界面移动
public class mLocale {
private int X;
private int Y;
public mLocale(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;
}
}
State 四方格旋转状态的内部类
public class State {
public int row0, col0, row1, col1, row2, col2, row3, col3;
public State() {
}
public State(int row0, int col0, int row1, int col1, int row2, int col2, int row3, int col3) {
this.row0 = row0;
this.col0 = col0;
this.row1 = row1;
this.col1 = col1;
this.row2 = row2;
this.col2 = col2;
this.row3 = row3;
this.col3 = col3;
}
public int getRow0() {
return row0;
}
public void setRow0(int row0) {
this.row0 = row0;
}
public int getCol0() {
return col0;
}
public void setCol0(int col0) {
this.col0 = col0;
}
public int getRow1() {
return row1;
}
public void setRow1(int row1) {
this.row1 = row1;
}
public int getCol1() {
return col1;
}
public void setCol1(int col1) {
this.col1 = col1;
}
public int getRow2() {
return row2;
}
public void setRow2(int row2) {
this.row2 = row2;
}
public int getCol2() {
return col2;
}
public void setCol2(int col2) {
this.col2 = col2;
}
public int getRow3() {
return row3;
}
public void setRow3(int row3) {
this.row3 = row3;
}
public int getCol3() {
return col3;
}
public void setCol3(int col3) {
this.col3 = col3;
}
@Override
public String toString() {
return "State{" +
"row0=" + row0 +
", col0=" + col0 +
", row1=" + row1 +
", col1=" + col1 +
", row2=" + row2 +
", col2=" + col2 +
", row3=" + row3 +
", col3=" + col3 +
'}';
}
}
Version 获取版本号
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.Paths;
import com.alibaba.fastjson.JSONObject;
import cn.zhshipu.service.GameJPanelService;
import log.WinLog;
import static cn.zhshipu.config.Config.*;
public class Version {
private static Version instance = null;
private String version;
private WinLog log = new WinLog("Version");
public static Version getInstance() {
if(instance == null) {
instance = new Version();
}
return instance;
}
private Version() {
File f1 = new File(versionPath);
if(f1.exists()) {
try {
JSONObject jsonobj = (JSONObject)JSONObject.parse(new String(Files.readAllBytes(Paths.get(f1.getPath()))));
version = jsonobj.getString("version");
log.info("版本号: "+version);
} catch (IOException e) {
log.error("版本文件打开失败");
log.error(e.getMessage());
}
}
new Thread(new Runnable() {
InputStream is = null;
BufferedReader buff = null;
@Override
public void run() {
try {
URL url = new URL(versionURL);
URLConnection uCon = url.openConnection();
uCon.connect();
is = uCon.getInputStream();
buff = new BufferedReader(new InputStreamReader(is));
String s,ss = "";
while ((s = buff.readLine()) != null) {
ss+=s;
}
JSONObject json = (JSONObject)JSONObject.parse(ss);
bijiaoqi(json);
} catch (Exception e) {
log.error("网页数据错误");
log.error(e.getMessage());
}finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
if(buff != null) {
try {
buff.close();
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}
}).start();
}
private void bijiaoqi(JSONObject json) {
if(null != json) {
String v_url = json.getString("version");
if(!"version".equals(v_url)) {
version +=" 有新版本";
GameJPanelService.showVersion(version);
}
}
}
public String getVersion() {
return version;
}
}
没用的知识又增加了,哈哈哈哈哈哈哈哈哈哈!!!
有啥推荐的小游戏各位小伙伴推荐一下
使用javaGUI做一个MC版俄罗斯方块(一)
使用javaGUI做一个MC版俄罗斯方块(二)