JavaScript實現網頁版貪吃蛇游戲
更新時間:2021年07月28日 11:14:31 作者:晚安小點點
這篇文章主要為大家詳細介紹了JavaScript實現網頁版貪吃蛇游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了JavaScript實現網頁貪吃蛇游戲的具體代碼,供大家參考,具體內容如下
<!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;
//定義一個方塊的構造函數
var Block=function(col,row,size)
{
this.col=col;
this.row=row;
this.size=size;
};
//定義Block函數的原型方法draw;
Block.prototype.draw =function()
{
ctx.fillRect(this.col*this.size,this.row*this.size,this.size,this.size)
}
//定義對象蛇
var snake ={
body:[
new Block(20,20,10),
new Block(20,21,10),
new Block(20,22,10)
],
direction:"right",
};
//定義畫蛇的函數
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)建蘋果對象
var apple={
posX:Math.floor(Math.random()*40),
posY:Math.floor(Math.random()*40),
sizeR:5
}
//畫蘋果函數
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";
};
//結束
var gameover=function()
{
ctx.font="60px Comic Sans MS";
ctx.textAlign="center";
ctx.textBaseline="middle";
ctx.fillText("GAME OVER!",200,200)
};
//定時功能
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>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
scrapyd schedule.json setting 傳入多個值問題
這篇文章主要介紹了scrapyd schedule.json setting 傳入多個值,本文給出了問題分析及思路解決方案,非常不錯,具有一定的參考借鑒價值,需要的朋友參考下吧2019-08-08
JavaScript精煉之構造函數 Constructor及Constructor屬性詳解
對象的constructor屬性用于返回創(chuàng)建該對象的函數,也就是我們常說的構造函數,除了創(chuàng)建對象,構造函數(constructor) 還做了另一件有用的事情—自動為創(chuàng)建的新對象設置了原型對象(prototype object)2015-11-11
深入理解JavaScript系列(34):設計模式之命令模式詳解
這篇文章主要介紹了深入理解JavaScript系列(34):設計模式之命令模式詳解,命令模式(Command)的定義是:用于將一個請求封裝成一個對象,從而使你可用不同的請求對客戶進行參數化,對請求排隊或者記錄請求日志,以及執(zhí)行可撤銷的操作,需要的朋友可以參考下2015-03-03

