javascript实现贪吃蛇

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <style>
        body{
            font-size:9pt;
        }
        table{
            border-collapse: collapse;
            border:solid #333 1px;
        }
        td{
            height: 15px;
            width: 15px;
            font-size: 0px;
        }
        .filled{
            background-color:blue;
        }
    </style>
    <script>
        function $(id){
            return document.getElementById(id);
        }
        var Snake = {
            table1: null,
            snakeBody:[],
            //当前移动的方向,取值0,1,2,3, 分别表示向左,上,右,下
            direction: 0,
            timer: null,
            speed: 250,
            paused: true,
            rowCount: 30,
            colCount: 30,
            colors:['red','orange','yellow','green','grey','blue','purple'],
            init: function(){
                //var colors = ['red','orange','yellow','green','grey','blue','purple'];
                this.table1 = $('main');
                var x = 0;
                var y = 0;
                var colorIndex = 0;
                this.direction = Math.floor(Math.random()*4);
                //构造表格
                for(var row = 0; row < this.rowCount; row++ ){
                    var tr = this.table1.insertRow(-1);
                    for(var col = 0; col < this.colCount; col++){
                        var td = tr.insertCell(-1);
                    }
                }
                //产生10个点
                for(var i = 0; i < 10; i++){
                    x = Math.floor(Math.random()*this.colCount);
                    y = Math.floor(Math.random()*this.rowCount);
                    colorIndex = Math.floor(Math.random()*7);
                    if(!this.isCellFilled(x, y)){
                        this.table1.rows[y].cells[x].style.backgroundColor = Snake.colors[colorIndex];
                    }
                }
                //产生蛇头
                while(true){
                    x = Math.floor(Math.random()*this.colCount);
                    y = Math.floor(Math.random()*this.rowCount);
                    if(!this.isCellFilled(x,y)){
                        this.table1.rows[y].cells[x].style.backgroundColor = 'black';
                        this.snakeBody.push({x: x, y: y,color:'black'});
                        break;
                       }
               }
                this.paused = true;
                //添加键盘事件
                document.onkeydown = function(e){
                    if(!e) e = windown.event;
                    switch(e.keyCode| e.which | e.charCode){
                        case 13:{//enter
                            if(Snake.paused){
                                Snake.move();
                                Snake.paused = false;
                                break;
                            }
                            else{
                                Snake.pause();
                                Snake.paused = true;
                                break;
                            }
                        }
                        case 37:{//left
                            if(Snake.direction == 2){
                                break;
                            }
                            Snake.direction = 0;
                            break;
                        }
                        case 38:{//up
                            if(event.ctrlKey){
                                Snake.speedUp(-20);
                                break;
                            }
                            if(Snake.direction == 3){
                                break;
                            }
                            Snake.direction = 1;
                            break;
                        }
                        case 39:{//right
                            if(Snake.direction == 0){
                                break;
                            }
                            Snake.direction = 2;
                            break;
                        }
                        case 40:{//down
                            if(event.ctrlKey){
                                Snake.speedUp(20);
                                break;
                            }
                            if(Snake.direction == 1){
                                break;
                            }
                            Snake.direction = 3;
                            break;
                        }
                    }
                }
            },
            move: function(){
                this.timer = setInterval(function(){
                    Snake.erase();
                    Snake.moveOneStep();
                    Snake.paint();
                },  this.speed);
            },
            moveOneStep: function(){
                if(this.checkNextStep() == -1){
                    clearInterval(this.timer);//this
                    alert("Game Over!");
                    return;
                }
                var point = this.getNextPos();
                if(this.checkNextStep() == 1){
                    var _point = this.getNextPos();
                    var _x = _point.x;
                    var _y = _point.y;
                    this.snakeBody.unshift({x:_x, y:_y, color:'black'});
                    this.generateFood();
                    return;
                }
                this.snakeBody.pop();
                this.snakeBody.unshift({x:point.x, y: point.y, color:'black' });

            },
            pause: function(){
                clearInterval(this.timer);//this
                this.paint();
            },
            getNextPos: function(){
                var x = this.snakeBody[0].x;
                var y = this.snakeBody[0].y;
                var color = this.snakeBody[0].color;
                if(this.direction == 0){
                    x--;
                }
                else if( this.direction == 1){
                    y--;
                }
                else if(this.direction == 2){
                    x++;
                }
                else{
                    y++;
                }
                return {x:x, y:y};
            },
            checkNextStep: function(){
                var point = this.getNextPos();
                var x = point.x;
                var y = point.y;
                if(x < 0 ||  x >= this.colCount || y < 0 || y >= this.rowCount){
                    return -1;//game over
                }
                for(var i = 0; i < this.snakeBody.length; i++){
                    if(this.snakeBody[i].x == x && this.snakeBody[i].y == y){
                        return -1;
                    }
                }
                if(this.isCellFilled(x, y)){//因为如果没有因为-1返回且又有颜色,则只可能是食物
                    return 1;
                }
                return 0;
            },

            erase: function(){
                for(var i = 0; i < this.snakeBody.length; i++){
                    this.eraseDot(this.snakeBody[i].x,this.snakeBody[i].y);
                }
            },
            paint: function(){
                for(var i = 0; i < this.snakeBody.length; i++){
                    this.paintDot(this.snakeBody[i].x, this.snakeBody[i].y, this.snakeBody[i].color);
                }
            },
            eraseDot:function(x, y){
                this.table1.rows[y].cells[x].style.backgroundColor = "";
            },
            paintDot: function(x, y, color){
                this.table1.rows[y].cells[x].style.backgroundColor = color;
            },
            getColor: function(x,y){
                    return this.snakeBody.rows[y].cells[x].style.backgroundColor;
            },
            isCellFilled: function(x,y){
                if(this.table1.rows[y].cells[x].style.backgroundColor == ""){
                    return false;
                }
                return true;
            },
            generateFood: function(){
                while(true){
                    var x = Math.floor(Math.random()*this.colCount);
                    var y = Math.floor(Math.random()*this.rowCount);
                    var colorIndex = Math.floor(Math.random()*7);
                    if(!this.isCellFilled(x, y)){
                        this.table1.rows[y].cells[x].style.backgroundColor = this.colors[colorIndex];
                        break;
                    }
                }
            },
            restart: function(){
                if(this.timer){
                    clearInterval(this.timer);
                }
                for(var i = 0; i < this.rowCount; i++){
                    this.table1.deleteRow(0);
                }
                this.snakeBody = [];
                this.init();
                this.speed = 250;

               // this.move();
            },
            speedUp: function(time){
                if(!this.paused){
                    if(this.speed + time < 10 || this.speed + time > 2000){
                        return;
                    }
                    this.speed += time;
                    this.pause();//
                    this.move();
                }
            }
        }
    </script>
</head>
<body οnlοad="Snake.init();">
<table id="main" border="1"></table>
<input type="button" id="btn" value="start/pause"/>
<input type="button" id="reset" value="restart"/>
<input type="button" id="speedup" value="speedup"/>
<input type="button" id="slowdown" value="slowdown"/>
<script>
    document.getElementById('btn').onclick = function(){
        if(Snake.paused){
            Snake.move();
            Snake.paused = false;
        }
        else{
            Snake.pause();
            Snake.paused = true;
        };
    };
    $('reset').onclick = function(){
        Snake.restart();

    };
    $('speedup').onclick = function(){
        Snake.speedUp(-20);
    };
    $('slowdown').onclick = function(){
        Snake.speedUp(20);
    };
</script>
</body>
</html>
总结一句话,想一天速成js还是不可能的。完全被this虐了~
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值