朴素的拼图游戏,通过WASD移动,我自己都不太会,可以通过篡改内部代码来取得想要的成功彩蛋
代码:
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.Random;import javax.swing.ImageIcon;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;public class MainFrame extends JFrame implements KeyListener, ActionListener{//继承父类非私有方法,方便创建新的方法public static void main(String[] args) {// TODO Auto-generated method stubnew MainFrame();}String path = "A";int[][] datas = { {1, 2, 3},{4, 5, 6},{7, 8, 0}};int fx = datas.length - 1, fy = datas.length - 1;int count = -1;//记录移动次数JMenuItem item1 = new JMenuItem("打乱");JMenuItem item2 = new JMenuItem("复原");boolean selfdo = false; //构造器public MainFrame(){initFrame();pantview();//放在这里更保险initmemu();//初始化菜单this.addKeyListener(this);setVisible(true);//设置窗体可见}//界面初始化public void initFrame(){setTitle("游戏"); setSize(804, 534);//设置尺寸 setLocationRelativeTo(null);//设置窗体居中 setAlwaysOnTop(true);//设置窗体置顶 setDefaultCloseOperation(3);//设置窗体的关闭模式//取消窗口默认布局 setLayout(null);}/*菜单*/public void initmemu() {JMenuBar menu = new JMenuBar();//菜单框JMenu menu1 = new JMenu("菜单"); JMenu menu2 = new JMenu("关于我们");//一阶菜单 JMenu menu3 = new JMenu("作者");JMenuItem item3 = new JMenuItem("Narnat");JMenu menu4 = new JMenu("指导者");JMenuItem item4 = new JMenuItem("黑马程序员");//串起来menu.add(menu1);menu.add(menu2);menu1.add(item1);menu1.add(item2);menu2.add(menu3);menu3.add(item3);menu2.add(menu4);menu4.add(item4);/*只要监听运行一次就会一直监听*/item1.addActionListener(this);item2.addActionListener(this);setJMenuBar(menu);//放入窗体}//图片放映public void pantview(){ /*先移除掉原有的画面*/ getContentPane().removeAll();count ++;//System.out.println(selfdo + " " + checkVictory());pantVictory(selfdo && checkVictory());//画出成功for(int i = 0; i < 3; i ++)for(int j = 0; j < 3; j ++) {JLabel image = new JLabel(new ImageIcon("D:\\New_Study\\JAVA\\gameJAVA\\" + datas[i][j] + ".png"));//读取图片image.setBounds(100 + 202 * j, 100 + 112 * i, 200, 110);getContentPane().add(image);//将图片放入窗口}JLabel background = new JLabel(new ImageIcon("D:\\New_Study\\JAVA\\gameJAVA\\background.png"));//放入背景background.setBounds(100, 100, 604, 334);//后放的图片放在下面getContentPane().add(background);JLabel scoreshow = new JLabel("步数: " + count);scoreshow.setBounds(100, 80, 100, 20);getContentPane().add(scoreshow);//装入窗口getContentPane().repaint();//刷新界面}@Overridepublic void keyTyped(KeyEvent e) {}//较多按键监视不到@Override/*按键被按下*/public void keyPressed(KeyEvent e) {// TODO Auto-generated method stubint keycode = e.getKeyCode();if(keycode == 87) {//上MoveUp();}else if(keycode == 83) {//下 MoveDown();}else if(keycode == 65) {//左 MoveLeft();}else if(keycode == 68) {//右 MoveRight();}else { } } @Override/*按键松开*/public void keyReleased(KeyEvent e) {// TODO Auto-generated method stub}public void MoveLeft() {// TODO Auto-generated method stubif(fy + 1 >= 0 && fy + 1 < datas.length) {swap(fx, fy, fx, fy + 1);pantview();//重新绘制界面 }}public void MoveRight() {// TODO Auto-generated method stubif(fy - 1 >= 0 && fy - 1 < datas.length) {swap(fx, fy, fx, fy - 1);pantview();//重新绘制界面 }}public void MoveDown(){// TODO Auto-generated method stub if(fx - 1 >= 0 && fx - 1 < datas.length) {swap(fx, fy, fx - 1, fy);pantview();//重新绘制界面 }}public void MoveUp() {// TODO Auto-generated method stubif(fx + 1 >= 0 && fx + 1 < datas.length) { swap(fx, fy, fx + 1, fy); pantview();//重新绘制界面 }}public void swap(int xx, int yy, int x, int y) {int temp = datas[xx][yy];datas[xx][yy] = datas[x][y];datas[x][y] = temp; fx = x;fy = y;}@Override/*监听菜单*/public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource() == item1) {selfdo = true;//再次完成拼图会出现成功画面changeAll();count = -1;pantview();}else if(e.getSource() == item2) {selfdo = false;//再次完成拼图不会出现成功画面recoverAll();count = -1;pantview();}}public void changeAll() {int len = datas.length * datas.length;int nums[] = new int[len];nums[len - 1] = 0;int x = 1;for(int i = 0; i < len - 1; i ++) nums[i] = x ++;Random r = new Random();//产生随机数for(int j = 0; j < len - 1; j ++) {//最后一位不做处理int index = r.nextInt(len - 1);//[0 ~ len - 1)随机数int temp = nums[j];nums[j] = nums[index];nums[index] = temp;}int length = (int)Math.abs(Math.sqrt(len));for(int i = 0; i < len; i++) datas[i / length][i % length] = nums[i];//重新记录空格位置fx = length - 1;fy = length - 1;}public void recoverAll() {int len = datas.length;datas[len - 1][len - 1] = 0;int x = 1;for(int i = 0; i < len; i ++)for(int j = 0; j < len; j ++) datas[i][j] = x ++;//记录空格位置fx = len - 1;fy = len - 1;}public boolean checkVictory() {int x = 1;int len = datas.length;for(int i = 0; i < len; i ++)for(int j = 0; j < len; j ++) {if(i == len - 1 && j == len - 1) return true;//最后一个不用检查if(datas[i][j] != x ++) return false;}return true;}public void pantVictory(boolean whodo) {if(whodo) {if(count <= 50) path = "A";else if(count <= 100) path = "B";else if(count <= 500) path = "C";else path = "D";System.out.println("成功");JLabel victory = new JLabel(new ImageIcon("D:\\New_Study\\JAVA\\gameJAVA\\"+ path +"_victory.jpg"));//成功画面victory.setBounds(252, 117, 300, 300);getContentPane().add(victory);//写入画框selfdo = false;}}}
调用的文件图片:
图片下载:
百度网盘:
https://pan.baidu.com/s/1ELlMZXXSqGCE8Dsyl1ETNQ” />
移动:通过WASD移动
打乱功能效果:
复原功能效果:
成功功能效果:
根据步数不同效果分别为: