一、项目及来源
我选取的项目是一个用Java语言开发的拼图小游戏,来自https://www.cnblogs.com/get-elaina/p/17854791.html
该拼图游戏实现了拼图的打乱功能和移动功能,用户可以通过↑ ↓ ← → 键来对图片进行移动,并记录下用户所使用的步数,当用户将拼图复位后,游戏步数将不在增加,键盘监听也停止,↑ ↓ ← → 键不在响应,此时用户可以重新开始新一轮的游戏,重新打乱图片在此开始挑战。
以下是原项目的运行截图:
如图所示:
二、运行环境
在jdk2.1环境下,开发环境为Intelij IDEA.
三、创新点
在测试并使用该项目后我认为该项目没有大问题,功能可以创新增加新功能。比如原项目中的拼图默认为4X4,拼图方块被切割的过小导致游戏难度加大,以至于难以通关,所以我决定可以对其增加难度等级,简单模式和困难模式,简单模式下的拼图为3X3,降低了游戏的难度,增加了用户的游戏体验。
此外,还可以多增加些图片供用户选择,可以减少游戏的单一性,增加游戏的趣味性,可以选取一些更好看的图片来增加用户想要玩游戏的意愿,
四、新增加的代码
新建一个二维数组3X3,用来储存图片默认顺序,将原始图片分为9份小图片,将他们的编号存入数组中,每次移动图片改变二维数组中编号位置,并更新图片。
以下是界面的详细参数设置,如宽高位置等:
int[][] date = new int[3][3];
int x = 0;
int y = 0;
int[][] win = new int[][]{{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
int step = 0;public GameJFrame() {this.InitJFrame();
}private void InitJFrame() {this.setSize(862, 900);this.setTitle("拼图小游戏单机版 v1.0");this.setAlwaysOnTop(true);this.setLocationRelativeTo((Component)null);this.setDefaultCloseOperation(2);this.setLayout((LayoutManager)null);this.InitJMenu();this.puzledate();this.puzzlePicture();this.addKeyListener(this);this.setVisible(true);
}private void puzledate() {int[] tempArr = new int[]{0, 1, 2, 3, 4, 5, 6, 7, 8};Random r = new Random();int i;for(i = 0; i < tempArr.length; ++i) {int num = r.nextInt(tempArr.length);int temp = tempArr[i];tempArr[i] = tempArr[num];tempArr[num] = temp;}for(i = 0; i < tempArr.length; ++i) {this.date[i / 3][i % 3] = tempArr[i];if (tempArr[i] == 0) {this.x = i / 3;this.y = i % 3;}}}private void puzzlePicture() {this.getContentPane().removeAll();if (this.victory()) {ImageIcon bg = new ImageIcon("src\\com\\lcy\\ui\\puzzlePicture\\win2.png");JLabel bgJLabel = new JLabel(bg);this.getContentPane().add(bgJLabel);bgJLabel.setBounds(230, 230, 260, 260);}JLabel stepCount = new JLabel("步数" + this.step);stepCount.setBounds(50, 30, 150, 75);this.getContentPane().add(stepCount);for(int i = 0; i < 3; ++i) {for(int j = 0; j < 3; ++j) {ImageIcon icon = new ImageIcon("src\\com\\lcy\\ui\\puzzlePicture\\0" + this.date[i][j] + ".jpg");JLabel jLabel = new JLabel(icon);this.getContentPane().add(jLabel);jLabel.setBounds(200 * j + 133, 200 * i + 162, 200, 200);jLabel.setBorder(new BevelBorder(1));}}ImageIcon bg = new ImageIcon("src\\com\\lcy\\ui\\puzzlePicture\\background.png");JLabel bgJLabel = new JLabel(bg);this.getContentPane().add(bgJLabel);bgJLabel.setSize(862, 862);this.getContentPane().repaint();
}void InitJMenu() {JMenuBar jMenuBar = new JMenuBar();JMenu funcJMenu = new JMenu("功能");JMenu moreJMenu = new JMenu("更多");JMenuItem changeItem = new JMenuItem("更换图片");JMenuItem replayItem = new JMenuItem("重新开始");JMenuItem reLoginItem = new JMenuItem("重新登录");JMenuItem exitItem = new JMenuItem("退出");JMenuItem moreItem = new JMenuItem("更多功能");funcJMenu.add(changeItem);funcJMenu.add(replayItem);funcJMenu.add(reLoginItem);funcJMenu.add(exitItem);moreJMenu.add(moreItem);jMenuBar.add(funcJMenu);jMenuBar.add(moreJMenu);this.setJMenuBar(jMenuBar);
}
}
五、改进后截图:
下面是改进后的项目展示
打乱后:
打乱前:
.
六、思考总结
对于此次项目的改进,我认为其中的难点和耗时较久的部分主要为界面布置和图片代码逻辑的更改:需要找到合适的图片并分为九张像素合适小图片,存入文件中,还需要在界面中找到合适的像素点插入图片以免出现与背景图片贴合不好;由于图片的编号数组由4X4更改为3X3,所以数组的边界判定,↑ ↓ ← → 的操作也需要做出对应的调整;更换图片时对应的图片储存的文件夹不同,需要建立字符串数组来记录不同图片的文件路径。
这次项目的创新从用户的角度出发,考虑到了用户不同的喜好以及对游戏难易程度的需求,对该项目进行了创新,增加了部分功能,使该项目有更好的功能实现和更强的商业竞争力,贴合用户需求,各个方面都更加完善。