亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)版貪吃蛇游戲

 更新時(shí)間:2021年07月28日 11:14:31   作者:晚安小點(diǎn)點(diǎn)  
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)網(wǎng)頁(yè)版貪吃蛇游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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

<!DOCTYPE html>
<html>
<head><title>貪吃蛇</title>
</head>
<body>
<canvas id="canvas" width="400" height="400"></canvas>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<script>
var canv=document.getElementById("canvas");
var ctx=canv.getContext("2d");
var score=0;
//定義一個(gè)方塊的構(gòu)造函數(shù)
var Block=function(col,row,size)
{
  this.col=col;
  this.row=row;
  this.size=size;
    };
//定義Block函數(shù)的原型方法draw;
Block.prototype.draw =function()
{
  ctx.fillRect(this.col*this.size,this.row*this.size,this.size,this.size)
   }
//定義對(duì)象蛇
var snake ={
  body:[
    new Block(20,20,10),
    new Block(20,21,10),
    new Block(20,22,10)
   ],
  direction:"right",
  };

//定義畫(huà)蛇的函數(shù)
snake.draw=function()
{
   for(var i=0;i<this.body.length;i++)
  {
     this.body[i].draw();
        }
   };

snake.move = function()
         {
          var head = this.body[0];

if(snake.direction=="right")
     {    
     var newhead=new Block(head.col+1,head.row,head.size)
            }
  
  if(snake.direction =="left")
   
     { 
     var newhead = new Block(head.col-1,head.row,head.size); 
           }  
 if(snake.direction=="up")
     {
     var newhead=new Block(head.col,head.row-1,head.size)
           }
    if(snake.direction=="down")
     {
     var newhead=new Block(head.col,head.row+1,head.size)
           } 
          if(newhead.col<0 || newhead.col>39 )
          {
           clearInterval(intervalId);
           gameover();
          }
          
          if(newhead.row<0 || newhead.row>39 )
          {
           clearInterval(intervalId);
           gameover();
          }
 for(var i=0;i<this.body.length;i++)
{
    if(this.body[i].col==newhead.col &&this.body[i].row==newhead.row)
  {
    clearInterval(intervalId);
    gameover();
      }
          }         
     this.body.unshift(newhead);
     if(newhead.col==apple.posX &&newhead.row==apple.posY)
{  
    score=score+100;
    while(true)
  {
     var checkApple =false;
     apple.posX=Math.floor(Math.random()*40);
     apple.posY=Math.floor(Math.random()*40);
     for(var i=0; i<this.body.length;i++)
   {
     if(this.body[i].col==apple.posX &&this.body[i].row==apple.posY)
          checkApple=true;
                        }
       if(!checkApple)
       break;
      }  
  }
else{
     this.body.pop();
        }         
         };
//創(chuàng)建蘋(píng)果對(duì)象
var apple={
    posX:Math.floor(Math.random()*40),
    posY:Math.floor(Math.random()*40),
    sizeR:5
}
//畫(huà)蘋(píng)果函數(shù)
apple.draw=function()
{
  ctx.fillStyle="Green";
  ctx.beginPath();
  ctx.arc(this.posX*10+5,this.posY*10+5,5,0,Math.PI*2,false);
  ctx.fill();
  ctx.fillStyle="Black";
     };
//結(jié)束
var gameover=function()
{
  ctx.font="60px Comic Sans MS";
  ctx.textAlign="center";
  ctx.textBaseline="middle";
  ctx.fillText("GAME OVER!",200,200)
    };
//定時(shí)功能
var intervalId=setInterval (function ()
{
   ctx.clearRect(0,0,400,400);
   ctx.font="20px Arial";
   ctx.fillText("Score:"+score,5,15);
   snake.draw();
   snake.move();
   apple.draw();
   ctx.strokeRect(0,0,400,400);
    },200);
//貪吃蛇的按鍵控制
$("body").keydown(function(event)
{
   console.log(event.keyCode);
    if(event.keyCode==37 &&snake.direction!="right")
     {
    snake.direction="left";
         }
    if(event.keyCode==38 &&snake.direction!="down")
     {
    snake.direction="up";
        }
    if(event.keyCode==39 &&snake.direction!="left")
     {
     snake.direction="right";
         }
     if(event.keyCode==40 &&snake.direction!="up")
     {
     snake.direction="down";
         }
              }
);
</script>
</body>
</html>

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

相關(guān)文章

  • JQuery 在表單提交之前修改 提交的值

    JQuery 在表單提交之前修改 提交的值

    本文介紹在表單提交之前修改提交的值的方法,希望給需要的朋友一些幫助。
    2016-04-04
  • scrapyd schedule.json setting 傳入多個(gè)值問(wèn)題

    scrapyd schedule.json setting 傳入多個(gè)值問(wèn)題

    這篇文章主要介紹了scrapyd schedule.json setting 傳入多個(gè)值,本文給出了問(wèn)題分析及思路解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2019-08-08
  • javascript正則表達(dá)式之search()用法實(shí)例

    javascript正則表達(dá)式之search()用法實(shí)例

    這篇文章主要介紹了javascript正則表達(dá)式之search()用法,實(shí)例分析了search()的使用技巧,需要的朋友可以參考下
    2015-01-01
  • JS實(shí)現(xiàn)非首屏圖片延遲加載的示例

    JS實(shí)現(xiàn)非首屏圖片延遲加載的示例

    下面小編就為大家分享一篇用JS實(shí)現(xiàn)非首屏圖片延遲加載的示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • 實(shí)現(xiàn)JavaScript中數(shù)據(jù)響應(yīng)的方法總結(jié)

    實(shí)現(xiàn)JavaScript中數(shù)據(jù)響應(yīng)的方法總結(jié)

    JavaScript 數(shù)據(jù)響應(yīng)是一種重要的前端開(kāi)發(fā)概念,是指在應(yīng)用程序中的數(shù)據(jù)發(fā)生變化時(shí),能夠自動(dòng)更新與這些數(shù)據(jù)相關(guān)的用戶界面(UI)部分的能力,本文我們來(lái)總結(jié)一下目前可以簡(jiǎn)單實(shí)現(xiàn) JavaScript 中的數(shù)據(jù)響應(yīng)的方法,需要的朋友可以參考下
    2023-09-09
  • JavaScript精煉之構(gòu)造函數(shù) Constructor及Constructor屬性詳解

    JavaScript精煉之構(gòu)造函數(shù) Constructor及Constructor屬性詳解

    對(duì)象的constructor屬性用于返回創(chuàng)建該對(duì)象的函數(shù),也就是我們常說(shuō)的構(gòu)造函數(shù),除了創(chuàng)建對(duì)象,構(gòu)造函數(shù)(constructor) 還做了另一件有用的事情—自動(dòng)為創(chuàng)建的新對(duì)象設(shè)置了原型對(duì)象(prototype object)
    2015-11-11
  • js逆向解密之網(wǎng)絡(luò)爬蟲(chóng)

    js逆向解密之網(wǎng)絡(luò)爬蟲(chóng)

    在本篇內(nèi)容里小編給大家整理的是關(guān)于js逆向解密之網(wǎng)絡(luò)爬蟲(chóng)的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們參考下。
    2019-05-05
  • js判斷undefined類(lèi)型示例代碼

    js判斷undefined類(lèi)型示例代碼

    這篇文章主要介紹了js判斷undefined類(lèi)型的實(shí)現(xiàn)方法,需要的朋友可以參考下
    2014-02-02
  • 深入理解JavaScript系列(34):設(shè)計(jì)模式之命令模式詳解

    深入理解JavaScript系列(34):設(shè)計(jì)模式之命令模式詳解

    這篇文章主要介紹了深入理解JavaScript系列(34):設(shè)計(jì)模式之命令模式詳解,命令模式(Command)的定義是:用于將一個(gè)請(qǐng)求封裝成一個(gè)對(duì)象,從而使你可用不同的請(qǐng)求對(duì)客戶進(jìn)行參數(shù)化,對(duì)請(qǐng)求排隊(duì)或者記錄請(qǐng)求日志,以及執(zhí)行可撤銷(xiāo)的操作,需要的朋友可以參考下
    2015-03-03
  • javascript AutoScroller 函數(shù)類(lèi)

    javascript AutoScroller 函數(shù)類(lèi)

    javascript AutoScroller 自動(dòng)滾動(dòng)類(lèi)代碼,學(xué)習(xí)類(lèi)的朋友可以參考下。
    2009-05-05

最新評(píng)論