项目介绍
功能简介
拼图游戏可以实现登录,注册,选择图片,可以对用户进行多个存档和读档。玩家需要先注册登录,玩家可以挑选游戏图片,通过键盘监听可以上下左右移动拼图,系统还可以实现判断是否已经完成拼图,并提供了w键,一键完成拼图。
采用了ai辅助编程
参考项目:Java Puzzle game - creating Java puzzle game clone
git地址:一路向北/ping
功能架构图
面对对象设计类图
-
GameJFrame类图:对游戏ui进行显示并对游戏逻辑进行实现
GameJFrame |
- JMenuItem replayItem - JMenuItem reLoginItem - JMenuItem closeItem - JMenuItem accountItem - JMenuItem girl - JMenuItem animal - JMenuItem sport - JMenu saveJMenu - JMenu loadJMenu |
+ initData() 图片初始化 + initImage() 图片美化及胜利判断 + initJMenuBar() ui界面设计 + initJFrame() ui界面设计 + keyPressed(KeyEvent e) 键盘按压监听 + keyReleased(KeyEvent e) 键盘松开监听 + victory() 胜利判断 + actionPerformed(ActionEvent e) 动作监听 |
-
游戏界面
-
功能界面
-
游戏关键代码
1、说明:实现 KeyListener 接口,用于处理按键的按下和释放事件,以移动图块并触发其他动作,如解谜或显示完整图片
public void keyReleased(KeyEvent e) {
int code = e.getKeyCode();
if (code == 37) {
System.out.println("向左");
if (y == 3) {
return;
}
arr[x][y] = arr[x][y + 1];
arr[x][y + 1] = 0;
y = y + 1;
step++;
initImage();
}
if (code == 38) {
System.out.println("向上");
if (x == 3) {
return;
}
arr[x][y] = arr[x + 1][y];
arr[x + 1][y] = 0;
x = x + 1;
step++;
initImage();
}
if (code == 39) {
System.out.println("向右");
if (y == 0) {
return;
}
arr[x][y] = arr[x][y - 1];
arr[x][y - 1] = 0;
y = y - 1;
step++;
initImage();
}
if (code == 40) {
System.out.println("向下");
if (x == 0) {
return;
}
arr[x][y] = arr[x - 1][y];
arr[x - 1][y] = 0;
x = x - 1;
step++;
initImage();
} else if (code == 65) {
initImage();
} else if (code == 87) {
arr = new int[][]{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 0}
};
x = 3;
y = 3;
initImage();
}
}
2、定义用户与菜单项交互时要采取的动作,如重新开始游戏、重新登录、关闭、更换图片、保存和加载游戏状态等。
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
Random s = new Random();
if (source == replayItem) {
System.out.println("重新开始");
//打乱数据
initData();
//计步器清零
step = 0;
//导入图片
initImage();
} else if (source == reLoginItem) {
System.out.println("重新登陆");
//关闭界面
this.setVisible(false);
//打开登录界面
new LoginJFrame();
} else if (source == closeItem) {
System.out.println("关闭游戏");
System.exit(0);
} else if (source == accountItem) {
System.out.println("公众号");
JDialog jDialog = new JDialog();
JLabel jLabel = new JLabel(new ImageIcon("..//pingGame\\image\\mewechat.jpg"));
jLabel.setBounds(0, 0, 300, 408);
jDialog.getContentPane().add(jLabel);
jDialog.setSize(500, 500);
jDialog.setAlwaysOnTop(true);
jDialog.setLocationRelativeTo(null);
jDialog.setModal(true);
jDialog.setVisible(true);
} else if (source == girl) {
int index = s.nextInt(14);
path = "..//pingGame\\image\\girl\\girl" + index + "\\";
System.out.println(path);
initData();
initImage();
} else if (source == sport) {
int index = s.nextInt(11);
path = "..//pingGame\\image\\sport\\sport" + index + "\\";
System.out.println(path);
initData();
initImage();
} else if (source == animal) {
int index = s.nextInt(9);
path = "..//pingGame\\image\\animal\\animal" + index + "\\";
System.out.println(path);
initData();
initImage();
} else if (source == saveItem0 || source == saveItem1 || source == saveItem2 || source == saveItem3 || source == saveItem4) {
System.out.println("存档");
JMenuItem item = (JMenuItem) source;
String str = item.getText();
System.out.println(str);
int index = str.charAt(2) - '0';
System.out.println(index);
//存档
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("..//pingGame//save//save" + index + ".data"));
GameInfo gi = new GameInfo(arr, x, y, path, step);
oos.writeObject(gi);
oos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
//把item展示出来
item.setText("存档" + index + "(" + step + "步)");
//读档的也可以处理一下
loadJMenu.getItem(index).setText("存档" + index + "(" + step + "步)");
} else if (source == loadItem0 || source == loadItem1 || source == loadItem2 || source == loadItem3 || source == loadItem4) {
System.out.println("读档");
//读档按数字
JMenuItem item = (JMenuItem) source;
String str = item.getText();
System.out.println(str);
int index = str.charAt(2) - '0';
System.out.println(index);
//读档
//要用但不是全局
GameInfo gi=null;
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("..//pingGame//save//save" + index + ".data"));
gi =(GameInfo) ois.readObject();
ois.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
//获取数据
arr=gi.getDate();
path=gi.getPath();
step=gi.getStep();
x=gi.getX();
y=gi.getY();
//重置
initImage();
}
3、检查当前图块排列是否与获胜状态相匹配。
public boolean victory() {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] != data[i][j]) {
return false;
}
}
}
return true;
}
4.进行存档及读档
else if (source == saveItem0 || source == saveItem1 || source == saveItem2 || source == saveItem3 || source == saveItem4) {
System.out.println("存档");
JMenuItem item = (JMenuItem) source;
String str = item.getText();
System.out.println(str);
int index = str.charAt(2) - '0';
System.out.println(index);
//存档
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("..//pingGame//save//save" + index + ".data"));
GameInfo gi = new GameInfo(arr, x, y, path, step);
oos.writeObject(gi);
oos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
//把item展示出来
item.setText("存档" + index + "(" + step + "步)");
//读档的也可以处理一下
loadJMenu.getItem(index).setText("存档" + index + "(" + step + "步)");
} else if (source == loadItem0 || source == loadItem1 || source == loadItem2 || source == loadItem3 || source == loadItem4) {
System.out.println("读档");
//读档按数字
JMenuItem item = (JMenuItem) source;
String str = item.getText();
System.out.println(str);
int index = str.charAt(2) - '0';
System.out.println(index);
//读档
//要用但不是全局
GameInfo gi=null;
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("..//pingGame//save//save" + index + ".data"));
gi =(GameInfo) ois.readObject();
ois.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
//获取数据
arr=gi.getDate();
path=gi.getPath();
step=gi.getStep();
x=gi.getX();
y=gi.getY();
//重置
initImage();
}
项目代码扫描结果及改造
使用阿里巴巴静态扫描工具对我们的项目进行扫描
修改后
项目总结
我负责的模块为游戏主界面可以实现图片的上下移动和存档读档等功能,目前项目还存在许多不足,比如不能划分游戏难度,不能实现图片的自动分割的功能,因此我们需要完善的东西还有很多,通过这个项目我体会到了工程化项目的实现及团队合作的沟通方式。