使用开源代码做开源游戏,真香!
喜欢玩俄罗斯方块和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
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{
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;
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;
}
}
}
}
}
}
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;
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";
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);
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);
}
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++) {
for(int i=0;i<15;i++) {
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++) {
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)) {
break;
}
if(name.equals(xia.getName())) {
int xia_row = xia.getRow();
log.info("xia_row = "+xia_row);
ImageIcon name_img = getTabImg(newTab);
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};
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();
}
}
}
}
}
}
}
}
}
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";
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);
System.arraycopy(mugan, 0, wall[row], col, 1);
if(row -1 >0) {
System.arraycopy(mugan, 1, wall[row-1], col, 1);
}
if(row -2 >0) {
System.arraycopy(mugan, 2, wall[row-2], col, 1);
}
if(row -2 >0 && col - 1 >=0) {
System.arraycopy(yezi1, 0, wall[row-2], col-1, 1);
}
if(row -3 >0) {
System.arraycopy(yezi1, 1, wall[row-3], col, 1);
}
if(row -2 >0 && col + 1 <15) {
System.arraycopy(yezi1, 2, wall[row-2], col+1, 1);
}
if(row -3 >0 && col - 1 >=0) {
System.arraycopy(yezi2, 0, wall[row-3], col-1, 1);
}
if(row -3 >0 && col + 1 <15) {
System.arraycopy(yezi2, 1, wall[row-3], col+1, 1);
}
if(row -2 >0 && col - 2 >=0) {
System.arraycopy(yezi3, 0, wall[row-2], col-2, 1);
}
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();
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":
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);
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) {
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);
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版俄罗斯方块(三)