拼图小游戏源码来了~!(可做课程设计,把好基友图片放上去恶搞好基友,没事也可以自己玩玩)

 拿代码的小伙伴别忘了三练,创作不易,需要现成图片的话关注私聊我就可!!!!!!

目录

主程序入口

创建图形化界面类

 图片路径举例

运行结果展示

 规则说明


主程序入口

(目前只有直接玩游戏界面,登录注册界面到数据库时更新)

import ui.Gamedenglu;
import ui.Gamejiemian;
import ui.Gamezhuce;

public class app {
    public static void main(String[] args) {


new Gamejiemian();

//new Gamedenglu();

//new Gamezhuce();
    }
}

创建图形化界面类

(注意:里面路径需自己手动根据自己图片所在路径更改,图片可自己添加,建议小伙伴有一定基础能知道图片路径添加到呢,上面也有注释,胜利图片和背景图片都可以网上寻找,我的就是找的,为了不侵权我就不找了)

package ui;

import com.sun.nio.sctp.AbstractNotificationHandler;

import javax.sql.rowset.JdbcRowSet;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.beans.EventHandler;
import java.nio.file.Path;
import java.util.Random;

public class Gamejiemian extends JFrame implements KeyListener, ActionListener {
    //产生随机数
    Random r = new Random();

    //定义变量计算步数
    int count = 0;

    //管理数据
    int[][] arr2 = new int[4][4];

    //图片路径
    int a1=r.nextInt(1,14);
    String str = "C:\\Users\\86199\\IdeaProjects\\puzzlegame\\image\\girl\\girl"+a1+"\\";

    //定义一个正确数组判断胜利
    int[][] win = {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12},
            {13, 14, 15, 0}
    };

    //用x,y来记录空白区的值
    int x = 0;
    int y = 0;

    //条目设置为成员方法以便方法之间调用
    JMenu jMenu0 = new JMenu("更换图片");
    JMenuItem jMenuItem1 = new JMenuItem("重新游戏");
    JMenuItem jMenuItem2 = new JMenuItem("重新登陆");
    JMenuItem jMenuItem3 = new JMenuItem("关闭游戏");
    JMenuItem jMenuItem4 = new JMenuItem("动物");
    JMenuItem jMenuItem5 = new JMenuItem("girl");
    JMenuItem jMenuItem6 = new JMenuItem("运动");

    JMenuItem jMenuItem7 = new JMenuItem("没事别点");


    //判断是否胜利
    public boolean pandaun() {
        for (int i = 0; i < arr2.length; i++) {
            for (int j = 0; j < arr2[i].length; j++) {
                if (arr2[i][j] != win[i][j]) {
                    return false;
                }

            }
        }
        return true;
    }

    //初始化界面
    public Gamejiemian() {
        initJFrame();

        //初始化目录
        initJMenuBer();

        //初始化数据(打乱图片)
        initdaluan();

        //初始化图片
        tupian();

        //可视化窗口,写最后
        this.setVisible(true);
    }

    private void initdaluan() {
        //定义一个一维数组并随机添加到二维数组里面
        int[] arr = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        Random r = new Random();
        for (int i = 0; i < arr.length; i++) {
            int sui1 = r.nextInt(16);
            int temp = arr[i];
            arr[i] = arr[sui1];
            arr[sui1] = temp;
        }
//        for (int i = 0; i < arr.length; i++) {
//            System.out.print(arr[i] + " ");
//        }
//        System.out.println();
        //方法一
//        int count = 0;
//        for (int i = 0; i < arr2.length; i++) {
//            for (int j = 0; j < arr2.length; j++) {
//                arr2[i][j] = arr[count];
//                count++;
//            }
//        }
        //方法二
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == 0) {
                x = i / 4;
                y = i % 4;
            }
            arr2[i / 4][i % 4] = arr[i];
        }
//        for (int i = 0; i < arr2.length; i++) {
//            for (int n = 0; n < arr2.length; n++) {
//                System.out.print(arr2[i][n] + " ");
//            }
//            System.out.println();
//
//        }
//
    }

    private void tupian() {
        //删除已经出现的图片,为新图片载入让出空间
        this.getContentPane().removeAll();

        //进行判断是否胜利
        if (pandaun()) {
            //打印胜利图案
            JLabel winjlabel = new JLabel(new ImageIcon("C:\\Users\\86199\\IdeaProjects\\puzzlegame\\image\\win.png"));
            winjlabel.setBounds(203, 283, 197, 73);
            this.getContentPane().add(winjlabel);
        }

        //插入步数组件
        JLabel bushu = new JLabel("步数:" + count);
        bushu.setBounds(50, 30, 100, 20);
        this.getContentPane().add(bushu);

        //先加载的图片在上方,后加载的在下方
        for (int i = 0; i < 4; i++) {
            for (int i1 = 0; i1 < 4; i1++) {
                int number = arr2[i][i1];
                //合并到了jlabel里面new ImageIcon("C:\\Users\\86199\\IdeaProjects\\puzzlegame\\image\\animal\\animal5\\1.jpg")
                //创建图片对象
                //创建管理容器对象
                JLabel jLabel = new JLabel(new ImageIcon(str + number + ".jpg"));
                //添加之前指定位置
                jLabel.setBounds(i1 * 105 + 83, i * 105 + 134, 105, 105);
                //把容器添加到界面
                //this.add(jLabel);
                //添加边框
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));
                this.getContentPane().add(jLabel);
                //刷新图片
                this.getContentPane().repaint();
            }
        }
        //后添加的在最下面
        //背景图片
        JLabel beijing = new JLabel(new ImageIcon("C:\\Users\\86199\\IdeaProjects\\puzzlegame\\image\\background.png"));
        beijing.setBounds(40, 40, 508, 560);
        this.getContentPane().add(beijing);

    }

    private void initJMenuBer() {
        //初始化菜单,创建菜单对象
        JMenuBar jMenuBar = new JMenuBar();
        //创建菜单选项对象
        JMenu jMenu1 = new JMenu("功能");
        JMenu jMenu2 = new JMenu("关于我们");
        //创建下面的条目对象
//        JMenuItem jMenuItem1 = new JMenuItem("重新游戏");
//        JMenuItem jMenuItem2 = new JMenuItem("重新登陆");
//        JMenuItem jMenuItem3 = new JMenuItem("关闭游戏");
//
//        JMenuItem jMenuItem7 = new JMenuItem("没事别点");
        //添加进各个条目
        jMenu1.add(jMenu0);
        jMenu1.add(jMenuItem1);
        jMenu1.add(jMenuItem2);
        jMenu1.add(jMenuItem3);
        jMenu0.add(jMenuItem4);
        jMenu0.add(jMenuItem5);
        jMenu0.add(jMenuItem6);

        jMenu2.add(jMenuItem7);

        //添加到最大条目
        jMenuBar.add(jMenu1);
        jMenuBar.add(jMenu2);

        //给每个条目添加事件
        jMenuItem1.addActionListener(this);
        jMenuItem2.addActionListener(this);
        jMenuItem3.addActionListener(this);
        jMenuItem4.addActionListener(this);
        jMenuItem5.addActionListener(this);
        jMenuItem6.addActionListener(this);
        jMenuItem7.addActionListener(this);

        //给整个方法设置菜单
        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        //设置界面宽高
        this.setSize(603, 680);
        //设置界面标题
        this.setTitle("拼图单机版 V1.0");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //界面居中
        this.setLocationRelativeTo(null);
        //设置游戏关闭模式
        this.setDefaultCloseOperation(3);
        //取消默认居中
        this.setLayout(null);


        //设置监听(上下左右走)
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
//按下不松调用
        int code = e.getKeyCode();
        if (code == 65) {
            this.getContentPane().removeAll();
        }
        ;
        //加载完整图片
        JLabel wanzheng = new JLabel(new ImageIcon(str + "all.jpg"));
        wanzheng.setBounds(83, 134, 420, 420);
        this.getContentPane().add(wanzheng);
        this.getContentPane().add(wanzheng);

        //加载背景图片
        JLabel beijing = new JLabel(new ImageIcon("C:\\Users\\86199\\IdeaProjects\\puzzlegame\\image\\background.png"));
        beijing.setBounds(40, 40, 508, 560);
        this.getContentPane().add(beijing);
        this.getContentPane().add(beijing);
//刷新界面
        this.getContentPane().repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if (pandaun()) {
            return;
        }
        //该方法对上下左右进行判断
        //左上右下对应:37,38,39,40
        int code = e.getKeyCode();
        if (code == 37) {
            if (y == 3) {
                return;
            }
            System.out.println("向左移动");
            //空白方块向右移动
            arr2[x][y] = arr2[x][y + 1];
            arr2[x][y + 1] = 0;
            y++;
            count++;
            tupian();

        }
        if (code == 38) {
            if (x == 3) {
                return;
            }

            System.out.println("向上移动");
            //空白方块向下移动
            arr2[x][y] = arr2[x + 1][y];
            arr2[x + 1][y] = 0;
            x++;
            count++;
            tupian();
        }
        if (code == 39) {
            if (y == 0) {
                return;
            }

            System.out.println("向右移动");
            arr2[x][y] = arr2[x][y - 1];
            arr2[x][y - 1] = 0;
            y--;
            count++;
            tupian();

        }
        if (code == 40) {
            if (x == 0) {
                return;
            }

            //空白方块向上移动
            System.out.println("向下移动");
            arr2[x][y] = arr2[x - 1][y];
            arr2[x - 1][y] = 0;
            x--;
            count++;
            tupian();
        }
        if (code == 65) {
            tupian();
        }
        if (code == 87) {
            arr2 = new int[][]{
                    {1, 2, 3, 4},
                    {5, 6, 7, 8},
                    {9, 10, 11, 12},
                    {13, 14, 15, 0}
            };
            count++;
            tupian();

        }
    }


    //鼠标监听
    @Override
    public void actionPerformed(ActionEvent e) {
//获取当前被点击条目对象
        Object obj = e.getSource();
//开始判断执行哪个条目
        if (obj == jMenuItem1) {
            System.out.println("重新游戏");
            //打乱数据
            initdaluan();
            //计步器清零
            count = 0;
            //重新加载图片
            tupian();

        }
        if (obj == jMenuItem2) {
            System.out.println("重新登录");
            this.setVisible(false);
            new Gamedenglu();
        }
        if (obj == jMenuItem3) {
            System.out.println("退出游戏");
            System.exit(0);
        }
        if (obj == jMenuItem7) {
            int a=r.nextInt(1,7);
            System.out.println("没事别点");
            //弹框对象
            JDialog jDialog = new JDialog();

            //界面容器
            JLabel jLabel = new JLabel(new ImageIcon("C:\\Users\\86199\\IdeaProjects\\puzzlegame\\image\\没事别点"+a+".jpg"));
            //设置位置,宽高
            jLabel.setBounds(0, 0, 400, 533);
            //添加容器到弹框
            jDialog.getContentPane().add(jLabel);
            //弹框设置大小
            jDialog.setSize(500, 600);
            //界面置顶居中
            jDialog.setAlwaysOnTop(true);
            jDialog.setLocationRelativeTo(null);
            //不关闭无法进行其他操作
            jDialog.setModal(true);
            //显示出来
            jDialog.setVisible(true);
        }
        if (obj == jMenuItem4) {
            System.out.println("动物");
            int a = r.nextInt(1, 9);
            str = "C:\\Users\\86199\\IdeaProjects\\puzzlegame\\image\\animal\\animal" + a + "\\";
            //打乱数据
            initdaluan();
            //计步器清零
            count = 0;
            //重新加载图片
            tupian();
        }
        if (obj == jMenuItem5) {
            System.out.println("美女");
            int a = r.nextInt(1, 14);
            str = "C:\\Users\\86199\\IdeaProjects\\puzzlegame\\image\\girl\\girl" + a + "\\";
            //打乱数据
            initdaluan();
            //计步器清零
            count = 0;
            //重新加载图片
            tupian();

        }
        if (obj == jMenuItem6) {
            System.out.println("运动");
            int a = r.nextInt(1, 11);
            str = "C:\\Users\\86199\\IdeaProjects\\puzzlegame\\image\\sport\\sport" + a + "\\";
            //打乱数据
            initdaluan();
            //计步器清零
            count = 0;
            //重新加载图片
            tupian();

        }
    }
}

 图片路径举例

(上面的15张jpg其实是一张完整图片切割成十六张105*105的小图片,少一张不添加当做空白用于当做移动空间)

拼图小游戏源码来了~!(可做课程设计,把好基友图片放上去恶搞好基友,没事也可以自己玩玩)_第1张图片

运行结果展示

拼图小游戏源码来了~!(可做课程设计,把好基友图片放上去恶搞好基友,没事也可以自己玩玩)_第2张图片

 规则说明

1.按A是查看拼好的图片

2.按W是一键通关

3.上面选项可以肆意更换自己所添加的图片

4.关于我们也可以设置成点击后自己想要显示的图片

加油各位,留个一键三连,让我们顶峰相见吧吧吧~!!!

你可能感兴趣的:(课程设计)