javascript貪吃蛇完整版(源碼)
javascript貪吃蛇完整版 注釋完整,面向?qū)ο?br />
<html>
<head>
<title>貪吃蛇 Snake v2.4</title>
<style>
body{
font-size:9pt;
}
table{
border-collapse: collapse;
border:solid #333 1px;
}
td{
height: 10px;
width: 10px;
font-size: 0px;
}
.filled{
background-color:blue;
}
</style>
</head>
<script>
function $(id){return document.getElementById(id);}
/**************************************************************
* javascript貪吃蛇 v2.4 <br />
* v2.4修正了蛇身顏色可以隨著蛇前進(jìn)而移動(dòng)
**************************************************************/
//貪吃蛇類
var Snake = {
tbl: null,
/**
* body: 蛇身,數(shù)組放蛇的每一節(jié),
* 數(shù)據(jù)結(jié)構(gòu){x:x0, y:y0, color:color0},
* x,y表示坐標(biāo),color表示顏色
**/
body: [],
//當(dāng)前移動(dòng)的方向,取值0,1,2,3, 分別表示向上,右,下,左, 按鍵盤方向鍵可以改變它
direction: 0,
//定時(shí)器
timer: null,
//速度
speed: 250,
//是否已經(jīng)暫停
paused: true,
//行數(shù)
rowCount: 30,
//列數(shù)
colCount: 30,
//初始化
init: function(){
var colors = ['red','orange','yellow','green','blue','purple','#ccc'];
this.tbl = $("main");
var x = 0;
var y = 0;
var colorIndex = 0;
//產(chǎn)生初始移動(dòng)方向
this.direction = Math.floor(Math.random()*4);
//構(gòu)造table
for(var row=0;row<this.rowCount;row++){
var tr=this.tbl.insertRow(-1);
for(var col=0;col<this.colCount;col++) {
var td=tr.insertCell(-1);
}
}
//產(chǎn)生20個(gè)松散節(jié)點(diǎn)
for(var i=0; i<10; i++){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex];
}
}
//產(chǎn)生蛇頭
while(true){
x = Math.floor(Math.random()*this.colCount);
y = Math.floor(Math.random()*this.rowCount);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = "black";
this.body.push({x:x,y:y,color:'black'});
break;
}
}
this.paused = true;
//添加鍵盤事件
document.onkeydown= function(e){
if (!e)e=window.event;
switch(e.keyCode | e.which | e.charCode){
case 13: {
if(Snake.paused){
Snake.move();
Snake.paused = false;
}
else{
//如果沒(méi)有暫停,則停止移動(dòng)
Snake.pause();
Snake.paused = true;
}
break;
}
}
}
},
//移動(dòng)
move: function(){
this.timer = setInterval(function(){
Snake.erase();
Snake.moveOneStep();
Snake.paint();
}, this.speed);
},
//移動(dòng)一節(jié)身體
moveOneStep: function(){
if(this.checkNextStep()==-1){
clearInterval(this.timer);
alert("Game over!/nPress Restart to continue.");
return;
}
if(this.checkNextStep()==1){
var _point = this.getNextPos();
var _x = _point.x;
var _y = _point.y;
var _color = this.getColor(_x,_y);
this.body.unshift({x:_x,y:_y,color:_color});
//因?yàn)槌粤艘粋€(gè)食物,所以再產(chǎn)生一個(gè)食物
this.generateDood();
return;
}
//window.status = this.toString();
var point = this.getNextPos();
//保留第一節(jié)的顏色
var color = this.body[0].color;
//顏色向前移動(dòng)
for(var i=0; i<this.body.length-1; i++){
this.body[i].color = this.body[i+1].color;
}
//蛇尾減一節(jié), 蛇尾加一節(jié),呈現(xiàn)蛇前進(jìn)的效果
this.body.pop();
this.body.unshift({x:point.x,y:point.y,color:color});
//window.status = this.toString();
},
//探尋下一步將走到什么地方
pause: function(){
clearInterval(Snake.timer);
this.paint();
},
getNextPos: function(){
var x = this.body[0].x;
var y = this.body[0].y;
var color = this.body[0].color;
//向上
if(this.direction==0){
y--;
}
//向右
else if(this.direction==1){
x++;
}
//向下
else if(this.direction==2){
y++;
}
//向左
else{
x--;
}
//返回一個(gè)坐標(biāo)
return {x:x,y:y};
},
//檢查將要移動(dòng)到的下一步是什么
checkNextStep: function(){
var point = this.getNextPos();
var x = point.x;
var y = point.y;
if(x<0||x>=this.colCount||y<0||y>=this.rowCount){
return -1;//觸邊界,游戲結(jié)束
}
for(var i=0; i<this.body.length; i++){
if(this.body[i].x==x&&this.body[i].y==y){
return -1;//碰到自己的身體,游戲結(jié)束
}
}
if(this.isCellFilled(x,y)){
return 1;//有東西
}
return 0;//空地
},
//擦除蛇身
erase: function(){
for(var i=0; i<this.body.length; i++){
this.eraseDot(this.body[i].x, this.body[i].y);
}
},
//繪制蛇身
paint: function(){
for(var i=0; i<this.body.length; i++){
this.paintDot(this.body[i].x, this.body[i].y,this.body[i].color);
}
},
//擦除一節(jié)
eraseDot: function(x,y){
this.tbl.rows[y].cells[x].style.backgroundColor = "";
},
paintDot: function(x,y,color){
this.tbl.rows[y].cells[x].style.backgroundColor = color;
},
//得到一個(gè)坐標(biāo)上的顏色
getColor: function(x,y){
return this.tbl.rows[y].cells[x].style.backgroundColor;
},
//用于調(diào)試
toString: function(){
var str = "";
for(var i=0; i<this.body.length; i++){
str += "x:" + this.body[i].x + " y:" + this.body[i].y + " color:" + this.body[i].color + " - ";
}
return str;
},
//檢查一個(gè)坐標(biāo)點(diǎn)有沒(méi)有被填充
isCellFilled: function(x,y){
if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){
return false;
}
return true;
},
//重新開(kāi)始
restart: function(){
if(this.timer){
clearInterval(this.timer);
}
for(var i=0; i<this.rowCount;i++){
this.tbl.deleteRow(0);
}
this.body = [];
this.init();
this.speed = 250;
},
//加速
speedUp: function(time){
if(!this.paused){
if(this.speed+time<10||this.speed+time>2000){
return;
}
this.speed +=time;
this.pause();
this.move();
}
},
//產(chǎn)生食物。
generateDood: function(){
var colors = ['red','orange','yellow','green','blue','purple','#ccc'];
var x = Math.floor(Math.random()*this.colCount);
var y = Math.floor(Math.random()*this.rowCount);
var colorIndex = Math.floor(Math.random()*7);
if(!this.isCellFilled(x,y)){
this.tbl.rows[y].cells[x].style.backgroundColor = colors[colorIndex];
}
}
};
</script>
<body onload="Snake.init();">
/*************************************************************<br />
* javascript貪吃蛇 v2.4<br />
**************************************************************/<br />
<table id="main" border="1" cellspacing="0" cellpadding="0"></table>
<input type="button" id="btn" value="開(kāi)始/暫停" />點(diǎn)左邊按鈕或按Enter開(kāi)始/暫停游戲<br />
<input type="button" id="reset" value="重新開(kāi)始" /><br />
<input type="button" id="upSpeed" value="加速" />點(diǎn)左邊按鈕或按Ctrl + ↑加速<br />
<input type="button" id="downSpeed" value="減速" />點(diǎn)左邊按鈕或按Ctrl + ↓減速
<script>
$('btn').onclick = function(){
if(Snake.paused){
Snake.move();
Snake.paused = false;
}
else{
Snake.pause();
Snake.paused = true;
}
};
$("reset").onclick = function(){
Snake.restart();
this.blur();
};
$("upSpeed").onclick = function(){
Snake.speedUp(-20);
};
$("downSpeed").onclick = function(){
Snake.speedUp(20);
};
</script>
</body>
</html>
相關(guān)文章
jQuery animate()實(shí)現(xiàn)背景色漸變效果的處理方法【使用jQuery.color.js插件】
這篇文章主要介紹了jQuery animate()實(shí)現(xiàn)背景色漸變效果的處理方法,結(jié)合實(shí)例形式分析了jQuery顏色插件jquery.color.js實(shí)現(xiàn)背景色漸變的簡(jiǎn)單操作技巧,需要的朋友可以參考下2017-03-03echarts實(shí)現(xiàn)響應(yīng)式定位和布局
這篇文章介紹了echarts實(shí)現(xiàn)響應(yīng)式定位和布局的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06three.js中g(shù)sap動(dòng)畫庫(kù)實(shí)現(xiàn)物體的動(dòng)畫
本文主要介紹了three.js中g(shù)sap動(dòng)畫庫(kù)實(shí)現(xiàn)物體的動(dòng)畫,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07js實(shí)現(xiàn)從數(shù)組里隨機(jī)獲取元素
這篇文章主要介紹了js實(shí)現(xiàn)從數(shù)組里隨機(jī)獲取元素的方法,以及個(gè)人封裝的js代碼分享,十分的實(shí)用,這里推薦給小伙伴們2015-01-01JS交互點(diǎn)擊WKWebView中的圖片實(shí)現(xiàn)預(yù)覽效果
這篇文章主要介紹了JS交互點(diǎn)擊WKWebView中的圖片實(shí)現(xiàn)預(yù)覽效果,需要的朋友可以參考下2018-01-01Bootstrap的popover(彈出框)2秒后定時(shí)消失的實(shí)現(xiàn)代碼
Bootstrap Popover(彈出框)是使用定制的 Jquery 插件創(chuàng)建的。它可以用來(lái)顯示任何元素的一些信息。這篇文章主要介紹了Bootstrap的popover(彈出框)2秒后定時(shí)消失功能,需要的朋友參考下2017-02-02Layui table 組件的使用之初始化加載數(shù)據(jù)、數(shù)據(jù)刷新表格、傳參數(shù)
這篇文章主要介紹了Layui table 組件的使用之初始化加載數(shù)據(jù)、數(shù)據(jù)刷新表格、傳參數(shù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下2017-09-09JavaScript如何實(shí)現(xiàn)數(shù)組按屬性分組
在JavaScript中,有多種方法可以對(duì)數(shù)組按屬性進(jìn)行分組,這篇文章主要為大家至少介紹了6種常見(jiàn)的方法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-08-08