欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

用js编写简单的贪吃蛇小游戏

程序员文章站 2022-04-03 21:04:52
本文实例为大家分享了js编写简单的贪吃蛇小游戏的具体代码,供大家参考,具体内容如下代码如下:html 5 部分&l...

本文实例为大家分享了js编写简单的贪吃蛇小游戏的具体代码,供大家参考,具体内容如下

代码如下:

html 5 部分

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>贪恰蛇</title>
    <style>
        #scence{
            width: 800px;
            height: 600px;
            border: 1px solid #000;
            margin: 50px auto;
            background-color: aliceblue;
            position: relative;
            overflow: hidden;
        }
       .head{
           position: absolute;
           width: 20px;
           height: 20px;
           background-color: #000;
       }
       .tail{
        position: absolute;
           width: 20px;
           height: 20px;
           background-color: grey;       
       }
    </style>
</head>
<body>
    <div id="scence">

    </div>
</body>
</html>
<script src="tools.js"></script>
<script src="../贪吃蛇/snake.js"></script>
<script src="food.js"></script>
<script src="game.js"></script>

js部分

tools.js

function getstyle(ele, styleobj) {
    for (const key in styleobj) {
        ele.style[key] = styleobj[key];
    }   
}
function getrandom(a, b) {
    return math.floor(math.random() * (b - a) + a +1)
}

snake.js

function snake() {
    this.scence = document.queryselector('#scence');
    this.body = [
        [0, 0, 'grey', null],
        [0, 1, 'grey', null],
        [0, 2, '#000', null]
    ];
    this.dir = 'right';
    this.lastdir = 'right';
    this.width = 20;
    this.height = 20;
    this.scence_w = scence.offsetwidth;
    this.scence_h = scence.offsetheight;
}
snake.prototype.found = function () {
    for (let i = 0; i < this.body.length; i++) {
        if (this.body[i][3] == null) {
            this.body[i][3] = document.createelement('div');
        }
        getstyle(this.body[i][3], {
            width: this.width + 'px',
            height: this.height + 'px',
            position: 'absolute',
            top: this.height * (this.body[i][0]) + 'px',
            left: this.width * (this.body[i][1]) + 'px',
            backgroundcolor: this.body[i][2]
        });
        this.scence.appendchild(this.body[i][3]);
    }
}
//运动函数
snake.prototype.move = function () {
    var length = this.body.length;
    for (let i = 0; i < length - 1; i++) {
        this.body[i][0] = this.body[i + 1][0];
        this.body[i][1] = this.body[i + 1][1];
    }
    let snakehead = this.body[length - 1]
    switch (this.dir) {
        case 'right':
            snakehead[1] += 1;
            break;
        case 'left':
            snakehead[1] -= 1
            break;
        case 'up':
            snakehead[0] -= 1
            break;
        case 'down':
            snakehead[0] += 1
            break;
    }
    this.lastdir = this.dir;
    snake.found();
}
//计时运动
snake.prototype.shift = function () {
    document.onkeydown = (e) => {
        e = e || window.event;
        let key = e.keycode;
        switch (key) {
            case 39:
                if (this.lastdir == 'left') {
                    this.dir = 'left'
                } else {
                    this.dir = 'right'
                };
                break;
            case 37:
                if (this.lastdir == 'right') {
                    this.dir = 'right'
                } else {
                    this.dir = 'left'
                };
                break;
            case 38:
                if (this.lastdir == 'down') {
                    this.dir = 'down'
                } else {
                    this.dir = 'up'
                };
                break;
            case 40:
                if (this.lastdir == 'up') {
                    this.dir = 'up'
                } else {
                    this.dir = 'down'
                };
                break;
        }
    }
}

//游戏结束
snake.prototype.over = function () {
    let top = this.body[this.body.length - 1][0];
    let left = this.body[this.body.length - 1][1];
    let width = this.scence_w / this.width - 1;
    let height = this.scence_w / this.height - 1;
    if (top < 0 || left < 0 || top > width || left > height) {
        clearinterval(timeid)
        alert('game over');
    }
    for (let i = 0; i < this.body.length - 1; i++) {
        if (top == this.body[i][0] && left == this.body[i][1]) {
            clearinterval(timeid)
            alert('game over');
        }
    }
}


let snake = new snake();
snake.found();
snake.shift();
timeid = setinterval(function () {
    snake.move();
    food.eat();
    snake.over()
}, 100)

food.js

function food() {
  this.scence = document.queryselector('#scence');
  this.width = 20;
  this.height = 20;
  this.body = [-1, -1, 'red', null];
  this.scence_w = scence.offsetwidth;
  this.scence_h = scence.offsetheight;
  
}
//食物生成
food.prototype.crteate = function () {
  this.body[1] = getrandom(0, this.scence_w / this.width-1);
  this.body[0] = getrandom(0, this.scence_h / this.height-1);
  this.body[3] = document.createelement('div');
  getstyle(this.body[3], {
    width: this.width + 'px',
    height: this.height + 'px',
    position: 'absolute',
    top: this.height * (this.body[0] ) + 'px',
    left: this.width * (this.body[1] ) + 'px',
    backgroundcolor: this.body[2],
    borderradius: ' 50%',
  });
  this.scence.appendchild(this.body[3]);


}
//蛇吃到食物
food.prototype.eat=function(){
  // const new=[0, 0, 'grey', null]
if(snake.body[snake.body.length-1][0]==this.body[0] && snake.body[snake.body.length-1][1]==this.body[1]){
  this.scence.removechild(this.body[3]);
  this.crteate();
  snake.body.unshift([-1,-1,"grey",null])
}
}
let food = new food();
food.crteate();
food.eat();

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: js 贪吃蛇

上一篇: 我认为是奥特曼

下一篇: 独自坚强