俄罗斯方块游戏背景-canvas

俄罗斯方块背景- canvans

第一次写不知道说些什么好,直接上代码了@_@...



$("body").prepend($(`<canvas id="Tetris"> </canvas>`))//插入到body中
        var data = {
            speed: 200,
            linecolor: "#ddd",
            borderColor: "#fff", 
            unitsize: 30,
            baseColor: "#ddd",
            boxTypeArr: [
                [
                    [1, 4, 5, 8],
                    [0, 1, 5, 6],
                ],
                [
                    [1, 2, 4, 5],
                    [1, 5, 6, 10],
                ],
                [[0, 1, 4, 5]],
                [
                    [1, 5, 9, 13],
                    [4, 5, 6, 7],
                ],
                [
                    [1, 4, 5, 6],
                    [1, 5, 6, 9],
                    [0, 1, 2, 5],
                    [1, 4, 5, 9],
                ],
                [
                    [1, 5, 8, 9],
                    [0, 4, 5, 6],
                    [0, 1, 4, 8],
                    [0, 1, 2, 6],
                ],
            ],
            boxColorArr: [
                "#ffc0cb",
                "#dda0dd",
                "#9370db",
                "#6495ed",
                "#fa8072",
                "#ff8c00",
                "#ddd",
                "#ddd",
                "#ddd",
                "#ddd",
                "#ddd",
                "#ddd",
            ],
        canvas: null,
        ctx: null,
        w: 0,
        h: 0,
        row: 0,
        col: 0,
        chequerArr: [],
        nowbox: {},
        timer: null,
    }
    TetrisInit();
    $(window).bind('resize', TetrisInit)
    $(window).keydown(function (event) {
        switch (event.keyCode) {
            case 65: //左
                moveLeft();
                break;
            case 87: //上
                moveUp();
                break;
            case 68: //右
                moveRight();
                break;
            case 83: //下
                movedown();
                break;
        };
    })

    function TetrisInit() {
        if (!data.canvas) {
            data.canvas = document.querySelector("#Tetris");
            data.ctx = data.canvas.getContext("2d");
        };
        DataInit(); //数据初始化
        drawline();
        gameinit();
    }
    function DataInit() {
        data.w = screenWidth;
        data.h = screenHeight;
        data.row = Math.round(data.w / data.unitsize);
        data.col = Math.round(data.h / data.unitsize);
        data.canvas.width = data.w;
        data.canvas.height = data.h;
        for (let i = 0; i < data.col; i++) {
            data.chequerArr[i] = [];
            for (let j = 0; j < data.row; j++) {
                data.chequerArr[i][j] = 0;
            }
        }
        data.nowbox = {};
        clearInterval(data.timer);
    }
    function drawline() {
        data.ctx.lineWidth = 1;
        data.ctx.strokeStyle = data.linecolor;
        for (let i = 0; i <= data.row; i++) {
            data.ctx.moveTo(data.unitsize * i, 0);
            data.ctx.lineTo(data.unitsize * i, data.h);
        }
        data.ctx.stroke();
        for (let j = 0; j <= data.col; j++) {
            data.ctx.moveTo(0, data.unitsize * j);
            data.ctx.lineTo(data.w, data.unitsize * j);
        }
        data.ctx.stroke();
    }
    function nowboxinit() {
        let index = Math.floor(Math.random() * data.boxTypeArr.length);
        data.nowbox.Type = data.boxTypeArr[index];
        data.nowbox.state = 0;
        data.nowbox.color = data.boxColorArr[index];
        data.nowbox.initX = Math.floor(Math.random() * (data.row - 3));
        data.nowbox.initY = -4;
    }
    function gameinit() {
        nowboxinit();
        data.timer = setInterval(movedown, data.speed);
    }
    function movedown() {
        if (
            bang(
                data.nowbox.initX,
                data.nowbox.initY + 1,
                data.nowbox.Type[data.nowbox.state]
            )
        ) {
            clearnbox();
            data.nowbox.initY++;
            drawbox();
        } else {
            for (
                let i = 0;
                i < data.nowbox.Type[data.nowbox.state].length;
                i++
            ) {
                let x =
                    data.nowbox.initX +
                    (data.nowbox.Type[data.nowbox.state][i] % 4);
                let y =
                    data.nowbox.initY +
                    Math.floor(data.nowbox.Type[data.nowbox.state][i] / 4);
                if (data.chequerArr[y]) {
                    data.chequerArr[y][x] = 1;
                } else {
                    setInterval(data.timer);
                    return TetrisInit();
                }
            }
            data.nowbox.color = data.baseColor;
            drawbox();
            fullLine();
            nowboxinit();
        }
    }
    function moveLeft() {
        if (
            bang(
                data.nowbox.initX - 1,
                data.nowbox.initY,
                data.nowbox.Type[data.nowbox.state]
            )
        ) {
            clearnbox();
            data.nowbox.initX--;
            drawbox();
        }
    }
    function moveRight() {
        if (
            bang(
                data.nowbox.initX + 1,
                data.nowbox.initY,
                data.nowbox.Type[data.nowbox.state]
            )
        ) {
            clearnbox();
            data.nowbox.initX++;
            drawbox();
        }
    }
    function moveUp() {
        let s = data.nowbox.state;
        if (s == data.nowbox.Type.length - 1) {
            s = 0;
        } else {
            s++;
        }
        if (
            bang(data.nowbox.initX, data.nowbox.initY, data.nowbox.Type[s])
        ) {
            clearnbox();
            data.nowbox.state = s;
            drawbox();
        }
    }
    function drawbox() {
        data.ctx.fillStyle = data.nowbox.color;
        data.ctx.strokeStyle = data.borderColor;
        var arr = data.nowbox.Type[data.nowbox.state];
        for (let i = 0; i < arr.length; i++) {
            let x = data.nowbox.initX + (arr[i] % 4);
            let y = data.nowbox.initY + Math.floor(arr[i] / 4);
            data.ctx.fillRect(
                data.unitsize * x,
                data.unitsize * y,
                data.unitsize,
                data.unitsize
            );
            data.ctx.strokeRect(
                data.unitsize * x,
                data.unitsize * y,
                data.unitsize,
                data.unitsize
            );
        }
    }
    function clearnbox() {
        data.ctx.lineWidth = 1;
        data.ctx.strokeStyle = data.linecolor;
        data.ctx.fillStyle = data.nowbox.color;
        var arr = data.nowbox.Type[data.nowbox.state];
        for (let i = 0; i < arr.length; i++) {
            let x = data.nowbox.initX + (arr[i] % 4);
            let y = data.nowbox.initY + Math.floor(arr[i] / 4);
            data.ctx.clearRect(
                data.unitsize * x,
                data.unitsize * y,
                data.unitsize,
                data.unitsize
            );
            data.ctx.strokeRect(
                data.unitsize * x,
                data.unitsize * y,
                data.unitsize,
                data.unitsize
            );
        }
    }
    function bang(x, y, nowArr) {
        for (let i = 0; i < nowArr.length; i++) {
            if (y + Math.ceil((nowArr[i] + 1) / 4) > data.col) {
                return false;
            } else if (data.chequerArr[y + Math.floor(nowArr[i] / 4)] &&
                data.chequerArr[y + Math.floor(nowArr[i] / 4)][
                x + (nowArr[i] % 4)
                ]
            ) {
                return false;
            } else if (
                x + (nowArr[i] % 4) < 0 ||
                x + (nowArr[i] % 4) > data.row - 1
            ) {
                return false;
            }
        }
        return true;
    }
    function fullLine() {
        let fullarr = [];
        for (let i = data.chequerArr.length - 1; i > 0; i--) {
            let isfull = true;
            for (let j = 0; j < data.chequerArr[i].length; j++) {
                if (data.chequerArr[i][j] == 0) {
                    isfull = false;
                    break;
                }
            }
            if (isfull) {
                fullarr.push(i);
            }
        }

        if (fullarr.length) {
            for (let i = 0; i < fullarr.length; i++) {
                let arr = data.chequerArr.splice(fullarr[i], 1);
                data.chequerArr.unshift(arr.fill(0));
            }
            data.ctx.clearRect(0, 0, data.w, data.h);
            drawline();
            data.ctx.fillStyle = data.baseColor;
            data.ctx.strokeStyle = data.borderColor;
            for (let i = 0; i < data.col; i++) {
                for (let j = 0; j < data.row; j++) {
                    if (data.chequerArr[i][j] == 1) {
                        data.ctx.fillRect(
                            data.unitsize * j,
                            data.unitsize * i,
                            data.unitsize,
                            data.unitsize
                        );
                        data.ctx.strokeRect(
                            data.unitsize * j,
                            data.unitsize * i,
                            data.unitsize,
                            data.unitsize
                        );
                    }
                }
            }
        }
    }

效果和我的博客背景一样

上一篇:DDD领域驱动设计-项目包结构说明-Ⅳ


下一篇:DDD领域驱动设计-概述-Ⅰ