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

相关文章

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory

JAVA智听未来一站式有声阅读平台听书系统小程序源码

智听未来,一站式有声阅读平台听书系统 🌟&nbsp;开篇:遇见未来,从“智听”开始 在这个快节奏的时代,你是否渴望在忙碌的间隙,找到一片属于自己的宁静角落?是否梦想着能随时随地,沉浸在知识的海洋,或是故事的奇幻世界里?今天,就让我带你一起探索“智听未来”——这一站式有声阅读平台听书系统,它正悄悄改变着我们的阅读方式,让未来触手可及! 📚&nbsp;第一站:海量资源,应有尽有 走进“智听

在cscode中通过maven创建java项目

在cscode中创建java项目 可以通过博客完成maven的导入 建立maven项目 使用快捷键 Ctrl + Shift + P 建立一个 Maven 项目 1 Ctrl + Shift + P 打开输入框2 输入 "> java create"3 选择 maven4 选择 No Archetype5 输入 域名6 输入项目名称7 建立一个文件目录存放项目,文件名一般为项目名8 确定