JavaScript實現(xiàn)經(jīng)典貪吃蛇游戲
本文實例為大家分享了JavaScript實現(xiàn)經(jīng)典貪吃蛇游戲的具體代碼,供大家參考,具體內(nèi)容如下
主要使用單例模式,所有元素動態(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)建一個集合,存放蛇的前三節(jié),遍歷集合,創(chuàng)建蛇的大小和顏色,設(shè)置每個小節(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.動態(tài)蛇,從后向前遍歷存放蛇的每一節(jié)的集合snackk,將每小節(jié)的屬性從后想前傳遞,并且設(shè)置蛇頭移動方向,方向改變蛇的左邊距和上邊距也會跟著改變,再設(shè)置一個計時器,每100ms讓蛇動起來一次;
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)建食物。
在地圖上隨機產(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.檢測蛇頭和食物的位置,蛇頭吃到食物,蛇的長度變長,給snackk集合追加元素,接著在隨機創(chuàng)建食物,再檢測食物位置,再吃到食物;
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時,改變邊距到最大值;
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è)計游戲結(jié)束,貪吃蛇撞在自己的身體就死了,游戲結(jié)束,關(guān)閉計時器
當(dāng)蛇頭的上邊距,左邊距等于蛇身體某一塊的上編劇和左邊距時,游戲結(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);
}
}
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript代碼壓縮工具UglifyJS和Google Closure Compiler的基本用法
網(wǎng)上搜索了,目前主流的Js代碼壓縮工具主要有Uglify、YUI Compressor、Google Closure Compiler,簡單試用了UglifyJS 和Google Closure Compiler 兩種工具的基本用法,需要的朋友可以參考下2020-04-04
iframe的父子窗口之間的對象相互調(diào)用基本用法
iframe在使用時可能會涉及到父子窗口之間傳值和方法的相互調(diào)用,研究了一下其實非常簡單,就那么幾個用法而已,在此與大家分享下,感興趣的朋友可以參考下2013-09-09

