欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用js編寫簡單的貪吃蛇小游戲

 更新時間:2021年09月10日 16:04:27   作者:驚蟄skk  
這篇文章主要為大家詳細(xì)介紹了用js編寫簡單的貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了js編寫簡單的貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下

代碼如下:

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]);
    }
}
//運動函數(shù)
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;
        }
    }
}

//游戲結(jié)束
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();

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 小程序?qū)崿F(xiàn)簡單列表功能

    小程序?qū)崿F(xiàn)簡單列表功能

    這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)簡單列表功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • js時間戳與日期格式之間轉(zhuǎn)換詳解

    js時間戳與日期格式之間轉(zhuǎn)換詳解

    這篇文章主要為大家詳細(xì)介紹了js時間戳與日期格式之間轉(zhuǎn)換,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • js原生輪播圖插件制作

    js原生輪播圖插件制作

    這篇文章主要為大家詳細(xì)介紹了js原生輪播圖插件制作,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • Express框架req?res對象使用詳解

    Express框架req?res對象使用詳解

    這篇文章主要為大家介紹了Express框架req?res對象使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • JavaScript的模塊化:封裝(閉包),繼承(原型) 介紹

    JavaScript的模塊化:封裝(閉包),繼承(原型) 介紹

    在復(fù)雜的邏輯下, JavaScript 需要被模塊化,模塊需要封裝起來,只留下供外界調(diào)用的接口。閉包是 JavaScript 中實現(xiàn)模塊封裝的關(guān)鍵,也是很多初學(xué)者難以理解的要點
    2013-07-07
  • JavaScript導(dǎo)出CSV文件不完整的問題解決方法

    JavaScript導(dǎo)出CSV文件不完整的問題解決方法

    在JavaScript中處理CSV文件時,需要特別注意一些特殊字符,例如逗號、雙引號、換行符等,這些字符可能會影響CSV文件的解析,導(dǎo)致數(shù)據(jù)錯亂,所以本文給大家介紹了如何解決JavaScript導(dǎo)出CSV文件不完整的問題,需要的朋友可以參考下
    2024-06-06
  • 微信小程序?qū)崿F(xiàn)上傳word、txt、Excel、PPT等文件功能

    微信小程序?qū)崿F(xiàn)上傳word、txt、Excel、PPT等文件功能

    這篇文章主要為大家詳細(xì)介紹了微信小程序?qū)崿F(xiàn)上傳word、txt、Excel、PPT等文件功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • 原生js實現(xiàn)計算購物車總金額的示例

    原生js實現(xiàn)計算購物車總金額的示例

    本文主要介紹了原生js實現(xiàn)計算購物車總金額的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • JS實現(xiàn)數(shù)組深拷貝的方法分析

    JS實現(xiàn)數(shù)組深拷貝的方法分析

    這篇文章主要介紹了JS實現(xiàn)數(shù)組深拷貝的方法,結(jié)合實例形式分析了javascript數(shù)組深拷貝的相關(guān)原理、實現(xiàn)方法及操作注意事項,需要的朋友可以參考下
    2019-03-03
  • JS實現(xiàn)Fisheye效果動感放大菜單代碼

    JS實現(xiàn)Fisheye效果動感放大菜單代碼

    這篇文章主要介紹了JS實現(xiàn)Fisheye效果動感放大菜單代碼,涉及JavaScript事假監(jiān)聽機制及定時函數(shù)等相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10

最新評論