jq + 面向对象实现拼图游戏
jq + 面向对象实现拼图游戏
知识点
- 拖拽事件
- es6面向对象
- jquery事件
- 效果图
  
html:
    <div class="wraper">
        <div class="btn">
            <button class="start">开始</button>
        </div>
        <div class="box"></div>
    </div>
css:
    * {
        margin: 0;
        padding: 0;
        list-style: none;
    }
    html,
    body {
        width: 100%;
        height: 100%;
        background: url('../img/bg_pic.jpg') no-repeat;
        background-size: 100% 100%;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
    }
    .wraper {
        width: 500px;
        height: 600px;
        position: relative;
    }
    .wraper .btn {
        text-align: center;
        line-height: 80px;
    }
    .wraper .btn button {
        width: 100px;
        height: 40px;
        background: yellow;
        border: none;
        outline: none;
        font-size: 14px;
        color: red;
        border-radius: 20px;
        cursor: pointer;
    }
    .wraper .box {
        width: 100%;
        height: 500px;
        position: relative;
        border: 10px solid red;
        border-radius: 10px;
    }
    .wraper .box .pic {
        position: absolute;
        background: url('../img/zy.jpg') no-repeat;
        box-shadow: 0 0 5px #fff;
        background-size: 500px 500px;
        cursor: pointer;
    }
js:
   class Game {
        constructor() {
            this.boxW = parseInt($('.box').css('width'));
            this.boxH = parseInt($('.box').css('height'));
            this.imgW = this.boxW / 5;
            this.imgH = this.boxH / 5;
            this.flag = true; //true为开始 false为重排
            this.orArr = []; //标准数组
            this.randArr = []; //乱序数组
            this.init();
        }
        init() {
            this.createDom();
            this.getState();
        }
        createDom() {
            //行
            for (var i = 0; i < 5; i++) {
                //列
                for (var j = 0; j < 5; j++) {
                    this.orArr.push(i * 5 + j);
                    let imgs = $("<div class='pic'></div>").css({
                        width: this.imgW + 'px',
                        height: this.imgH + 'px',
                        left: j * this.imgW + 'px',
                        top: i * this.imgH + 'px',
                        backgroundPosition: -j * this.imgW + 'px ' + -i * this.imgH + 'px'
                    });
                    $('.box').append(imgs);
                }
            }
        }
        getState() {
            let btn = $('.btn .start');
            let imgs = $('.pic');
            let _this = this;
            btn.on('click', function() {
                if (_this.flag) {
                    _this.flag = false;
                    btn.text('重排');
                    _this.getRandom();
                    _this.getOrder(_this.randArr);
                    imgs.on('mousedown', function(e) {
                        let index = $(this).index();
                        let left = e.pageX - imgs.eq(index).offset().left;
                        let top = e.pageY - imgs.eq(index).offset().top;
                        $(document).on('mousemove', function(e1) {
                            let left1 = e1.pageX - left - $('.box').offset().left - 10;
                            let top1 = e1.pageY - top - $('.box').offset().top - 10;
                            imgs.eq(index).css({
                                'z-index': '40',
                                'left': left1,
                                'top': top1
                            })
                        }).on('mouseup', function(e2) {
                            let left2 = e2.pageX - left - $('.box').offset().left - 10;
                            let top2 = e2.pageY - top - $('.box').offset().top - 10;
                            let index2 = _this.changeIndex(left2, top2, index);
                            if (index === index2) {
                                _this.picReturn(index);
                            } else {
                                _this.picChange(index, index2);
                            }
                            $(document).off('mousemove').off('mouseup').off('mousedown');
                        })
                        return false;
                    })
                } else {
                    _this.flag = true;
                    btn.text('开始');
                    _this.getOrder(_this.orArr);
                    imgs.off('mousemove').off('mouseup').off('mousedown');
                }
            })
        }
        changeIndex(left, top, index) {
            if (left < 0 || left > this.boxW || top < 0 || top > this.boxH) {
                return index;
            } else {
                let col = Math.floor(left / this.imgW);
                let row = Math.floor(top / this.imgH);
                let moveIndex = 5 * row + col;
                let i = 0;
                let len = this.randArr.length;
                while ((i < len) && this.randArr[i] !== moveIndex) {
                    i++;
                }
                return i;
            }
        }
        picReturn(index) {
            let j = this.randArr[index] % 5;
            let i = Math.floor(this.randArr[index] / 5);
            $('.pic').eq(index).css('z-index', '40').animate({
                'left': j * this.imgW,
                'top': i * this.imgH
            }, 300, function() {
                $(this).css('z-index', '10');
            })
        }
        picChange(index, index2) {
            let _this = this;
            let fromJ = _this.randArr[index] % 5;
            let fromI = Math.floor(_this.randArr[index] / 5);
            let toJ = _this.randArr[index2] % 5;
            let toI = Math.floor(_this.randArr[index2] / 5);
            let temp = _this.randArr[index];
            $('.pic').eq(index).css('z-index', '40').animate({
                'left': toJ * _this.imgW + 'px',
                'top': toI * _this.imgH + 'px'
            }, 300, function() {
                $(this).css('z-index', '10');
            })
            $('.pic').eq(index2).css('z-index', '40').animate({
                'left': fromJ * _this.imgW + 'px',
                'top': fromI * _this.imgH + 'px'
            }, 300, function() {
                $(this).css('z-index', '10');
                _this.randArr[index] = _this.randArr[index2];
                _this.randArr[index2] = temp;
                _this.check();
            })
        }
        getRandom() {
            this.randArr = [...this.orArr];
            this.randArr.sort(function() {
                return Math.random() - 0.5;
            })
        }
        getOrder(arr) {
            let len = arr.length;
            for (var i = 0; i < len; i++) {
                $('.box .pic').eq(i).animate({
                    left: arr[i] % 5 * this.imgW,
                    top: Math.floor(arr[i] / 5) * this.imgH
                }, 400)
            }
        }
        check() { //判断是否成功
            if (this.randArr.toString() == this.orArr.toString()) {
                alert('拼图成功');
                this.flag = true;
                $('.btn .start').text('开始');
                $('.pic').off('mousemove').off('mouseup').off('mousedown');
            }
        }
    }
    new Game();
参考至腾讯课堂渡一教育
jq + 面向对象实现拼图游戏的更多相关文章
- JavaScript写一个拼图游戏
		拼图游戏的代码400行, 有点多了, 在线DEMO的地址是:打开: 因为使用canvas,所以某些浏览器是不支持的: you know: 为什么要用canvas(⊙o⊙)? 因为图片是一整张jpg或 ... 
- 利用Vue.js实现拼图游戏
		之前写过一篇<基于Vue.js的表格分页组件>的文章,主要介绍了Vue组件的编写方法,有兴趣的可以访问这里进行阅读:http://www.cnblogs.com/luozhihao/p/5 ... 
- JavaScript拼图游戏
		今天是2016年最后一天上班了.最近几天都比较休闲,有时间空闲下来写写文档之类的. 2016过得真是快.感觉没做什么就过去了.想到之前想坚持每个月写一写博客都没坚持到.希望2017年可以吧. 无聊之余 ... 
- SDL制作拼图游戏
		看完教程第三集后,好像自己能用这个来写一个简单的拼图游戏,第一次写出个带界面的游戏,好有成就感. 图片是自己慢慢截左上部分8个脸. #include <stdio.h> #include ... 
- 拼图游戏(js,C#,java三种语言)
		<html> <head> <meta charset="utf-8"> <style type="text/css" ... 
- atitit.html5 拼图游戏的解决之道.
		atitit.html5 拼图游戏的解决之道. 1. 拼图游戏的操作(点击法and 拖动法) 1 1. 支持键盘上.下.左.右键移动: 1 2. 支持点击空白模块中的上下左右箭头移动: 1 3. 支持 ... 
- [CareerCup] 8.6 Jigsaw Puzzle 拼图游戏
		8.6 Implement a jigsaw puzzle. Design the data structures and explain an algorithm to solve the puzz ... 
- Android拼图游戏
		效果如下 游戏的设计 首先我们分析下如何设计这款游戏: 1.我们需要一个容器,可以放这些图片的块块,为了方便,我们准备使用RelativeLayout配合addRule实现 2.每个图片的块块,我们准 ... 
- 拼图游戏 v1.1
		我一直对拼图游戏比较有兴趣,市面上卖的所谓“1000块拼图”也玩过不少,不过玩那个太占地方,后来也不再买了,同时也就萌生了在电脑上玩拼图的想法. 现在虽然有很多拼图游戏,但能大多数只能支持几十或几百块 ... 
随机推荐
- 刷题记录:[0CTF 2016]piapiapia
			目录 刷题记录:[0CTF 2016]piapiapia 一.涉及知识点 1.数组绕过正则及相关 2.改变序列化字符串长度导致反序列化漏洞 二.解题方法 刷题记录:[0CTF 2016]piapiap ... 
- docker容器启动后添加端口映射
			DOCKER 给运行中的容器添加映射端口 方法1 1.获得容器IP 将container_name 换成实际环境中的容器名 docker inspect `container_name` | grep ... 
- 【定制开发】经纪人报备软件 全民经纪人系统 房产中介微信小程序分享家恒房通
			信真科技2019年最先扛鼎之作 - 全民经纪人软件系统 1.含有最基础的经纪人注册.客户报备系统功能: 2.可支持定制开发,针对房企售楼部.中介门店: 3.与微信端绑定使用,方便快捷,快速分享: 4. ... 
- Springboot单元测试Junit深度实践
			Springboot单元测试Junit深度实践 前言 单元测试的好处估计大家也都知道了,但是大家可以发现在国内IT公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这 ... 
- EOS测试链智能合约部署调用
			ETH与EOS两者智能合约进行简单的对比. 1.编译智能合约(合约编译成.wasm与.abi格式后即可部署到区块链) [root@C03-12U-26 testcontract]# cat testc ... 
- thinkphp5---join联合查询
			使用thinkphp3.2进行联合查询,join联合查询: $list = M('document as d') ->join('tp_admin_column as c on d.cid = ... 
- Win10 高频率使用的快捷组合键
			Win10 系统有很多的快捷组合键,学会使用这些快捷组合键可以节省一点时间 0x01 Win+D 显示或最小化桌面在键盘上按下Win+D可以切换显示桌面或最小化桌面所有内容: 0x02 Ctrl+Sh ... 
- Samba通过ad域进行认证并限制空间大小
			最近正在做单位电脑的AD域管理. 为漫游用户文件,研究配置Samba通过ad域进行认证并限制空间大小. 参考了很多资料,现总结如下: DC:windows server 2016(配置安装域控制器)略 ... 
- python:连接Oracle数据库后控制台打印中文为??
			打印查询结果,中文显示为了??? [('72H FCR', '2.0'), ('?????', '8.0')] E:\Python35\Lib\site-packages中新增文件: sitecust ... 
- 【原创】C++11:左值和右值(深度分析)
			——原创,引用请附带博客地址 2019-12-06 23:42:18 这篇文章分析的还是不行,先暂时放在这以后再更新. 本篇比较长,需要耐心阅读 以一个实际问题开始分析 class Sub{} Sub ... 
