JS贪吃蛇

源码如下:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta >
    <title></title>
    <style>
        .map {
            position: relative;
            margin:0 auto;
            width: 900px;
            height: 600px;
            background-color: black;
        }

        .snake {
            position: absolute;
            width: 30px;
            height: 30px;
            border: 1px solid white;
            box-sizing: border-box;
        }

        .food {
            position: absolute;
            width: 30px;
            height: 30px;
            border: 1px solid white;
            box-sizing: border-box;
            background-color: blue;
        }
    </style>
</head>
<body>
<script>
    var Map;
    function map(){
        this._map=null;
        this.creatMap=function(){
            this._map=document.createElement("div");
            this._map.className="map";
            document.body.appendChild(this._map);
        }
    }
    var Snake;
    function snake(){
        this._snake=[["red", 3, 1, null], ["yellow", 2, 1, null], ["yellow", 1, 1, null]]
        this.direction="right";
        this.createSnake=function(){
            for(var i=0;i<this._snake.length;i++){
                if(this._snake[i][3]==null){
                    this._snake[i][3]=document.createElement("div");
                    this._snake[i][3].className="snake";
                    this._snake[i][3].style.backgroundColor=this._snake[i][0];
                    console.log(this._snake[i][3]);
                    Map._map.appendChild(this._snake[i][3]);
                }

                this._snake[i][3].style.left=this._snake[i][1]*30+"px";
                this._snake[i][3].style.top=this._snake[i][2]*30+"px";
        }
            this.snakemove = function(){
                var len =this._snake.length-1;
                //蛇移动,传递位置
                for(var i = len;i>0;i--){
                    this._snake[i][1]=this._snake[i-1][1];
                    this._snake[i][2]=this._snake[i-1][2];
                }
                //判断移动方向行走
                switch(this.direction){
                    case "right":this._snake[0][1]+=1;break;
                    case "left":this._snake[0][1]-=1;break;
                    case "up":this._snake[0][2]-=1;break;
                    case "down":this._snake[0][2]+=1;break;
                }
                //当蛇穿墙
                if (this._snake[0][1] > 29) {
                    this._snake[0][1] = 0;
                }
                if (this._snake[0][1] < 0) {
                    this._snake[0][1] = 29;
                }
                if (this._snake[0][2] > 19) {
                    this._snake[0][2] = 0;
                }
                if (this._snake[0][2] < 0) {
                    this._snake[0][2] = 19;
                }
                //检测食物
                if(this._snake[0][1] == Food.x&&this._snake[0][2] == Food.y){
                    this._snake.push(["yellow",
                        this._snake[this._snake.length-1][1],
                        this._snake[this._snake.length-1][2],
                    ])
                    Food.createFood();
                }
                this.createSnake();
            }

        }
    }
    var Food;
    function food(){
        this._food = null;
        this.x;
        this.y;
        this.createFood = function(){
            this.x = Math.floor(Math.random()*30);
            this.y = Math.floor(Math.random()*20);
            if(this._food==null){
                this._food = document.createElement("div");
                this._food.className="food";
                Map._map.appendChild(this._food);
            }
            this._food.style.left = this.x *30+"px";
            this._food.style.top = this.y *30+"px";
        }
    }
    window.onload=function(){
        Map=new map();
        Map.creatMap();
        Snake=new snake();
        Snake.createSnake();
        Food=new food();
        Food.createFood();
        document.body.onkeyup = function (e) {
            console.log(e.keyCode);
            switch (e.keyCode) {
                case 87:
                    if (Snake.direction != "down") {
                        Snake.direction = "up";
                    }
                    break;
                case 65:
                    if (Snake.direction != "right") {
                        Snake.direction = "left";
                    }
                    break;
                case 68:
                    if (Snake.direction != "left") {
                        Snake.direction = "right";
                    }
                    break;
                case 83:
                    if (Snake.direction != "up") {
                        Snake.direction = "down";
                    }
                    break;
            }
        }
        setInterval(function () {
            Snake.snakemove();
        }, 100);
    }
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值