JavaScript 拼图游戏

JavaScript 拼图游戏

文章目录

  • JavaScript 拼图游戏
    • html部分
    • js部分
    • 图片

html部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
   
</head>
<body>
    <div id="game">
        <!-- <img src="img/lol.png" alt=""> -->
    </div>
    <script src="script/index2.js"></script>
</body>
</html>

js部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/**
 * 游戏配置
 *  */

var gameConfig = {
    width: 500,
    height: 500,
    rows: 3, //行数
    cols: 3, //列数
    isOver: false, //游戏是否结束
    imgurl: "img/lol.png", //图片路径,注意:相对的是页面路径
    dom: document.getElementById("game") //游戏的dom对象
};
//每一块的高度
gameConfig.pieceWidth = gameConfig.width / gameConfig.cols;
gameConfig.pieceHeight = gameConfig.height / gameConfig.rows;

//小块的数量
gameConfig.pieceNumber = gameConfig.rows * gameConfig.cols;
/**
 * @param init() 初始化游戏
 */

var blocks = [];
function isEqual(b1,b2) {
    return parseInt(b1) === parseInt(b2);
}

function Block(left, top, isVisible) {
    this.left = left; //当前的横坐标
    this.top = top; //当前的纵坐标
    this.correctLeft = this.left; //正确的横坐标
    this.correctTop = this.top; //正确的纵坐标
    this.isVisible = isVisible;
    this.dom = document.createElement("div");
    this.dom.style.width = gameConfig.pieceWidth + "px";
    this.dom.style.height = gameConfig.pieceHeight + "px";
    this.dom.style.background = `url("${gameConfig.imgurl}") -${this.correctLeft}px -${this.correctTop}px`;
    this.dom.style.position = "absolute";
    this.dom.style.border = "1px solid #fff";
    this.dom.style.boxSizing = "border-box";
    this.dom.style.cursor = "pointer";
    // this.dom.style.transition = ".5s"; //css属性变化的时候,在0.5秒中内完成
    if (!isVisible) {
        this.dom.style.display = "none";
    }
    gameConfig.dom.appendChild(this.dom);
    this.show = function () {
        //根据当前的left、top,重新设置div的位置
        this.dom.style.left = this.left + "px";
        this.dom.style.top = this.top + "px";
    }
    //判断是否在正确的位置上
    this.isCorrect = function () {
        return isEqual(this.left , this.correctLeft) && isEqual(this.top , this.correctTop);
    }
    this.show();
}

function init() {
  //1.用函数设置最外面的容器样式
  initGameDom();
  //2. 初始化小方块
  //2.1 准备好一个数组,数组的每一项是一个对象,记录了每一个小方块的信息
  initBlocksArray();
  //3.随机小块的下表
  shuffle()
  //4. 注册点击事件
    regEvent();

    function regEvent() {
        var isVisibleBlock = blocks.find(function (b) {
            return !b.isVisible;
        })
        blocks.forEach(function (b) {
            b.dom.onclick = function () {
                if (gameConfig.isOver) {
                    return;
                }
                if(b.top === isVisibleBlock.top && isEqual(Math.abs(b.left - isVisibleBlock.left), gameConfig.pieceWidth)
                ||
                b.left === isVisibleBlock.left && isEqual(Math.abs(b.top - isVisibleBlock.top), gameConfig.pieceHeight)){
                    //交换当前方块和看不见的方块的坐标位置
                    exchange(b, isVisibleBlock);
                    //游戏结束判定
                    isWin();
                }
            }
        })
    }
    function isWin() {
        var wrongs = blocks.filter(function (item) {
            return !item.isCorrect();
        })
        if(wrongs.length === 0) {
            gameConfig.isOver = true;
            //游戏结束,去掉所有边框
            blocks.forEach(function (b) {
                b.dom.style.border = "none";
                b.dom.style.display = "block";
            });
        }
    }

  function getRandom(min, max) {
    return Math.floor(Math.random() * (max + 1 - min) + min);
}
function exchange(b1, b2) {
    var temp = b1.left;
    b1.left = b2.left;
    b2.left = temp;

    temp = b1.top;
    b1.top = b2.top;
    b2.top = temp;

    b1.show();
    b2.show();
}
  function shuffle(){
    for(var i = 0; i < blocks.length - 1; i ++) {
        var index = getRandom(0,blocks.length - 2);
        exchange(blocks[i],blocks[index]);
       
    }
  }


  function initBlocksArray() {
    for (var i = 0; i < gameConfig.rows; i++) {
      for (var j = 0; j < gameConfig.cols; j++) {
        var isVisible = true;
        if(i === gameConfig.rows - 1 && j === gameConfig.cols -1) {
            isVisible = false;
        }
         var b = new Block(j * gameConfig.pieceWidth, i * gameConfig.pieceHeight, isVisible);
        blocks.push(b);
      }
    }
  }

  function initGameDom() {
    gameConfig.dom.style.width = gameConfig.width + "px";
      gameConfig.dom.style.height = gameConfig.height + "px";
      gameConfig.dom.style.border = "2px solid #ccc";
    gameConfig.dom.style.position = "relative";
  }
}
init();

图片

在这里插入图片描述您可下载或者复制,试着写写哦!!! 但记得点赞哦! 哈哈.
在这里插入图片描述