本文主要是介绍JavaSE石头迷阵,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
根据黑马程序员javase课程编写的,如有错误,欢迎指正
先创建MineFrame类继承JFrame,实现鼠标监听和键盘监听
package com.liu;import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;public class MineFrame extends JFrame implements KeyListener, ActionListener {/***无参构造*/public MineFrame(){initFrame();initDatas();initMenu();paintView();//添加键盘监听this.addKeyListener(this);setVisible(true);}int[][] datas=new int[4][4];int x0;int y0;JMenuItem item_new = new JMenuItem("重新游戏");JMenuItem item_new2 = new JMenuItem("作弊!!");/*** 初始化窗体*/public void initFrame(){// 设置宽和高setSize(514, 595);// 设置窗体居中setLocationRelativeTo(null);// 设置窗体置顶setAlwaysOnTop(true);// 设置关闭模式setDefaultCloseOperation(3);// 设置窗体标题setTitle("石头迷阵");// 取消默认布局setLayout(null);}/*** 绘制窗体样式*/public void paintView(){getContentPane().removeAll();//清除绘制的窗口if(victory()){JLabel win=new JLabel(new ImageIcon("D:\\WebText\\heima资料\\day5\\资料\\石头迷阵image\\win.png"));win.setBounds(124,230,266,88);getContentPane().add(win);}for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {JLabel imag = new JLabel(new ImageIcon("D:\\WebText\\heima资料\\day5\\资料\\石头迷阵image\\"+datas[i][j]+".png"));imag.setBounds(50+100*j, 90+100*i, 100, 100);getContentPane().add(imag);}}JLabel background = new JLabel(new ImageIcon("D:\\WebText\\heima资料\\day5\\资料\\石头迷阵image\\background.png"));background.setBounds(20, 20, 450, 484);getContentPane().add(background);getContentPane().repaint();}/*** 初始化菜单*/private void initMenu() {JMenuBar menuBar = new JMenuBar();JMenu menu = new JMenu("功能");item_new.addActionListener(this);item_new2.addActionListener(this);menu.add(item_new);menu.add(item_new2);menuBar.add(menu);setJMenuBar(menuBar);}/*** 初始化数组,输出还没删除!!!!!!*/public void initDatas(){//打乱顺序Random random=new Random();int num[]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};for (int i = 0; i <16 ; i++) {int index=random.nextInt(num.length);int temp=num[i];num[i]=num[index];num[index]=temp;}for (int i = 0; i <16 ; i++) {datas[i/4][i%4]=num[i];}for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if(datas[i][j]==0){x0=i;y0=j;}//System.out.print(datas[i][j]+" ");}//System.out.println();}//System.out.println(x0+" "+y0);}/*** 键盘按下*/@Overridepublic void keyPressed(KeyEvent e) {int action=e.getKeyCode();if(action==37){//左键if(y0!=3){moveLeft();}else {return;}}else if(action==38){//上键if(x0!=3){moveUp();}else {return;}}else if(action==39){//右键if(y0!=0){moveRight();}else {return;}}else if(action==40){//下键if(x0!=0){moveDown();}else {return;}}else {return;}// 每一次移动的逻辑执行完, 都要去调用victory方法, 检查游戏是否是失败的状态.victory();// 每一次移动完成, 重新绘制界面paintView();}/*** 左移*/public void moveLeft(){datas[x0][y0]=datas[x0][y0+1];datas[x0][y0+1]=0;y0++;}/*** 上移*/public void moveUp(){datas[x0][y0]=datas[x0+1][y0];datas[x0+1][y0]=0;x0++;}/*** 右移*/public void moveRight(){datas[x0][y0]=datas[x0][y0-1];datas[x0][y0-1]=0;y0--;}/***下移*/public void moveDown(){datas[x0][y0]=datas[x0-1][y0];datas[x0-1][y0]=0;x0--;}/*** 检查成功*/public boolean victory(){//1.创建一个新数组,如果和移动后的数组一样,那么就是成功!int[][] newArr={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};for (int i = 0; i < datas.length; i++) {for (int j = 0; j < datas[i].length; j++) {if(datas[i][j]!=newArr[i][j]){return false;}}}return true;}/*** 游戏作弊码*/public void cheat(){//点击就把数组换成顺序好的int[][] newArr={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};datas=newArr;}/*** 无视*/@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyReleased(KeyEvent e) {}/*** 用于鼠标点击,获得点击的对象!*/@Overridepublic void actionPerformed(ActionEvent e) {if(e.getSource()==item_new){initDatas();paintView();}else if(e.getSource()==item_new2){datas = new int[][]{{1, 2, 3, 4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 0}};victory();paintView();}else {return;}}
}
运行的类:
package com.liu;public class Ipml {public static void main(String[] args) {new MineFrame();}
}
运行结果:
可点击重新游戏,游戏将会随机打乱顺序
点击作弊,游戏自动胜利
这篇关于JavaSE石头迷阵的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!