使用javaGUI做一个MC版俄罗斯方块(一)

使用开源代码做开源游戏,真香!

喜欢玩俄罗斯方块和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
      • WinLog.java
    • MyThread
      • BackFontTH.java
      • BackgroundMusic.java
      • BooTH.java
      • DestroyLineTH.java
      • DownOverTH.java
      • HeChengTH.java
      • QianZou.java
      • StartGameTH.java
      • StopGameTH.java
      • ZiHuiTh.java
      • ZIPThread.java
        界面图
        效果图

Main 主界面

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.ccqq.getDPI.zhuoMianDPI;
import MyThread.QianZou;
import bean.mLocale;
import log.WinLog;
import static cn.zhshipu.config.Config.*;

public class Main extends Frame{
	
	/**
	 * @author CQ
	 */
	private static final long serialVersionUID = 1L;

	private mLocale ml = new mLocale(zhuoMianDPI.winWidth()/2-mW/2, zhuoMianDPI.winHeight()/2-mH/2);
	
	private JLabel jl_bgt;
	private JPanel jp_login;
	private JButton jb_close,jb_xiao,jb_newGame,jb_oldGame,jb_Mods,jb_Config;
	private NewGameJPanel ngj = new NewGameJPanel();	//加载新游戏控件
	private static WinLog log = new WinLog("Main");
	private static QianZou qz = new QianZou();
	private static int x,y;
	
	Main(){
		this.setTitle("MC俄罗斯方块");
		this.setLayout(null);
		this.setBounds(ml.getX(), ml.getY(), mW, mH);
		this.setUndecorated(true);
		this.setBackground(new Color(0,0,0,0));
		this.setIconImage(logo.getImage());
		
		jl_bgt = new JLabel(bgt);
		bgt.setImage(bgt.getImage().getScaledInstance(bgt.getIconWidth()*2/3,bgt.getIconHeight()*2/3,Image.SCALE_DEFAULT));
		jl_bgt.setBounds(this.getWidth()/2-bgt.getIconWidth()/2, 0, bgt.getIconWidth(), bgt.getIconHeight());
		this.add(jl_bgt);
		
		jp_login = new JPanel();
		jp_login.setLayout(null);
		jp_login.setBounds(0, jl_bgt.getHeight()-150, this.getWidth(), this.getHeight()-jl_bgt.getHeight()+150);
		jp_login.setBackground(new Color(153, 217, 234));
		this.add(jp_login);
		
		jb_close = new JButton("X");
		jb_close.setFont(f40);
		jb_close.setBounds(this.getWidth()-50, jl_bgt.getHeight()-200, 50, 50);
		jb_close.setBorder(BorderFactory.createLineBorder(new Color(0,0,0,0)));
		jb_close.setBackground(new Color(0,0,0,0));
		this.add(jb_close);
		
		jb_xiao = new JButton("-");
		jb_xiao.setFont(f40);
		jb_xiao.setBounds(this.getWidth()-100, jl_bgt.getHeight()-200, 50, 50);
		jb_xiao.setBorder(BorderFactory.createLineBorder(new Color(0,0,0,0)));
		jb_xiao.setBackground(new Color(0,0,0,0));
		this.add(jb_xiao);
		
		InitJPanel();	//初始化控件
		FontJPanel();	//设置控件字体
		BackGroundJPanel();	//去掉控件背景
		BoundsJPanel(jp_login);	//设置控件位置
		AddJPanel(jp_login);	//实现控件
		MouseListendsJPanel();	//控件监听
		
		jp_login.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				super.mousePressed(e);
				x=0;
				y=0;
				x = e.getX();
				y = e.getY();
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				super.mouseReleased(e);
				int p = e.getX() - x;
				int q = e.getY() - y;
				ml.setX(ml.getX()+p);
				ml.setY(ml.getY()+q);
				Main.this.setLocation(ml.getX(), ml.getY());
			}
		});
		
		jl_bgt.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				super.mousePressed(e);
				x=0;
				y=0;
				x = e.getX();
				y = e.getY();
			}
			@Override
			public void mouseReleased(MouseEvent e) {
				super.mouseReleased(e);
				int p = e.getX() - x;
				int q = e.getY() - y;
				ml.setX(ml.getX()+p);
				ml.setY(ml.getY()+q);
				Main.this.setLocation(ml.getX(), ml.getY());
			}
		});
		
		jb_xiao.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				Main.this.setExtendedState(Frame.ICONIFIED);	//最小化
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				jb_xiao.setCursor(new Cursor(Cursor.HAND_CURSOR));
			}
		});
		
		jb_close.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
			@Override
			public void mouseEntered(MouseEvent e) {
				jb_close.setCursor(new Cursor(Cursor.HAND_CURSOR));
			}
		});

		//开场加载完毕,播放前奏
		qz.start();
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				System.exit(0);
			}
		});
		this.setVisible(true);
	}

	private void MouseListendsJPanel() {
		jb_newGame.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				log.info("新游戏");
				qz.Close();
				ngj.showNewGame(Main.this);
			}
		});
		
		jb_oldGame.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				log.info("上一次游戏");
				qz.Close();
				ngj.updateNewGame(Main.this);
			}
		});
		
		jb_Mods.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				log.info("模组界面");
				ModsJPanel.getInstance().showMods(Main.this);
			}
		});
		
		jb_Config.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				log.info("设置界面");
				ConfigJPanel.getInstance().showConfigJPanel(Main.this);
			}
		});
	}

	private void BackGroundJPanel() {
		jb_newGame.setBackground(new Color(0,0,0,0));
		jb_oldGame.setBackground(new Color(0,0,0,0));
		jb_Mods.setBackground(new Color(0,0,0,0));
		jb_Config.setBackground(new Color(0,0,0,0));
	}

	private void AddJPanel(JPanel jp) {
		jp.add(jb_newGame);
		jp.add(jb_oldGame);
		jp.add(jb_Mods);
		jp.add(jb_Config);
	}

	private void BoundsJPanel(JPanel jp) {
		jb_newGame.setBounds(jp.getWidth()/2-150, 200, 300, 50);
		jb_oldGame.setBounds(jp.getWidth()/2-150, 260, 300, 50);
		jb_Mods.setBounds(jp.getWidth()/2-150, 320, 300, 50);
		jb_Config.setBounds(jp.getWidth()/2-150, 380, 300, 50);
	}

	private void FontJPanel() {
		jb_newGame.setFont(f30);
		jb_oldGame.setFont(f30);
		jb_Mods.setFont(f30);
		jb_Config.setFont(f30);
	}

	private void InitJPanel() {
		jb_newGame = new JButton("新游戏");
		jb_oldGame = new JButton("继续上一次游戏");
		jb_Mods = new JButton("模组");
		jb_Config = new JButton("设置");
	}

	public static void main(String[] args) {
		log.info("加载屏幕大小 width = "+zhuoMianDPI.winWidth()+" , height = "+zhuoMianDPI.winHeight());
		new Main();
	}

WinLog 日志

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import MyThread.ZIPThread;

public class WinLog {
	public static final String logPath = "log\\";
	private String name;
	public boolean logFlag = true;	//日志开关
	private static final int size = 10 * 1024 * 1024;	// 10M
	
	public WinLog(String name){
		this.name = name;
		File f = new File(logPath);
		if(!f.exists()) {
			f.mkdir();
		}
	}
	
	public boolean isLogFlag() {
		return logFlag;
	}
	
	public void setLogFlag(boolean logFlag) {
		this.logFlag = logFlag;
	}
	
	//过程日志
	public void info(String str) {
		if(logFlag) {
			File f = new File(logPath+"info.txt");
			if(!f.exists()) {
				try {
					f.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			OutputStream os = null;
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				os = new FileOutputStream(f.getPath(),true);
				str = df.format(new Date())+"    ----    "+name+"    ----    "+str+"\r\n";
				System.out.println(str);
				byte[] by = str.getBytes();
				os.write(by);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				try {
					if(os != null) {
						os.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				long len = f.length();
				if(len >= size) {
					System.out.println("len = "+len);
					int m=1;
					List<File> fileList = new ArrayList<>();
					fileList.add(new File(f.getPath()));
					SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
					Date date = new Date(System.currentTimeMillis());
					String zipPath = "";
					while(true) {
						zipPath = f.getParent()+"/"+f.getName().substring(0, f.getName().indexOf("."))+formatter.format(date)+"-"+m+".zip";
						File ss = new File(zipPath);
						if(ss.exists()) {
							m++;
						}else {
							FileOutputStream fos;
							try {
								fos = new FileOutputStream(new File(zipPath));
								toZip(fileList,fos);
								f.delete();
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
							return;
						}
					}
				}
			}
		}
	}
	
	//报错日志
	public void error(String str) {
		if(logFlag) {
			File f = new File(logPath+"error.txt");
			if(!f.exists()) {
				try {
					f.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			OutputStream os = null;
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				os = new FileOutputStream(f.getPath(),true);
				str = df.format(new Date())+"    ----    "+name+"    ----    "+str+"\r\n";
				System.out.println(str);
				byte[] by = str.getBytes();
				os.write(by);
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}finally {
				try {
					if(os != null) {
						os.close();
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
				long len = f.length();
				if(len >= size) {
					System.out.println("len = "+len);
					int m=1;
					List<File> fileList = new ArrayList<>();
					fileList.add(new File(f.getPath()));
					SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd");
					Date date = new Date(System.currentTimeMillis());
					String zipPath = "";
					while(true) {
						zipPath = f.getParent()+"/"+f.getName().substring(0, f.getName().indexOf("."))+formatter.format(date)+"-"+m+".zip";
						File ss = new File(zipPath);
						if(ss.exists()) {
							m++;
						}else {
							FileOutputStream fos;
							try {
								fos = new FileOutputStream(new File(zipPath));
								toZip(fileList,fos);
								f.delete();
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
							return;
						}
					}
				}
			}
		}
	}
		
	/**
	 * 压缩成ZIP
	 * @param srcFiles 需要压缩的文件列表
	 * @param out 压缩文件输出流
	 * @throws RuntimeException 压缩失败会抛出运行时异常
	*/
	public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
	    long start = System.currentTimeMillis();
	    ZipOutputStream zos = null;
	    try {
	        zos = new ZipOutputStream(out);
	        for (File srcFile : srcFiles) {
	            byte[] buf = new byte[size];
	            zos.putNextEntry(new ZipEntry(srcFile.getName()));
	            int len;
	            FileInputStream in = new FileInputStream(srcFile);
	            while ((len = in.read(buf)) != -1) {
	                zos.write(buf, 0, len);
	            }
	            zos.closeEntry();
	            in.close();
	        }
	        long end = System.currentTimeMillis();
	        System.out.println("压缩完成,耗时:" + (end - start) + " ms");
	    } catch (Exception e) {
	        throw new RuntimeException("zip error from ZipUtils", e);
	    } finally {
	        if (zos != null) {
	            try {
	                zos.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	        new ZIPThread(logPath).start();
	    }
	}

ZIPThread 日志压缩并处理

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 	检测日志压缩包
 * 	同类型超过9个多余的删除
 * 
 * */
public class ZIPThread extends Thread{
	private String path;
	private Map<Integer, File> map_file = null,map_err_file = null;
	public ZIPThread(String path) {
		this.path = path;
	}
	
	@Override
	public void run() {
		super.run();
		File parte = new File(path);
		File[] listFile = parte.listFiles();
		List<File> listZipFile = new ArrayList<File>();
		List<File> listErrFile = new ArrayList<File>();
		if(listFile!=null) {
			for(int i=0;i<listFile.length;i++) {
				String name = listFile[i].getName();
				if(name.indexOf("info")!=-1 && name.indexOf("zip")!=-1) {
					listZipFile.add(listFile[i]);
				}
				if(name.indexOf("error")!=-1 && name.indexOf("zip")!=-1) {
					listErrFile.add(listFile[i]);
				}
			}
		}
		
		if(listZipFile.size() > 8) {
			map_file = new HashMap<Integer, File>();
			for(File f : listZipFile) {
				int n = Integer.parseInt(f.getName().substring(f.getName().lastIndexOf("-")+1, f.getName().indexOf(".")));
				map_file.put(n, f);
			}
			//重新排序
			for(Map.Entry<Integer, File> entry:map_file.entrySet()){
				if(entry.getKey() > 7) {
					File ff = entry.getValue();
					ff.delete();
				}
		    }
		}
		
		if(listErrFile.size() > 8) {
			map_err_file = new HashMap<Integer, File>();
			for(File f : listErrFile) {
				int n = Integer.parseInt(f.getName().substring(f.getName().lastIndexOf("-")+1, f.getName().indexOf(".")));
				map_err_file.put(n, f);
			}
			//重新排序
			for(Map.Entry<Integer, File> entry:map_err_file.entrySet()){
				if(entry.getKey() > 7) {
					File ff = entry.getValue();
					ff.delete();
				}
		    }
		}
	}
}

Config 主配置文件

import java.awt.Font;

import javax.swing.ImageIcon;

public class Config {
	//版本号
	public static final String versionPath = "config\\version.json";
//	获取版本号地址
	public static final String versionURL = "http://localhost:9898";	//根据自己的服务器,不会的@作者帮忙弄一下简单的socket就行
	
	public static final int mW = 600;
	public static final int mH = 800;
	public static final int W = 800;
	public static final int H = 1000;
	public static final int gX = 2;
	public static final int gY = 77;
	public static final int gW = 525;
	public static final int gH = 805;
	public static final int tX = 660;
	public static final int tY = 250;
	public static final int zX = 650;
	public static final int zY = 455;
	public static final int fk = 35;
	
	public static final int music_size = 20;
	
	public static ImageIcon logo = new ImageIcon("images\\lo.png");
	public static ImageIcon bgk = new ImageIcon("images\\bgk.png");
	public static ImageIcon bgt = new ImageIcon("images\\bgt.png");
	
	public static final Font f15 = new Font("楷体",Font.BOLD,15);
	public static final Font f20 = new Font("楷体",Font.BOLD,20);
	public static final Font f25 = new Font("楷体",Font.BOLD,25);
	public static final Font f30 = new Font("楷体",Font.BOLD,30);
	public static final Font f35 = new Font("楷体",Font.BOLD,35);
	public static final Font f40 = new Font("楷体",Font.BOLD,40);
	
	//日志路径
	public static final String logPath = "logs\\log.txt";
	//日志开关
	public static final boolean logFlag = true;
	//保存游戏文件路径
	public static final String savePath = "save\\map.cq";
}

QianZou 打开游戏的前奏音乐线程

import bean.TeXiaoMusic;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import log.WinLog;

public class QianZou extends Thread{
	Player player = TeXiaoMusic.getInstance().getBeforeGame();
	WinLog log = new WinLog("QianZou");
	@Override
	public void run() {
		super.run();
		try {
			Thread.sleep(400);
			player.play();
		} catch (JavaLayerException | InterruptedException e) {
			e.printStackTrace();
		}
	}
	public void Close() {
		try {
			player.close();
			this.join();
		} catch (InterruptedException e) {
			log.error("前奏线程销毁失败");
			log.error(e.getMessage());
		}
	}
}

TeXiaoMusic 加载特效音乐到内存

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
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 javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;
import log.WinLog;
import static cn.zhshipu.config.MusicConfig.*;

public class TeXiaoMusic {
	private static TeXiaoMusic instance = null;
	private WinLog log = new WinLog("TeXiaoMusic");
			
	private static ByteArrayOutputStream boo_out = null;	//爆炸
	private static ByteArrayOutputStream beforeGame_out = null;	//游戏前奏
	private static ByteArrayOutputStream removeHang_out = null;	//消除行
	private static ByteArrayOutputStream downOver_out = null;	//快放落到最下面
	private static ByteArrayOutputStream hecheng_out = null;	//合成物品音效
	private static ByteArrayOutputStream stopGame_out = null;	//游戏暂停
	private static ByteArrayOutputStream startGame_out = null;	//游戏继续
	
	private InputStream boo_is;
	private InputStream beforeGame_is;
	private InputStream removeHang_is;
	private InputStream downOver_is;
	private InputStream hecheng_is;
	private InputStream stopGame_is;
	private InputStream startGame_is;
	
	public static TeXiaoMusic getInstance() {
		if(instance == null) {
			instance = new TeXiaoMusic();
		}
		return instance;
	}
	TeXiaoMusic() {
		if(stopGame_out == null) {
			File removeHang_f = new File(stopGame_path);
			if(removeHang_f.exists()) {
				try {
					stopGame_is = new FileInputStream(removeHang_f);
					stopGame_out = new ByteArrayOutputStream();
					byte[] buffer = new byte[1024];
					int len;
					while ((len = stopGame_is.read(buffer)) > -1) {
						stopGame_out.write(buffer, 0, len);
					}
					stopGame_out.flush();
				} catch (FileNotFoundException e) {
					log.error("游戏暂停文件打开失败    ----    "+e.getMessage());
				} catch (IOException e) {
					log.error(e.getMessage());
				}finally {
					if(stopGame_is !=null) {
						try {
							stopGame_is.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
				
			}
		}
		if(startGame_out == null) {
			File removeHang_f = new File(startGame_path);
			if(removeHang_f.exists()) {
				try {
					startGame_is = new FileInputStream(removeHang_f);
					startGame_out = new ByteArrayOutputStream();
					byte[] buffer = new byte[1024];
					int len;
					while ((len = startGame_is.read(buffer)) > -1) {
						startGame_out.write(buffer, 0, len);
					}
					startGame_out.flush();
				} catch (FileNotFoundException e) {
					log.error("游戏继续文件打开失败    ----    "+e.getMessage());
				} catch (IOException e) {
					log.error(e.getMessage());
				}finally {
					if(startGame_is !=null) {
						try {
							startGame_is.close();
						} catch (IOException e) {
							log.error(e.getMessage());
						}
					}
				}
				
			}
		}
		if(hecheng_out == null) {
			File removeHang_f = new File(hecheng_path);
			if(removeHang_f.exists()) {
				try {
					hecheng_is = new FileInputStream(removeHang_f);
					hecheng_out = new ByteArrayOutputStream();
					byte[] buffer = new byte[1024];
					int len;
					while ((len = hecheng_is.read(buffer)) > -1) {
						hecheng_out.write(buffer, 0, len);
					}
					hecheng_out.flush();
				} catch (FileNotFoundException e) {
					log.error("物品合成文件打开失败    ----    "+e.getMessage());
				} catch (IOException e) {
					log.error(e.getMessage());
				}finally {
					if(hecheng_is !=null) {
						try {
							hecheng_is.close();
						} catch (IOException e) {
							log.error(e.getMessage());
						}
					}
				}
				
			}
		}
		if(downOver_out == null) {
			File removeHang_f = new File(downOver_path);
			if(removeHang_f.exists()) {
				try {
					downOver_is = new FileInputStream(removeHang_f);
					downOver_out = new ByteArrayOutputStream();
					byte[] buffer = new byte[1024];
					int len;
					while ((len = downOver_is.read(buffer)) > -1) {
						downOver_out.write(buffer, 0, len);
					}
					downOver_out.flush();
				} catch (FileNotFoundException e) {
					log.error("方块落下文件打开失败    ----    "+e.getMessage());
				} catch (IOException e) {
					log.error(e.getMessage());
				}finally {
					if(downOver_is !=null) {
						try {
							downOver_is.close();
						} catch (IOException e) {
							log.error(e.getMessage());
						}
					}
				}
				
			}
		}
		if(removeHang_out == null) {
			File removeHang_f = new File(removeHang_path);
			if(removeHang_f.exists()) {
				try {
					removeHang_is = new FileInputStream(removeHang_f);
					removeHang_out = new ByteArrayOutputStream();
					byte[] buffer = new byte[1024];
					int len;
					while ((len = removeHang_is.read(buffer)) > -1) {
						removeHang_out.write(buffer, 0, len);
					}
					removeHang_out.flush();
				} catch (FileNotFoundException e) {
					log.error("游戏前奏文件打开失败    ----    "+e.getMessage());
				} catch (IOException e) {
					log.error(e.getMessage());
				}finally {
					if(removeHang_is !=null) {
						try {
							removeHang_is.close();
						} catch (IOException e) {
							log.error(e.getMessage());
						}
					}
				}
				
			}
		}
		if(beforeGame_out == null) {
			File beforeGame_f = new File(beforeGame_path);
			if(beforeGame_f.exists()) {
				try {
					beforeGame_is = new FileInputStream(beforeGame_f);
					beforeGame_out = new ByteArrayOutputStream();
					byte[] buffer = new byte[1024];
					int len;
					while ((len = beforeGame_is.read(buffer)) > -1) {
						beforeGame_out.write(buffer, 0, len);
					}
					beforeGame_out.flush();
				} catch (FileNotFoundException e) {
					log.error("游戏前奏文件打开失败    ----    "+e.getMessage());
				} catch (IOException e) {
					log.error(e.getMessage());
				}finally {
					if(beforeGame_is !=null) {
						try {
							beforeGame_is.close();
						} catch (IOException e) {
							log.error(e.getMessage());
						}
					}
				}
				
			}
		}
		if(boo_out == null) {
			File boo_f = new File(boo_path);
			if(boo_f.exists()) {
				try {
					boo_is = new FileInputStream(boo_path);
					boo_out = new ByteArrayOutputStream();
					byte[] buffer = new byte[1024];
					int len;
					while ((len = boo_is.read(buffer)) > -1) {
						boo_out.write(buffer, 0, len);
					}
					boo_out.flush();
					
				} catch (FileNotFoundException e) {
					log.error("爆炸音效文件打开失败    ----    "+e.getMessage());
				}
				catch (IOException e) {
					log.error(e.getMessage());
				}finally {
					if(boo_is !=null) {
						try {
							boo_is.close();
						} catch (IOException e) {
							log.error(e.getMessage());
						}
					}
				}
			}else {
				log.info("空文件");
			}
		}
	}
	
	//爆炸声
	public Player getBoo() {
		try {
			Player player = new Player(new BufferedInputStream(new ByteArrayInputStream(boo_out.toByteArray())));
			return player;
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
		return null;
	}
	//游戏前奏
	public Player getBeforeGame() {
		try {
			Player player = new Player(new BufferedInputStream(new ByteArrayInputStream(beforeGame_out.toByteArray())));
			return player;
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
		return null;
	}
	//消除行
	public Player getRemoveHang() {
		try {
			Player player = new Player(new BufferedInputStream(new ByteArrayInputStream(removeHang_out.toByteArray())));
			return player;
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
		return null;
	}
	//方块落下
	public Player getDownOver() {
		try {
			Player player = new Player(new BufferedInputStream(new ByteArrayInputStream(downOver_out.toByteArray())));
			return player;
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
		return null;
	}
	//物品合成
	public Player getHeCheng() {
		try {
			Player player = new Player(new BufferedInputStream(new ByteArrayInputStream(hecheng_out.toByteArray())));
			return player;
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
		return null;
	}
	//游戏暂停
	public Player getStopGame() {
		try {
			Player player = new Player(new BufferedInputStream(new ByteArrayInputStream(stopGame_out.toByteArray())));
			return player;
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
		return null;
	}
	//游戏继续
	public Player getStartGame() {
		try {
			Player player = new Player(new BufferedInputStream(new ByteArrayInputStream(startGame_out.toByteArray())));
			return player;
		} catch (JavaLayerException e) {
			e.printStackTrace();
		}
		return null;
	}
}

MusicConfig 音乐配置文件

public class MusicConfig {
	public static String MusicPath = "mp3\\music";
	public static String[]peizhi = {"背景音乐","游戏音乐"};
	//游戏前奏
	public static String beforeGame_path = "mp3\\boforeGame\\boforeGame.mp3";
	//特效音乐
	//爆炸
	public static String boo_path = "mp3\\bomb\\boo.mp3";
	//消除行
	public static String removeHang_path = "mp3\\bomb\\removeHang.mp3";
	//落到最后一行
	public static String downOver_path = "mp3\\bomb\\DownOver.mp3";
	//物品合成
	public static String hecheng_path = "mp3\\bomb\\hecheng.mp3";
	//暂停
	public static String stopGame_path = "mp3\\tishi\\stopGame.mp3";
	//继续
	public static String startGame_path = "mp3\\tishi\\startGame.mp3";
}

NewGameJPanel 加载新游戏数据面板

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import MyThread.BackFontTH;
import MyThread.BackgroundMusic;
import MyThread.BooTH;
import MyThread.DestroyLineTh;
import MyThread.DownOverTH;
import MyThread.HeChengTH;
import MyThread.StartGameTH;
import MyThread.StopGameTH;
import MyThread.ZiHuiTh;
import bean.Cell;
import bean.ImageMap;
import bean.ImagesMap;
import bean.JSONsConfig;
import bean.MusicJButton;
import cn.zhshipu.service.AllImage;
import cn.zhshipu.service.GameJPanelService;
import cn.zhshipu.service.JSONService;
import cn.zhshipu.service.LastSaveService;
import log.WinLog;

import static cn.zhshipu.config.Config.*;
import static cn.zhshipu.config.imageConfig.*;

public class NewGameJPanel extends GameJPanelService{
	private static final long serialVersionUID = 1L;
	
	private static List<Map<String, Boolean>> list_map_mods,list_map_config;
	
	
	public static Map<Integer, Integer> type;		//属性
	public static Map<Integer, String> tab;		//合成
    private static final int CELL_SIZE = 35;    //声明单元格的值
	
	//存档
	public static Cell[][] saveCell = null;
	public static AllImage saveCurrentOnt = null;
	public static AllImage saveNextOne = null;
	public static Double saveFenshu = null;
	public static List<Map<String,Boolean>> save_mods = null;
    
    //音乐
    private JPanel jp_music_name;
    private JLabel jl_music_name;
	private MusicJButton mjb = new MusicJButton(100, bf, "无");
	
	private boolean music_flag = false;

	public static boolean music_bg = true;		//背景音乐
	public static boolean music_yx = true;		//游戏声音
    
    private WinLog log = new WinLog("NewGameJPanel");
    
  //游戏三种状态 游戏中、暂停、结束
    public static final int PLING = 0;
    public static final int STOP = 1;
    public static final int OVER = 2;
    //当前游戏状态值
    private int game_state;
    //显示游戏状态
    String[] show_state = {"P[暂停]", "C[继续]", "S[重玩]"};
    //游戏分数池
    double[] scores_pool = {0.0, 1.0, 2.0, 5.0, 10.0, 15.0, 20.0, 30.0, 40.0, 50.0, 100.0, 200.0, 400.0, 800.0, 2000.0};
    
    
	//双重缓冲画布
	@Override
	public void update(Graphics g) {
		Image buff = this.createImage(W, H);
		Graphics g2 = buff.getGraphics();
		
			//绘制主区域,框
			zhuquyu(g2);
	        //正在下落的四方格
	        xialuoimg(g2);
	        //绘制将要下落的方格
	        xialuo(g2);
			//绘制游戏得分
	        fenshu(g2);
	        //绘制当前游戏状态
	        gameState(g2);
		
		//原图16x16  放大到35x35 基岩 底图
		for(int jy = 0;jy < 15; jy++) {
			g2.drawImage(jiyan, gX+35*jy,gY+gH, fk, fk ,null);
		}
		
		super.paint(g2);
		g2.dispose();
		g.drawImage(buff,0,0,null);
	}

	
	public void start() {
		
    	KeyListener l = new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            int code = e.getKeyCode();
            switch (code) {
                case KeyEvent.VK_DOWN:
                    sortDropActive();
                    break;
                case KeyEvent.VK_LEFT:
                    moveleftActive();
                    break;
                case KeyEvent.VK_RIGHT:
                    moveRightActive();
                    break;
                case KeyEvent.VK_UP:
                    rotateRightActive();
                    break;
                case KeyEvent.VK_SPACE:
                        hadnDropActive();
                    break;
                case KeyEvent.VK_P:
                    //判断当前游戏状态
                    if (game_state == PLING) {
                        game_state = STOP;
                    }
                    log.info("暂停");
                    if(music_yx) {
                    	//暂停音效
                    	new StopGameTH().start();
                    }
                    repaint();
                    break;
                case KeyEvent.VK_C:
                    if (game_state == STOP) {
                        game_state = PLING;
                    }
                    log.info("继续");
                    if(music_yx) {
                    	//继续音效
                    	new StartGameTH().start();
                    }
                    break;
                case KeyEvent.VK_S:
                    //重新开始
                    wall = new Cell[23][15];
                    currentOne = AllImage.randomOne();
                    nextOne = AllImage.randomOne();
                    fenshu = 0.0;
                	saveAll();
                    repaint();
                    game_state = PLING;
                    log.info("重玩");
                    break;
            	}
        	}
    	};
    
    	//将窗口设置为焦点
    	NewGameJPanel.this.addKeyListener(l);
    	NewGameJPanel.this.requestFocus();
	}
	

	//空格瞬间下落
	private void hadnDropActive() {
        //判断能否下落
		while (camDrop()) {
                //当前四方格下落一格
			sortDropActive();
        }
	}
	
	//顺时针旋转
	private void rotateRightActive() {
		currentOne.rotateRight();
		if(outOFBounds() || coincide()) {
			currentOne.rotateLeft();
		}
	}
	
	//按键一次,右移动一格
	private void moveRightActive() {
		currentOne.moveRight();
        //判断是否越界或重合
        if (outOFBounds() || coincide()) {
            currentOne.moveLeft();
        }
	}
	//按键一次,左移动一格
	private void moveleftActive() {
		currentOne.moveLeft();
        //判断是否越界或重合
        if (outOFBounds() || coincide()) {
            currentOne.moveRight();
        }
	}
	
	//判断是否出界
	private boolean outOFBounds() {
        Cell[] cells = currentOne.cells;
        for (Cell cell : cells) {
            int col = cell.getCol();
            int row = cell.getRow();
            if (row < 0 || row > wall.length - 1 || col < 0 || col > wall[0].length-1) {
                return true;
            }
        }
        return false;
	}
	
	//判断是否重合
	private boolean coincide() {
		Cell[] cells = currentOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            int col = cell.getCol();
            if (wall[row][col] != null) {
                return true;
            }
        }
        return false;
	}

	//按键一次,下落一格
	private void sortDropActive() {
        if (camDrop()) {
            //当前四方格下落一格
            currentOne.moveDrop();
        } else {
            landToWall();
            TypeLine();
            TabLine();
            destroyLine();
            if (isGameOver()) {
            	log.info("sortDropActive 游戏结束");
                game_state = OVER;
            	repaint();
            } else {
                //游戏没有结束
                currentOne = nextOne;
                nextOne = AllImage.randomOne();
            	saveAll();
                if(coincide()) {
                	game_state = OVER;
                	repaint();
                }
            }
        }
	}
	
	//判断游戏是否结束
	private boolean isGameOver() {
		Cell[] cells = nextOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            int col = cell.getCol();
            if (wall[row][col] != null) {
                return true;
            }
        }
        return false;
	}
	
	//合成	范围下一格,合成两格材料
	private void TabLine() {
		
		Map<Integer, Boolean> map_tab = new HashMap<Integer, Boolean>();
		while(true) {

			Cell[] celltab = currentOne.cells;
			map_tab.put(0, false);
			map_tab.put(1, false);
			map_tab.put(2, false);
			map_tab.put(3, false);

			for(int tabi =0;tabi<celltab.length;tabi++) {
				int row = celltab[tabi].getRow();
				int col = celltab[tabi].getCol();
				int x_row = row+1;
				
				JSONArray tab = celltab[tabi].getTab();

				if(tab != null && !"null".equals(tab)) {

					if(x_row < wall.length) {

						Cell xia = wall[x_row][col];
						if(xia!=null) {

							for(int tt=0;tt<tab.size();tt++) {
								JSONObject tabObj = (JSONObject)JSONObject.parse(new String(tab.getString(tt).getBytes()));
								String name = tabObj.getString("name");
								String newTab = tabObj.getString("newTab");

								if(name.equals(xia.getName())) {

									ImageIcon name_img = getTabImg(newTab);
									name_img.setImage(name_img.getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT));
									Cell tsg = new Cell(newTab,
											x_row,
											col,
											name_img,
											JSONService.getInstance().getJSONType(newTab),
											JSONService.getInstance().getJSONTab(newTab),0);
									Cell[] tsgs = {tsg};
									System.arraycopy(tsgs, 0, wall[x_row], col, 1);
									
									for(int xx = row;xx>0;xx--) {
										System.arraycopy(wall[xx-1], col, wall[xx], col, 1);
									}
									
									//判断是否合成TNT,进行爆炸
									if("tnt".equals(newTab)) {
										new Thread(new Runnable() {
											@Override
											public void run() {
												try {
													Thread.sleep(2000);
												} catch (InterruptedException e) {
													e.printStackTrace();
												}
												baoza(tsg);
												baozhayinxiao();
												baozhaxiaoguo2(tsg);
												xiaochu(tsg);
											}
										}).start();
									}
									
									celltab[tabi].down();
									
									Cell[] kong = new Cell[15];
									System.arraycopy(kong, col, wall[0], col, 1);
									if(music_yx) {
										new HeChengTH().start();
									}
									fenshu +=1.0;
									
									
									repaint();
									try {
										Thread.sleep(100);
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								}else {
									map_tab.put(tabi, true);
								}

							}
						}else{
							map_tab.put(tabi, true);
						}
					}else {
						map_tab.put(tabi, true);
					}
				}else {
					map_tab.put(tabi, true);
				}
			}
			
			if(map_tab.get(0) && map_tab.get(1) && map_tab.get(2) && map_tab.get(3)) {
            	saveAll();
				//再次检验全局是否有合成
				AllTab();
				break;
			}
		}
	}

	//检查全局是否有能合成的方块
	private void AllTab() {
		for(int j=0;j<22;j++) {		//22行,最后一行放弃
			for(int i=0;i<15;i++) {		//15列
				Cell cell = wall[j][i];		//每一个赋值
				if(cell != null) {	//原方块,过滤空值
					Cell xia = wall[j+1][i];	//获取下一个值信息
					if(xia != null) {	//下方方块	,过滤空值
						log.info("xia.Name = "+xia.getName());
						int row = cell.getRow();
						int col = cell.getCol();
						JSONArray tab = cell.getTab();	//获取当前合成列表
						if(tab != null ) {	//排除当前合成表空值
							log.info("tab ? = "+tab);
							for(int tt=0;tt<tab.size();tt++) {	//解析当前合成json
								if(!"null".equals(tab.getString(tt))) {	//	排除当前合成列表空值
									JSONObject tabObj = (JSONObject)JSONObject.parse(new String(tab.getString(tt).getBytes()));
									String name = tabObj.getString("name");
									String newTab = tabObj.getString("newTab");
									log.info("tab.name = "+name);
									if("null".equals(name)) {	//排除当前name对应的null值
										break;
									}
									if(name.equals(xia.getName())) {	//当前合成名字方块和下一个方块名字对比
										int xia_row = xia.getRow();		//获取下一个方块的行
										log.info("xia_row = "+xia_row);
										ImageIcon name_img = getTabImg(newTab);		//获取合成方块的img值
										name_img.setImage(name_img.getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT));
										Cell tsg = new Cell(newTab,
												xia_row,
												col,
												name_img,
												JSONService.getInstance().getJSONType(newTab),
												JSONService.getInstance().getJSONTab(newTab),0);	//创建新方块,合成后的方块
										log.info("tsg = "+tsg);
										Cell[] tsgs = {tsg};
										
										//判断是否合成TNT,进行爆炸
										if("tnt".equals(newTab)) {
											new Thread(new Runnable() {
												@Override
												public void run() {
													try {
														Thread.sleep(2000);
													} catch (InterruptedException e) {
														e.printStackTrace();
													}
													baoza(tsg);
													baozhayinxiao();
													baozhaxiaoguo2(tsg);
													xiaochu(tsg);
												}
											}).start();
										}
										
										System.arraycopy(tsgs, 0, wall[xia_row], col, 1);
										
										for(int xx = row;xx>0;xx--) {
											System.arraycopy(wall[xx-1], col, wall[xx], col, 1);
										}
										
										Cell[] kong = new Cell[15];
										System.arraycopy(kong, col, wall[0], col, 1);

										if(music_yx) {
											new HeChengTH().start();
										}
										fenshu +=1.0;
										
										repaint();
										try {
											Thread.sleep(100);
										} catch (InterruptedException e) {
											e.printStackTrace();
										}

				                    	saveAll();
										System.gc();

//										AllTab();
									}
								}
								
							}
						}
					}
				}
			}
		}
	}

	//判断下方块
	private ImageIcon getTabImg(String name) {
		Map<String, ImageIcon> map_name = ImageMap.getInstance().getStrToImg();
		ImageIcon img = map_name.get(name);
		return img;
	}
	// 属性
	private void TypeLine() {
		Cell[] cells = currentOne.cells;
		JSONArray type = cells[0].getType();
		if(type != null &&  !"null".equals(type)) {
			log.info("type.name = "+type);
			log.info("type.size = "+type.size());
			for(int tt=0;tt<type.size();tt++) {
				JSONObject typeObj = (JSONObject)JSONObject.parse(new String(type.getString(tt).getBytes()));
				log.info("jsonObj.name = "+typeObj.getString("name"));
				switch(typeObj.getString("name")) {
				case "chaijie":		//拆解台拆解
					log.info("拆解");
					//向右拆解
					for(Cell cell : cells) {
						int row = cell.getRow();
						int col = cell.getCol();
						if(row<22 && wall[row+1][col] != null) {
							Cell xia = wall[row+1][col];
							JSONArray xia_tab = xia.getTab();
							for(int x=0;x<xia_tab.size();x++) {
								JSONObject xiaObj = (JSONObject)JSONObject.parse(new String(xia_tab.getString(x).getBytes()));
								if(!"null".equals(xiaObj.getString("name")) && xiaObj != null) {
									String name = xiaObj.getString("name");
									String newTab = xiaObj.getString("newTab");
									log.info("分解 name = "+name);
									log.info("分解newTab = "+newTab);
								}
							}
							
						}
					}
					break;
				case "zihui":
					log.info("树叶自毁");
					for(Cell c : cells) {
						new ZiHuiTh(c,wall).start();
					}
					break;
				case "shengzhang":
					log.info("树苗生长"); 	//树苗生长
					//判断下一个物品是否是泥土
					for(Cell cell : cells) {
						int row = cell.getRow();
						int col = cell.getCol();
						if(row+1 < 23) {
							if(null != wall[row+1][col]) {
								String xia = wall[row+1][col].getName();
								if("caokuai".equals(xia)) {
									//清除当前
									Cell[] tsg = new Cell[10];
									System.arraycopy(tsg, 0, wall[row], col, 1);
									if(row-1>0)
									System.arraycopy(tsg, 1, wall[row-1], col, 1);
									if(row-2>0)
									System.arraycopy(tsg, 2, wall[row-2], col, 1);
									if(row-2>0&&col-1>0)
									System.arraycopy(tsg, 3, wall[row-2], col-1, 1);
									if(row-2>0&&col+1<15)
									System.arraycopy(tsg, 4, wall[row-2], col+1, 1);
									if(row-3>0)
									System.arraycopy(tsg, 5, wall[row-3], col, 1);
									if(row-3>0&&col+1<15)
									System.arraycopy(tsg, 6, wall[row-3], col+1, 1);
									if(row-3>0&&col-1>0)
									System.arraycopy(tsg, 7, wall[row-3], col-1, 1);
									if(row-2>0&&col-2>0)
									System.arraycopy(tsg, 8, wall[row-2], col-2, 1);
									if(row-2>0&&col+2<15)
									System.arraycopy(tsg, 9, wall[row-2], col+2, 1);
									
									Cell[] mugan = new Cell[3];
									Cell[] yezi1 = new Cell[3];
									Cell[] yezi2 = new Cell[2];
									Cell[] yezi3 = new Cell[2];
									String mg = "xiangmushugan";
									String yz1 = "yezi1";
									String yz2 = "yezi2";
									String yz3 = "yezi3";

									/*
									 *   6 4 7  
									 * 8 3 2 5 9
									 *     1    
									 *     0    
									 * */
									
									mugan[0] = getCell(mg,row,col);
									mugan[1] = getCell(mg,row-1,col);
									mugan[2] = getCell(mg,row-2,col);
									
									yezi1[0] = getCell(yz1,row-2,col-1);
									yezi1[1] = getCell(yz1,row-3,col);
									yezi1[2] = getCell(yz1,row-2,col+1);
									
									yezi2[0] = getCell(yz2,row-3,col-1);
									yezi2[1] = getCell(yz2,row-3,col+1);
									
									yezi3[0] = getCell(yz3,row-2,col-2);
									yezi3[1] = getCell(yz3,row-2,col+2);
									
									//0	
									System.arraycopy(mugan, 0, wall[row], col, 1);
									//1
									if(row -1 >0) {
										System.arraycopy(mugan, 1, wall[row-1], col, 1);
									}
									//2
									if(row -2 >0) {
										System.arraycopy(mugan, 2, wall[row-2], col, 1);
									}
									//3
									if(row -2 >0 && col - 1 >=0) {
										System.arraycopy(yezi1, 0, wall[row-2], col-1, 1);
									}
									//4
									if(row -3 >0) {
										System.arraycopy(yezi1, 1, wall[row-3], col, 1);
									}
									//5
									if(row -2 >0 && col + 1 <15) {
										System.arraycopy(yezi1, 2, wall[row-2], col+1, 1);
									}
									//6
									if(row -3 >0 && col - 1 >=0) {
										System.arraycopy(yezi2, 0, wall[row-3], col-1, 1);
									}
									//7
									if(row -3 >0 && col + 1 <15) {
										System.arraycopy(yezi2, 1, wall[row-3], col+1, 1);
									}
									//8
									if(row -2 >0 && col - 2 >=0) {
										System.arraycopy(yezi3, 0, wall[row-2], col-2, 1);
									}
									//9
									if(row -2 >0 && col + 2 <15) {
										System.arraycopy(yezi3, 1, wall[row-2], col+2, 1);
									}
									for(int o=0;o<3;o++) {
										new ZiHuiTh(yezi1[o],wall).start();
									}
									for(int p=0;p<2;p++) {
										new ZiHuiTh(yezi2[p],wall).start();
									}
									for(int q=0;q<2;q++) {
										new ZiHuiTh(yezi3[q],wall).start();
									}
									try {
										Thread.sleep(50);
									} catch (InterruptedException e) {
										e.printStackTrace();
									}
								}
							}
							}
							
					}
					System.gc();
//					TypeLine();
					break;
				case "yeti":
					log.info("液体");	//具有向下流动性
					Map<Integer, Boolean> map_boo = new HashMap<Integer, Boolean>();
					while(true) {
						Cell[] cellsh = currentOne.cells;
						map_boo.put(0, false);
						map_boo.put(1, false);
						map_boo.put(2, false);
						map_boo.put(3, false);
						
						for(int bo=0;bo<cellsh.length;bo++) {
							int row = cellsh[bo].getRow();
							int col = cellsh[bo].getCol();
							
							if((row+1) < wall.length && wall[row+1][col] == null) {
								cellsh[bo].down();
								Cell[] tsg = new Cell[15];
								System.arraycopy(wall[row], col, wall[row+1], col, 1);
								System.arraycopy(tsg,col,wall[row],col,1);
							}else {
								map_boo.put(bo, true);
							}
							
						}
						
						try {
							repaint();
							Thread.sleep(100);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						if(map_boo.get(0) && map_boo.get(1) && map_boo.get(2) && map_boo.get(3)) {
	                    	saveAll();
							break;
						}
					}
					break;
				case "baozha":	//TXT爆炸 苦力怕爆炸	消除3x3
					log.info("爆炸");
					
					//变形
					for(Cell cell : cells) {
						baoza(cell);
					}
					//爆炸音效
					baozhayinxiao();
					
					//爆炸效果
					baozhaxiaoguo();
					
					//消除
					for(Cell cell : cells) {
						xiaochu(cell);
					}
					fenshu+=5.0;

                	saveAll();
					System.gc();
					
					break;
				default:
					break;
				}
			}
		}
	}
	
	private void xiaochu(Cell cell) {
		int row = cell.getRow();
		int col = cell.getCol();
		if((col-1>=0)) {
			col -=1;
		}
		if((row+1)<=wall.length-1) {
			row+=1;
		}
		int bz = 3;	
		if((cell.getCol()+1) > wall[0].length-1) {	//右边是墙壁,无法爆炸
			bz = 2;
		}
		for(int j = 3;j>0;j--) {		//消除行
			for(int i=row;i>0;i--) {	//消除列
				System.arraycopy(wall[i-1], col, wall[i], col, bz);
			}
			Cell tsg[] = new Cell[15];
			System.arraycopy(tsg, col, wall[0], col, bz);
		}
	}
	
	private void baozhaxiaoguo2(Cell cell) {
		for(int bb =1;bb<22;bb++) {
			ImageIcon bomb = new ImageIcon("images\\bomb\\"+bb+".png");
			bomb.setImage(bomb.getImage().getScaledInstance(fk,fk,Image.SCALE_DEFAULT));
			cell.setImg(bomb);
			try {
				Thread.sleep(32);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void baozhaxiaoguo() {
		for(int bb =1;bb<22;bb++) {
			ImageIcon bomb = new ImageIcon("images\\bomb\\"+bb+".png");
			bomb.setImage(bomb.getImage().getScaledInstance(fk,fk,Image.SCALE_DEFAULT));
			currentOne.HuanImg(bomb);
			try {
				Thread.sleep(32);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	private void baozhayinxiao() {
		if(music_yx) {
			new BooTH().start();
		}
	}

	private void baoza(Cell cell) {
		int row = cell.getRow();
		int col = cell.getCol();
		int x1 = row - 1;
		int x2 = row + 1;
		int y1 = col - 1;
		int y2 = col + 1;
		if(x1>=0) {
			System.arraycopy(wall[row], col, wall[row-1], col, 1);	//复制粘贴到上方
		}
		if(x2<=wall.length-1) {
			System.arraycopy(wall[row], col, wall[row+1], col, 1);	//复制粘贴到下方
		}
		if(y1 >= 0) {
			System.arraycopy(wall[row], col, wall[row], col-1, 1);	//复制粘贴到左方
		}
		if(y2 <= wall[0].length-1) {
			System.arraycopy(wall[row], col, wall[row], col+1, 1);	//复制粘贴到右方
		}
		if(x1 >= 0 && y1 >= 0) {
			System.arraycopy(wall[row], col, wall[row-1], col-1, 1);	//复制粘贴到左上方
		}
		if(x1 >= 0 && y2 <= wall[0].length-1) {
			System.arraycopy(wall[row], col, wall[row-1], col+1, 1);	//复制粘贴到右上方
		}
		if(x2<=wall.length-1 && y1 >= 0) {
			System.arraycopy(wall[row], col, wall[row+1], col-1, 1);	//复制粘贴到左下方
		}
		if(x2<=wall.length-1 && y2 <= wall[0].length-1) {
			System.arraycopy(wall[row], col, wall[row+1], col+1, 1);	//复制粘贴到右下方
		}
	}

	private Cell getCell(String name, int row, int col) {
		ImageIcon img = ImageMap.getInstance().getStrToImg().get(name);
		img.setImage(img.getImage().getScaledInstance(35,35,Image.SCALE_DEFAULT));
		JSONArray type_s = JSONService.getInstance().getJSONType(ImageMap.getInstance().getImgMapName().get(img));
		JSONArray tab_s = JSONService.getInstance().getJSONTab(ImageMap.getInstance().getImgMapName().get(img));
		Cell c = new Cell(name,row,col,img,type_s,tab_s,0);
		return c;
	}

	//消除行
	private void destroyLine() {
		int line = 0;
        Cell[] cells = currentOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            if (isFullLine(row)) {
            	log.info("第"+(int)(row+1)+"行满了进行消除");

                line++;
                for (int i = row; i > 0; i--) {
                    System.arraycopy(wall[i - 1], 0, wall[i], 0, wall[0].length);
                }
                wall[0] = new Cell[15];
                if(music_yx) {
                	new DestroyLineTh().start();
                }
                
                //遍历一遍所有
                AllRow();
            }
        }
        //分数池获取分数,累加到总分
        fenshu += scores_pool[line];
	}

	//消除多余没有消除的行
	private void AllRow() {
		int line = 0 ;
		for(int row=0;row<23;row++) {
			if(isFullLine(row)) {
            	log.info("第"+(int)(row+1)+"行满了进行消除");
            	line++;
                for (int i = row; i > 0; i--) {
                    System.arraycopy(wall[i - 1], 0, wall[i], 0, wall[0].length);
                }
                wall[0] = new Cell[15];
                if(music_yx) {
                    new DestroyLineTh().start();
                }
			}
		}
		fenshu += scores_pool[line];
	}

	//判断当前行是否满了
	private boolean isFullLine(int row) {
        Cell[] cells = wall[row];
        for (Cell cell : cells) {
            if (cell == null) {
                return false;
            }
        }
        return true;
	}
	//单元格嵌入墙中
	private void landToWall() {
        Cell[] cells = currentOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            int col = cell.getCol();
            try {
                wall[row][col] = cell;
                if(music_yx) {
                	new DownOverTH().start();
                }
            }catch (Exception e) {
				if (camDrop()) {
                	currentOne.moveDrop();
            	} else {
            		if(music_yx) {
                    	new DownOverTH().start();
            		}
                	landToWall();
                	destroyLine();
                	if (isGameOver()) {
                		log.info("landToWall 游戏结束");
                    	game_state = OVER;
                    	repaint();
                	} else {
                		//游戏没有结束
                    	currentOne = nextOne;
                    	nextOne = AllImage.randomOne();
                    	saveAll();
                    	if(coincide()) {
                        	game_state = OVER;
	                    	repaint();
                        }
                		}
            		}
			}
        }
	}
	//判断四方格能否下落
	private boolean camDrop() {
		saveAll();
        Cell[] cells = currentOne.cells;
        for (Cell cell : cells) {
            int row = cell.getRow();
            int col = cell.getCol();
            //判断是否到达底部
            if (row == wall.length - 1) {
                return false;
            } else if (wall[row + 1][col] != null) {
                return false;
            }else if(col < 0) {
            	return false;
            }
        }
        return true;
	}

	private void gameState(Graphics g2) {
        if (game_state == PLING) {
            g2.drawString("状态: "+show_state[PLING], 560, 580);
        } else if (game_state == STOP) {
            g2.drawString("状态: "+show_state[STOP], 560, 580);
        } else {
            g2.drawString("状态: "+show_state[OVER], 560, 580);
            g2.setColor(Color.RED);
            g2.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 60));
            g2.drawString("GAME OVER!", 30, 400);
        }
	}

	private void fenshu(Graphics g2) {
		//分数
		g2.setColor(Color.red);
		g2.setFont(f30);
		g2.drawString(fenshu.intValue()+"分", zX, zY);
	}

	//将要下落的四方块
	private void xialuo(Graphics g2) {
        Cell[] cells = nextOne.cells;
        for (Cell cell : cells) {
            int x = cell.getCol() * CELL_SIZE ;
            int y = cell.getRow() * CELL_SIZE ;
            g2.drawImage(cell.getImg().getImage(), x+tX-260, y+tY-fk, null);
        }
	}

	//正在下落的四方块
	private void xialuoimg(Graphics g2) {
		Cell[] cells = currentOne.cells;
        for (Cell cell : cells) {
            int x = cell.getCol() * CELL_SIZE;
            int y = cell.getRow() * CELL_SIZE;
            g2.drawImage(cell.getImg().getImage(), x+gX, y+gY, null);
        }
	}

	private void zhuquyu(Graphics g2) {
		g2.setColor(Color.black);
		//大框 横分15个 竖分23个 一个像素35x35 fk x fk
        for (int i = 0; i < wall.length; i++) {
            for (int j = 0; j < wall[i].length; j++) {
                int x = j * CELL_SIZE;
                int y = i * CELL_SIZE;
                Cell cell = wall[i][j];
                //判断是否有小方块
                if (cell == null) {
                    g2.drawRect(x+gX, y+gY, CELL_SIZE, CELL_SIZE);
                } else {
                    g2.drawImage(cell.getImg().getImage(), x+gX, y+gY, null);
                }
            }
        }
	}

	//画布
	@Override
	public void paint(Graphics g) {
		update(g);
	}
	
	NewGameJPanel(){
		log.info("加载控件");
	}
	
	//加载模组图片
	private void initModsMap(List<Map<String, Boolean>> map) {
		for(int i=0;i<map.size();i++) {
			if(map.get(i).get(mods[i])) {
				//加载模组图片
				ImagesMap im = new ImagesMap(i);
				im.updatee();
				
				//加载模组配置
				JSONsConfig jc = new JSONsConfig(i);
				jc.update();
			}
		}
	}

	private void gogogo() {
    	new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
					try {
						Thread.sleep(24);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					if(game_state == PLING) {
			        	repaint();
					}
				}
			}
		}).start();
    	
    	new Thread(new Runnable() {
			@Override
			public void run() {
		    	while (true) {
	            	try {
	                	Thread.sleep(1000);
	            	} catch (InterruptedException e) {
	                	e.printStackTrace();
	            	}
		        	if (game_state == PLING) {
		            	if (camDrop()) {
		                	currentOne.moveDrop();
		            	} else {
		                	landToWall();
		                	TypeLine();
		                	TabLine();
		                	destroyLine();
		                	if (isGameOver()) {
		                		log.info("new Thread(new Runnable() 游戏结束");
		                    	game_state = OVER;
		                    	repaint();
		                	} else {
		                		//游戏没有结束
		                    	currentOne = nextOne;
		                    	nextOne = AllImage.randomOne();
		                    	saveAll();
		                    	if(coincide()) {
		                        	game_state = OVER;
			                    	repaint();
		                        }
		                	}
		            	}
		        	}
		    	}
			}
		}).start();
	}

	private void initMusic(List<Map<String, Boolean>> list_map) {
//		JLabel jl_bgk = getJLbgk();	//总面板
		

		music_flag = true;
		//音乐名字
		jp_music_name = new JPanel();
		jp_music_name.setLayout(null);
		jp_music_name.setBounds(230, 30, 300, 40);
		jl_bgk.add(jp_music_name);
				
		jl_music_name = new JLabel(mjb.music_name);
		jl_music_name.setBounds(mjb.x, 0, mjb.music_name.length()*45, 40);
		jl_music_name.setFont(f40);
		jp_music_name.add(jl_music_name);
		
		for(Map<String, Boolean> map : list_map) {
			String name = map.keySet().toString().substring(1, map.keySet().toString().length()-1);
			switch(name) {
			case "背景音乐":
				if(map.get(name)) {
					new BackFontTH(music_flag, mjb, jl_music_name).start();
					new BackgroundMusic(mjb,jl_music_name).start();
				}
				break;
			case "游戏音乐":
				music_yx = map.get(name);
				break;
			}
		}
		
	}
	
	//存档
	private void saveAll() {
		NewGameJPanel.saveCell = wall;
		NewGameJPanel.saveCurrentOnt = currentOne;
		NewGameJPanel.saveNextOne = nextOne;
		NewGameJPanel.saveFenshu = fenshu;
	}

	//新游戏
	public void showNewGame(Frame main) {
		list_map_mods = ModsJPanel.getInstance().getListMap();
		save_mods = list_map_mods;
		list_map_config = ConfigJPanel.getInstance().getListMap();
		
		log.info("传入配置 = "+list_map_config);
		
		game_state = PLING;	//开始状态
		//加载模组图片数组
		initModsMap(list_map_mods);
//		JSONService.getInstance().initConfigImage();	//加载图片和配置对应关系
		
		//加载音乐
		initMusic(list_map_config);
		
		main.setVisible(false);
		NewGameJPanel.this.setVisible(true);
		
		start();	//进入游戏
		gogogo();	//刷新
	}

	//上一次游戏
	public void updateNewGame(Frame main) {
		list_map_mods = LastSaveService.getInstance().getListMods();
		save_mods = list_map_mods;
		list_map_config = getMods();
		wall = LastSaveService.getInstance().getWall();
		fenshu = LastSaveService.getInstance().getFenShu();
		
		currentOne = LastSaveService.getInstance().getOne();
		nextOne = LastSaveService.getInstance().getNextOne();
		
		game_state = STOP;	//暂停状态
		
		//加载模组图片数组
		initModsMap(list_map_mods);
		//加载音乐
		initMusic(list_map_config);
		
		
		main.setVisible(false);
		NewGameJPanel.this.setVisible(true);

		saveAll();	//保存一次当前
		start();	//进入游戏		
		gogogo();	//刷新
	}
	
}

没用的知识又增加了,哈哈哈哈哈哈哈哈哈哈!!!

有啥推荐的小游戏各位小伙伴推荐一下

使用javaGUI做一个MC版俄罗斯方块(二)
使用javaGUI做一个MC版俄罗斯方块(三)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈氏家族-廷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值