java--拼图游戏

2024-01-14 19:10
文章标签 java 拼图游戏

本文主要是介绍java--拼图游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、项目介绍

1、登录界面

2、游戏界面

 3、注册界面

二、代码实现

1、工具类

2、用户pojo类

3、登录界面

4、游戏界面

5、注册界面

6、启动类

三、源码获取


一、项目介绍

1、登录界面

登录界面有需要填写用户名、密码、验证码

2、游戏界面

游戏界面通过上下左右键进行拼图,上方菜单栏可以切换图片、重新开始

有隐藏键:按w可以直接胜利,长按A键可以查看完整图片

 3、注册界面

注册界面需要填写用户,密码,再次确认密码,两次密码需要一致

点击重置按键清空填写框,重新填写

二、代码实现

1、工具类

分析

        因为有一些功能在其他类中需要重复用到,所以写了个工具类,用来存放一些公共方法

arrUtil.java

package com.lwj.util;import com.lwj.pojo.User;import javax.swing.*;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Random;public class arrUtil {//打乱图片public int[][] arrChange(){int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,0};int[][] newArr = new int[4][4];Random random = new Random();// 打乱一维数组for (int i = 0; i < arr.length; i++) {//生成随机索引int index = random.nextInt(arr.length);int temp = arr[i];arr[i] = arr[index];arr[index] = temp;}//将一维数组赋值给二维数组int index = 0;for (int i = 0; i < newArr.length; i++) {for (int j = 0; j < newArr[i].length; j++) {newArr[i][j] = arr[index];index++;}}return newArr;}//判断是否胜利public boolean victory(int[][] arr){//定义胜利的数组int[][] win = {{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,0}};//判断两个数组是否一样for (int i = 0; i < win.length; i++) {for (int j = 0; j < win[i].length; j++) {if (win[i][j] != arr[i][j]){return false;}}}return true;}//随机路径public String changePath(){String[] str = {"animal","girl","sport"};Random random = new Random();String path = "";int index = random.nextInt(3);if (index == 0){int i = random.nextInt(8) + 1;path = str[0]+"\\"+"\\"+str[0]+i+"\\\\";} else if (index == 1){int i = random.nextInt(13) + 1;path = str[1]+"\\"+"\\"+str[1]+i+"\\\\";} else if (index == 2){int i = random.nextInt(10) + 1;path = str[2]+"\\"+"\\"+str[2]+i+"\\\\";}return path;}//生成验证码public String createCode(){String[] arr = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m"};String code = "";Random random = new Random();for (int i = 0; i < 4; i++) {code += arr[random.nextInt(26)];}code+= random.nextInt(10);return code;}//弹窗提示方法public void showJDialog(){JDialog jDialog = new JDialog();jDialog.setSize(200,150);jDialog.setAlwaysOnTop(true);jDialog.setLocationRelativeTo(null);jDialog.setModal(true);JLabel jLabel = new JLabel();jLabel.setText("输入错误");jLabel.setBounds(0,0,200,150);jDialog.getContentPane().add(jLabel);jDialog.setVisible(true);}
}

2、用户pojo类

分析

        存放用户信息的javabean类

User.java

package com.lwj.pojo;public class User {private String name;private String password;public User() {}public User(String name, String password) {this.name = name;this.password = password;}/*** 获取* @return name*/public String getName() {return name;}/*** 设置* @param name*/public void setName(String name) {this.name = name;}/*** 获取* @return password*/public String getPassword() {return password;}/*** 设置* @param password*/public void setPassword(String password) {this.password = password;}public String toString() {return "User{name = " + name + ", password = " + password + "}";}
}

        

3、登录界面

分析

        其实就是添加图片、文本输入框、按钮,设置按钮事件,点击登录获取文本框的按钮,在对已有用户进行比对,在账号和密码都正确的情况下要跳转到游戏界面

登录界面代码

LoginJFrame.java

package com.lwj.ui;import com.lwj.pojo.User;
import com.lwj.util.arrUtil;import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;/*** 登录界面*/
public class LoginJFrame extends JFrame implements ActionListener , KeyListener {public LoginJFrame(){initJFrame();initImage();}//设置窗体private void initJFrame() {//设置窗体大小this.setSize(480,440);//设置界面标题this.setTitle("登录界面");//设置界面置顶this.setAlwaysOnTop(true);//设置窗口居中this.setLocationRelativeTo(null);//设置默认关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗体可见this.setVisible(true);}//创建用户输入框对象JTextField userText = new JTextField();//创建密码输入框JPasswordField passwordText = new JPasswordField();//创建验证码输入框JTextField codeText = new JTextField();//创建注册按钮JButton registerButton = new JButton();//创建登录按钮JButton loginButton = new JButton();//存放验证码String create = "";//创建工具类对象arrUtil  arrutil = new arrUtil();//创建数组存放用户static ArrayList<User> userList = new ArrayList<>();static {userList.add(new User("林","123456"));userList.add(new User("沈敬斌","123456"));}public ArrayList<User> reUser(){return userList;}public void setUser(String username,String password){userList.add(new User(username,password));}private void initImage() {//清空原本已经出现的图片this.getContentPane().removeAll();//添加用户图片JLabel userImage = new JLabel(new ImageIcon("puzzGame\\image\\login\\username.png"));userImage.setBounds(150,150,47,17);this.add(userImage);//添加用户输入框userText.setBounds(210,150,100,18);this.add(userText);//添加密码图片JLabel passImage = new JLabel(new ImageIcon("puzzGame\\image\\login\\pass.png"));passImage.setBounds(158,200,32,16);this.add(passImage);//添加密码输入框passwordText.setBounds(210,200,100,18);this.add(passwordText);//添加验证码图片JLabel codeImage = new JLabel(new ImageIcon("puzzGame\\image\\login\\code.png"));codeImage.setBounds(150,250,56,21);this.add(codeImage);//添加验证码输入框codeText.setBounds(210,250,100,18);this.add(codeText);//生成验证码JLabel code = new JLabel();code.setBounds(320,245,50,30);create = arrutil.createCode();code.setText(create);System.out.println(create);this.add(code);//添加登录按钮loginButton.setBounds(180,300,50,30);loginButton.setIcon(new ImageIcon("puzzGame\\image\\login\\loginbutton.png"));loginButton.addActionListener(this);this.add(loginButton);//添加注册按钮registerButton.setBounds(260,300,50,30);registerButton.setIcon(new ImageIcon("puzzGame\\image\\login\\registerButton.png"));registerButton.addActionListener(this);this.add(registerButton);//添加背景图片JLabel backround = new JLabel(new ImageIcon("puzzGame\\image\\register\\background.png"));backround.setBounds(0,0,470,390);this.add(backround);//刷新页面this.getContentPane().repaint();}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == loginButton){String username = userText.getText();String password = passwordText.getText();String code = codeText.getText();boolean flag = checkUser(userList, username, password, code);if (flag){new GameJFrame();}  else {arrutil.showJDialog();initImage();}} else if (e.getSource() == registerButton){new RegisterJFrame();this.setVisible(false);System.out.println("注册");}}@Overridepublic void keyTyped(KeyEvent e) {}@Overridepublic void keyPressed(KeyEvent e) {}@Overridepublic void keyReleased(KeyEvent e) {}//判断用户名密码是否正确public boolean checkUser(ArrayList<User> userList,String username,String password,String code) {for (int i = 0; i < userList.size(); i++) {if (username.equals(userList.get(i).getName())) {System.out.println("用户名正确");if (password.equals(userList.get(i).getPassword())) {System.out.println("密码正确");if (code.equals(create)) {return true;}}}}return false;}}

4、游戏界面

分析

  • 拼图部分,其实是15张图片,将其改成对应名字,再通过数组的方式打乱,通过事件,再点击键盘的时候,交换索引来实现图片移动

  • 查看完整图片功能,就是按A键在页面中找出完整图片

  • w键直接胜利,通过对数组排序来实现完成图片

游戏界面代码

GameJFrame.java

package com.lwj.ui;import com.lwj.util.arrUtil;import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;/*** 游戏界面*/
public class GameJFrame extends JFrame implements KeyListener , ActionListener {public GameJFrame(){//初始化界面initJFrame();//初始化菜单initJMenuBar();//初始化图片initImage();//设置窗体可见this.setVisible(true);}//记录空白方块位置int x = 0;int y = 0;//记录图片路径String path = "puzzGame\\image\\animal\\animal3\\";//随机图片记录String changeimage = "";String background = "puzzGame\\image\\background.png";//调用工具类方法,打乱数组arrUtil arrutil = new arrUtil();int[][] ints = arrutil.arrChange();//判断游戏是否胜利标记boolean flag = false;//统计步数int step = 0;private void initImage() {//清空原本已经出现的图片this.getContentPane().removeAll();flag = arrutil.victory(ints);if (flag){//显示胜利图片JLabel winJLabel = new JLabel(new ImageIcon("puzzGame\\image\\win.png"));winJLabel.setBounds(203,283,197,73);this.getContentPane().add(winJLabel);}JLabel stepCount = new JLabel("步数"+step);stepCount.setBounds(50,30,100,20);this.getContentPane().add(stepCount);//双重循序添加图片for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {//创建一个图片对象,将数组进行图片循环ImageIcon icon = new ImageIcon(path+ints[i][j]+".jpg");//创建Jlabel对象JLabel jLabel = new JLabel(icon);//指定图片位置jLabel.setBounds(105*j+83,105*i+134, 105, 105);//给图片添加边框jLabel.setBorder(new BevelBorder(BevelBorder.RAISED));this.setLayout(null);this.getContentPane().add(jLabel);//记录空白方块位置if (ints[i][j] == 0){x = i;y = j;}//刷新页面this.getContentPane().repaint();}}JLabel backround = new JLabel(new ImageIcon(background));backround.setBounds(40,40,508,560);this.add(backround);}//创建条目对象JMenuItem replayItem = new JMenuItem("重新游戏");JMenuItem reLoginItem = new JMenuItem("重新登录");JMenuItem closeItem = new JMenuItem("关闭游戏");JMenuItem changeImage = new JMenuItem("切换图片");JMenuItem accountItem = new JMenuItem("公众号");private void initJMenuBar() {/*** 初始化菜单* 1、创建整个菜单对象* 2、创建选项对象* 3、创建选项条目对象* 4、组合,将条目对象放在选项中,选项放进菜单*///创建菜单对象JMenuBar jMenuBar = new JMenuBar();//创建菜单选项JMenu functionJMenu = new JMenu("功能");JMenu aboutJMenu = new JMenu("关于我们");//条目对象添加进选项functionJMenu.add(replayItem);functionJMenu.add(reLoginItem);functionJMenu.add(changeImage);functionJMenu.add(closeItem);aboutJMenu.add(accountItem);//给条目绑定事件replayItem.addActionListener(this);reLoginItem.addActionListener(this);closeItem.addActionListener(this);accountItem.addActionListener(this);changeImage.addActionListener(this);//菜单选项添加进菜单jMenuBar.add(functionJMenu);jMenuBar.add(aboutJMenu);//设置整个界面this.setJMenuBar(jMenuBar);}private void initJFrame() {//设置窗体大小this.setSize(603,680);//设置界面标题this.setTitle("拼图单机版");//设置界面置顶this.setAlwaysOnTop(true);//设置窗口居中this.setLocationRelativeTo(null);//设置默认关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//给整个界面添加键盘监听事件this.addKeyListener(this);}@Overridepublic void keyTyped(KeyEvent e) {}//按下不松调用方法@Overridepublic void keyPressed(KeyEvent e) {if (e.getKeyCode() == 65){//把界面中所有图片全部删除this.getContentPane().removeAll();//加载完整图片ImageIcon icon = new ImageIcon(path+"all.jpg");JLabel jLabel = new JLabel(icon);jLabel.setBounds(83,134,420,420);this.setLayout(null);this.getContentPane().add(jLabel);//添加背景图片JLabel backround = new JLabel(new ImageIcon(background));backround.setBounds(40,40,508,560);this.add(backround);//刷新页面this.getContentPane().repaint();}}@Overridepublic void keyReleased(KeyEvent e) {//判断游戏是否胜利,胜利结束该方法if (flag){return;}/***键盘上下左右判断* 左:37,右:39    上:38,下:40*/int code = e.getKeyCode();if (code == 37){System.out.println("向左移动");if (y != 3){//把空白方块下方的数字赋值给空白方块ints[x][y] = ints[x][y+1];ints[x][y+1] = 0;y++;//步数增step++;//调用方法展示最新数据initImage();}} else if (code == 38){System.out.println("向上移动");if (x != 3){//把空白方块下方的数字赋值给空白方块ints[x][y] = ints[x+1][y];ints[x+1][y] = 0;x++;//步数增step++;//调用方法展示最新数据initImage();}} else if (code == 39){System.out.println("向右移动");if (y != 0){//把空白方块下方的数字赋值给空白方块ints[x][y] = ints[x][y-1];ints[x][y-1] = 0;y--;//步数增step++;//调用方法展示最新数据initImage();}} else if (code == 40){System.out.println("向下移动");if (x != 0){//把空白方块下方的数字赋值给空白方块ints[x][y] = ints[x-1][y];ints[x-1][y] = 0;x--;//步数增step++;//调用方法展示最新数据initImage();}} else if (code == 65){initImage();} else if (code == 87){//按下w键直接获胜ints = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};initImage();}}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == replayItem){System.out.println("重新游戏");//打乱数组数据ints = arrutil.arrChange();//计数器清零step = 0;//重新加载图initImage();} else if (e.getSource() == reLoginItem) {System.out.println("重新登录");this.setVisible(false);new LoginJFrame();} else if (e.getSource() == closeItem) {System.out.println("关闭游戏");//直接停止虚拟机System.exit(0);} else if (e.getSource() == accountItem) {System.out.println("公众号");//创建弹框对象JDialog jDialog = new JDialog();//创建容器对象JLabel jLabel = new JLabel(new ImageIcon("puzzGame\\image\\my.jpg"));//设置位置jLabel.setBounds(0,0,258,258);jDialog.getContentPane().add(jLabel);jDialog.setSize(344,344);//设置置顶居中jDialog.setAlwaysOnTop(true);jDialog.setLocationRelativeTo(null);//弹框不关闭则无法操作页面jDialog.setModal(true);jDialog.setVisible(true);} else if (e.getSource() == changeImage) {String newPath = arrutil.changePath();path = "puzzGame\\\\image\\\\"+newPath;System.out.println(path);//打乱数组ints = arrutil.arrChange();step = 0;//重新加载图initImage();}}}

5、注册界面

分析

添加图片、文本框、按钮,通过按钮来实现事件,获取到文本框的文本,再将用户名跟现有的用户进行比对,如果存在则注册失败,不存在再进行二次密码确认,注册成功跳转到登录界面

注册界面代码

RegisterJFrame.java

package com.lwj.ui;import com.lwj.pojo.User;
import com.lwj.util.arrUtil;import javax.swing.*;import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;import static com.lwj.ui.LoginJFrame.*;/*** 注册界面*/
public class RegisterJFrame extends JFrame implements ActionListener {public RegisterJFrame(){initJFrmae();initImage();}//创建注册用户对象JTextField registerUsername = new JTextField();//创建密码输入框对象JPasswordField passwordText = new JPasswordField();//创建再次输入密码对象JPasswordField rePasswordText = new JPasswordField();//注册对象JButton registerButton = new JButton();//重置对象JButton rebootButton = new JButton();//添加图片private void initImage() {//清空原本已经出现的图片this.getContentPane().removeAll();//添加用户图片JLabel userImage = new JLabel(new ImageIcon("puzzGame\\image\\register\\registername.png"));userImage.setBounds(125,150,79,17);this.add(userImage);//添加用户输入框registerUsername.setBounds(210,150,100,18);this.add(registerUsername);//添加密码图片JLabel passwordImage = new JLabel(new ImageIcon("puzzGame\\image\\register\\password.png"));passwordImage.setBounds(130,200,64,16);this.add(passwordImage);//添加密码输入框passwordText.setBounds(210,200,100,18);this.add(passwordText);//添加再次密码图片JLabel repasswordImage = new JLabel(new ImageIcon("puzzGame\\image\\register\\repassowrd.png"));repasswordImage.setBounds(110,250,96,17);this.add(repasswordImage);//添加密码输入框rePasswordText.setBounds(210,250,100,18);this.add(rePasswordText);//添加注册按钮registerButton.setBounds(180,300,50,30);registerButton.setIcon(new ImageIcon("puzzGame\\image\\register\\registerbutton.png"));registerButton.addActionListener(this);this.add(registerButton);rebootButton.setBounds(250,300,50,30);rebootButton.setIcon(new ImageIcon("puzzGame\\image\\register\\rebootButton.png"));rebootButton.addActionListener(this);this.add(rebootButton);//添加背景图片JLabel backround = new JLabel(new ImageIcon("puzzGame\\image\\register\\background.png"));backround.setBounds(0,0,470,390);this.add(backround);//刷新页面this.getContentPane().repaint();}//初始化窗体private void initJFrmae() {//设置窗体大小this.setSize(480,440);//设置界面标题this.setTitle("注册界面");//设置界面置顶this.setAlwaysOnTop(true);//设置窗口居中this.setLocationRelativeTo(null);//设置默认关闭模式this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置窗体可见this.setVisible(true);}//添加用户public static boolean addUser(String username,String password,String repassword){LoginJFrame jF = new LoginJFrame();ArrayList<User> users = jF.reUser();for (int i = 0; i < users.size(); i++) {if (username.equals(users.get(i).getName())){return false;}}if (!(password.equals(repassword))){return false;}jF.setUser(username,password);return true;}@Overridepublic void actionPerformed(ActionEvent e) {if (e.getSource() == registerButton){String username = registerUsername.getText();String password = passwordText.getText();String repassword = rePasswordText.getText();boolean flag = addUser(username, password, repassword);if (flag){new LoginJFrame();this.setVisible(false);} else {arrUtil util = new arrUtil();util.showJDialog();initImage();}} else if (e.getSource() == rebootButton){registerUsername.setText("");passwordText.setText("");rePasswordText.setText("");initImage();}}
}

6、启动类

添加main方法启动

 import com.lwj.ui.GameJFrame;import com.lwj.ui.LoginJFrame;import com.lwj.ui.RegisterJFrame;​public class App {public static void main(String[] args) {// 调用登录界面new LoginJFrame();​//调用游戏界面//        new GameJFrame();​//调用注册界面//        new RegisterJFrame();}}

三、源码获取

关注微信公众号:小林学编程,回复:拼图游戏

这篇关于java--拼图游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/606221

相关文章

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法