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

JavaScript實(shí)現(xiàn)經(jīng)典貪吃蛇游戲

 更新時(shí)間:2021年09月17日 09:49:42   作者:fanfan_h  
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)經(jīng)典貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了JavaScript實(shí)現(xiàn)經(jīng)典貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下

主要使用單例模式,所有元素動(dòng)態(tài)創(chuàng)建;

1.創(chuàng)建地圖

var Map;
    function map(){
        this.mapp=null;
        this.makemap=function(){
            this.mapp=document.createElement ("div");
            this.mapp.className ="mymap";
            document.body.appendChild(this.mapp );
        }
    }

2.創(chuàng)建貪吃蛇模型

創(chuàng)建一個(gè)集合,存放蛇的前三節(jié),遍歷集合,創(chuàng)建蛇的大小和顏色,設(shè)置每個(gè)小節(jié)的寬高為30px;初始化蛇頭方向向右 

var Snack;
    function snack(){
        this.snackk=[["red",3,1,null],["pink",2,1,null],["pink",1,1,null]];
        this.direct="right";
        this.makesnack=function(){
             for(var i=0;i<this.snackk.length;i++){
                 if(this.snackk[i][3]==null){
                     this.snackk[i][3]=document.createElement ("div");
                     this.snackk[i][3].className ="eatsnack";
                     this.snackk[i][3].style.backgroundColor =this.snackk[i][0];
                     Map.mapp.appendChild(this.snackk[i][3]);
                 }
                 this.snackk[i][3].style.left=this.snackk[i][1]*30+"px";
                 this.snackk[i][3].style.top=this.snackk[i][2]*30+"px";
             }
        };

3.動(dòng)態(tài)蛇,從后向前遍歷存放蛇的每一節(jié)的集合snackk,將每小節(jié)的屬性從后想前傳遞,并且設(shè)置蛇頭移動(dòng)方向,方向改變蛇的左邊距和上邊距也會(huì)跟著改變,再設(shè)置一個(gè)計(jì)時(shí)器,每100ms讓蛇動(dòng)起來一次;

this.movesnack=function(){
            var long=this.snackk.length-1;
            for(var i=long; i>0; i--){
                this.snackk[i][1]=this.snackk[i-1][1];
                this.snackk[i][2]=this.snackk [i-1][2];
            }
            switch(this.direct){
                case "right":  //向右
                        this.snackk[0][1] +=1;
                    break;
                case "left":  //向左
                    this.snackk[0][1] -=1;
                    break;
                case "down": //向下
                    this.snackk[0][2] +=1;
                     break;
                case "up":  //向上
                    this.snackk[0][2] -=1;
                     break;
            }

4.創(chuàng)建食物。

在地圖上隨機(jī)產(chǎn)生食物,食物的大小和每一小節(jié)蛇的大小一樣

var Guoguo;
    function guozi(){
        this.guoo=null;
        this.x;
        this.y;
        this.makeguozi=function(){
            this.x=Math.floor( Math.random() * 30);  //0-29
            this.y=Math.floor( Math.random() * 20);  //0-19
            if(this.guoo==null){
                this.guoo=document.createElement ("div");
                this.guoo.className ="guozi";
                Map.mapp.appendChild(this.guoo);
            }
            this.guoo.style.left=this.x * 30+"px";
            this.guoo.style.top =this.y * 30+"px";
        }
    }

5.設(shè)置鍵盤事件,上下左右鍵控制蛇頭的變化方向

document.body.onkeyup=function(e){
           // console.log(e);
            switch(e.keyCode){
                case 37:  //左
                        if(Snack.direct!="right"){
                            Snack.direct ="left";
                        }
                    break;
                case 39:// 右
                    if(Snack.direct!="left"){
                        Snack.direct ="right";
                    }
                    break;
                case 38: //上
                    if(Snack.direct!="down"){
                        Snack.direct ="up";
                    }
                    break;
                case 40:   //下
                    if(Snack.direct!="up"){
                       Snack.direct ="down";
                    }
                    break;
                case 87:
                    if (Snack.direct != "down") {
                        Snack.direct = "up";
                    }
                    break;
                case 65:
                    if (Snack.direct != "right") {
                        Snack.direct = "left";
                    }
                    break;
                case 68:
                    if (Snack.direct != "left") {
                        Snack.direct = "right";
                    }
                    break;
                case  83:
                    if (Snack.direct != "up") {
                        Snack.direct = "down";
                    }
                    break;
            }
        };

6.檢測(cè)蛇頭和食物的位置,蛇頭吃到食物,蛇的長(zhǎng)度變長(zhǎng),給snackk集合追加元素,接著在隨機(jī)創(chuàng)建食物,再檢測(cè)食物位置,再吃到食物;

if(this.snackk[0][1]==Guoguo.x && this.snackk[0][2]==Guoguo.y ){
                 this.snackk.push([
                         "pink",
                         this.snackk[this.snackk.length-1][1],
                         this.snackk[this.snackk.length-1][2],
                         null
                 ]);
                 Guoguo.makeguozi ();
                 }

7.設(shè)置蛇身可以穿墻而過,如果蛇頭的上下左右邊距等于0時(shí),改變邊距到最大值;

if(this.snackk[0][1]>29){
                this.snackk[0][1]=0 ;  //從右邊穿墻
            }
            if(this.snackk[0][1]<0){
                this.snackk[0][1]=29 ;  //從右邊穿墻
            }
            if(this.snackk[0][2]>19){
                this.snackk[0][2]=0 ;  //從右邊穿墻
            }
            if(this.snackk[0][2]<0){
                this.snackk[0][2]=19 ;  //從右邊穿墻
            }
            this.makesnack();
            this.stopsnack();

        };

8.設(shè)計(jì)游戲結(jié)束,貪吃蛇撞在自己的身體就死了,游戲結(jié)束,關(guān)閉計(jì)時(shí)器
當(dāng)蛇頭的上邊距,左邊距等于蛇身體某一塊的上編劇和左邊距時(shí),游戲結(jié)束,彈出游戲結(jié)束的提示圖片

this.stopsnack=function(){
            for(var k=this.snackk.length-1;k>0;k--){
                if(this.snackk[0][1]==this.snackk [k][1] && this.snackk[0][2]==this.snackk [k][2]){
                    clearInterval(time);
                    var gameover=document.createElement ("div");
                    gameover.className ="over";
                    gameover.style.display ="block";
                    Map.mapp.appendChild (gameover);
                }
            }
}

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

相關(guān)文章

  • 通過js為元素添加多項(xiàng)樣式,瀏覽器全兼容寫法

    通過js為元素添加多項(xiàng)樣式,瀏覽器全兼容寫法

    這篇文章主要介紹了通過js為元素添加多項(xiàng)樣式,瀏覽器全兼容寫法,需要的朋友可以參考下
    2014-08-08
  • JS+Canvas繪制時(shí)鐘效果

    JS+Canvas繪制時(shí)鐘效果

    這篇文章主要為大家詳細(xì)介紹了基于javascript下使用canvas繪制時(shí)鐘的具體實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-05-05
  • 使用TypeScript在接口中定義靜態(tài)方法詳解

    使用TypeScript在接口中定義靜態(tài)方法詳解

    當(dāng)我們談?wù)撁嫦驅(qū)ο缶幊虝r(shí),最難理解的事情之一就是靜態(tài)屬性與實(shí)例屬性的概念,尤其是當(dāng)我們?cè)噲D在靜態(tài)類型的基礎(chǔ)上進(jìn)行動(dòng)態(tài)語言類型化時(shí),在本文中,我將主要介紹一下如何使用TypeScript在接口中定義靜態(tài)方法,需要的朋友可以參考下
    2023-10-10
  • JS中sort函數(shù)排序用法實(shí)例分析

    JS中sort函數(shù)排序用法實(shí)例分析

    這篇文章主要介紹了JS中sort函數(shù)排序用法,結(jié)合實(shí)例形式詳細(xì)分析了sort函數(shù)的功能、原理及實(shí)現(xiàn)數(shù)組排序的相關(guān)技巧,代碼中備有詳盡的注釋便于理解,需要的朋友可以參考下
    2016-06-06
  • js實(shí)現(xiàn)時(shí)分秒倒計(jì)時(shí)

    js實(shí)現(xiàn)時(shí)分秒倒計(jì)時(shí)

    這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)時(shí)分秒倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 超級(jí)兔子讓浮動(dòng)層消失的前因后果

    超級(jí)兔子讓浮動(dòng)層消失的前因后果

    超級(jí)兔子讓浮動(dòng)層消失的前因后果...
    2007-03-03
  • 原生js+css調(diào)節(jié)音量滑塊

    原生js+css調(diào)節(jié)音量滑塊

    這篇文章主要介紹了原生js+css調(diào)節(jié)音量滑塊,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • JavaScript代碼壓縮工具UglifyJS和Google Closure Compiler的基本用法

    JavaScript代碼壓縮工具UglifyJS和Google Closure Compiler的基本用法

    網(wǎng)上搜索了,目前主流的Js代碼壓縮工具主要有Uglify、YUI Compressor、Google Closure Compiler,簡(jiǎn)單試用了UglifyJS 和Google Closure Compiler 兩種工具的基本用法,需要的朋友可以參考下
    2020-04-04
  • 原生JavaScript之es6中Class的用法分析

    原生JavaScript之es6中Class的用法分析

    這篇文章主要介紹了原生JavaScript之es6中Class的用法,結(jié)合實(shí)例形式對(duì)比分析了es6與es5相關(guān)class定義、區(qū)別及使用技巧,需要的朋友可以參考下
    2020-02-02
  • iframe的父子窗口之間的對(duì)象相互調(diào)用基本用法

    iframe的父子窗口之間的對(duì)象相互調(diào)用基本用法

    iframe在使用時(shí)可能會(huì)涉及到父子窗口之間傳值和方法的相互調(diào)用,研究了一下其實(shí)非常簡(jiǎn)單,就那么幾個(gè)用法而已,在此與大家分享下,感興趣的朋友可以參考下
    2013-09-09

最新評(píng)論