美文网首页
三消游戏h5

三消游戏h5

作者: 叫我亮王 | 来源:发表于2017-05-19 22:09 被阅读0次

    var wrap = document.getElementsByClassName("wrap")[0];

    var random_color = ["red","green","yellow"]

    //    var random_color = ["./img/s1.jpg","./img/s2.jpg","./img/s3.jpg","./img/s4.jpg","./img/s5.jpg"]

    var row_len = 10;

    var col_len = 10;

    var box_left = 50+20;

    var box_top = 50+20;

    var box_del_len = 3;

    var row_over = [];

    var col_over = [];

    for(var i = 0; i < row_len;i++){

    row_over[i] = 0;

    }

    for(var i = 0; i < col_len;i++){

    col_over[i] = 0;

    }

    var arr_box = [];

    for(var i = 0; i < row_len; i++){

    var arr_col_box = [];

    for(var j = 0; j < col_len; j++){

    var attr = {

    row:i,

    col:col_len-1-j,

    bgColor:random_bg(random_color)

    }

    arr_col_box[col_len-1-j] = new Box(attr);

    }

    arr_box[i] = arr_col_box;

    }

    //  创造box

    function Box(attr) {

    //  方块第几排

    this.row = attr.row;

    //  方块第几列

    this.col = attr.col;

    //  方块背景颜色(哪个方块)

    this.bgColor = attr.bgColor;

    this.left = (this.row ) * box_left;

    this.top = (this.col) * box_top;

    this.div = document.createElement("div");

    this.div.className = "a";

    this.div.style.left = this.left+"px";

    this.div.style.top = this.top+"px";

    this.div.style.backgroundColor = this.bgColor;

    //        var newimg = new Image();

    //        newimg.src = this.bgColor;

    //        newimg.className = "img";

    //        this.div.appendChild(newimg);

    var that = this;

    this.div.onclick = function () {

    check_same(that);

    check_row_all_none;

    console.log(arr_box)

    }

    //        this.div.innerHTML = this.row + " " + this.col;

    wrap.appendChild(this.div)

    }

    //  随机颜色

    function random_bg(random_color) {

    var ran_len = random_color.length;

    var ran = Math.floor(Math.random() * ran_len);

    return random_color[ran];

    }

    //  判断3个相同

    function check_same(box) {

    var row = box.row;

    var col = box.col;

    var bgColor = box.bgColor;

    var arr_res = [box];

    check_row(box,arr_res)

    check_col(box,arr_res)

    if(arr_res.length >= box_del_len){

    del_box(arr_res)

    }

    }

    //  检查整列

    function check_col(box,arr_res) {

    var row = box.row;

    var col = box.col;

    var bgColor = box.bgColor;

    for(var i = col + 1; i < col_len; i++){

    var box_col = arr_box[row][i];

    if(!box_col) break;

    if(box_col.bgColor == bgColor && arr_res.indexOf(box_col) == -1){

    arr_res.push(box_col)

    check_row(box_col,arr_res)

    } else {

    break;

    }

    }

    for(var i = col - 1; i >= 0; i--){

    var box_col = arr_box[row][i];

    if(!box_col) break;

    if(box_col.bgColor == bgColor && arr_res.indexOf(box_col) == -1){

    arr_res.push(box_col)

    check_row(box_col,arr_res)

    } else {

    break;

    }

    }

    }

    //  检查整行

    function check_row(box,arr_res) {

    var row = box.row;

    var col = box.col;

    var bgColor = box.bgColor;

    for(var i = row + 1; i < row_len; i++){

    var box_row = arr_box[i][col];

    if(!box_row) break;

    if(box_row.bgColor == bgColor && arr_res.indexOf(box_row) == -1){

    arr_res.push(box_row)

    check_col(box_row,arr_res)

    } else {

    break;

    }

    }

    for(var i = row - 1; i >= 0; i--){

    var box_row = arr_box[i][col];

    if(!box_row) break;

    if(box_row.bgColor == bgColor && arr_res.indexOf(box_row) == -1){

    arr_res.push(box_row)

    check_col(box_row,arr_res)

    } else {

    break;

    }

    }

    }

    //  删除box

    function del_box(arr) {

    var arrlen = arr.length;

    //  储存改变的所有行,并且列数是最大的

    var arr_max_col = [];

    for(var i = 0; i < arrlen; i++){

    arr[i].div.style.backgroundColor = "white";

    arr[i].div.style.display = "none";

    row_over[arr[i].row]++;

    col_over[arr[i].col]++;

    create_max_col(arr_max_col,arr[i])

    }

    col_fall(arr_max_col)

    }

    //  创建arr_max_col

    function create_max_col(arr,box) {

    var arrlen = arr.length;

    if(arrlen == 0){

    arr.push({row:box.row,maxcol:box.col,mincol:box.col});

    return false;

    }

    for(var i = 0; i < arrlen; i++){

    if(arr[i].row == box.row){

    arr[i].maxcol = arr[i].maxcol < box.col ? box.col : arr[i].maxcol;

    arr[i].mincol = arr[i].mincol > box.col ? box.col : arr[i].mincol;

    break;

    } else if(i == arrlen - 1) {

    arr.push({row:box.row,maxcol:box.col,mincol:box.col})

    }

    }

    }

    //  列落下

    function col_fall(arr) {

    var arrlen = arr.length;

    var arr_row = [];

    for(var i = 0; i < arrlen; i++){

    var row = arr[i].row;

    var mincol = arr[i].mincol;

    var col = arr[i].maxcol - mincol + 1;

    for(var j =  mincol - 1; j >= 0; j--){

    if(arr_box[row][j]) {

    arr_box[row][j].div.style.top = arr_box[row][j].div.offsetTop + col * box_top + "px";

    console.log(arr_box[row][j].div.offsetTop)

    arr_box[row][j].col += col;

    arr_box[row][arr_box[row][j].col] = arr_box[row][j];

    }

    }

    for(var n = 0; n < row_over[row];n++){

    arr_box[row][n] = undefined;

    }

    }

    }

    //  检查是否有某一行全部被消掉

    function check_row_all_none() {

    for(var i = 0; i < row_len;i++){

    if(col_over[i] == col_len){

    all_row_none(i);

    };

    }

    }

    //  某一行全部消掉时

    function all_row_none(row) {

    for(var i = row; i < row_len; i++){

    if(row == row_len-1 || arr_box[i+1].length == 0) break;

    arr_box[i] = arr_box[i+1];

    arr_box[i+1] = [];

    for(var j = 0; j < col_len; j++){

    arr_box[row][j].row -= 1;

    arr_box[row][j].div.style.left = arr_box[row][j].div.offsetLeft - box_left + "px";

    }

    }

    }

    相关文章

      网友评论

          本文标题:三消游戏h5

          本文链接:https://www.haomeiwen.com/subject/wwluxxtx.html