javascript貪吃蛇完整版(源碼)
更新時間:2013年12月09日 09:57:47 投稿:jingxian
這篇文章主要是對javascript貪吃蛇完整版(源碼)進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助
javascript貪吃蛇完整版 注釋完整,面向?qū)ο?br />
復(fù)制代碼 代碼如下:
<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)而移動
**************************************************************/
//貪吃蛇類
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)前移動的方向,取值0,1,2,3, 分別表示向上,右,下,左, 按鍵盤方向鍵可以改變它
direction: 0,
//定時器
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)生初始移動方向
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個松散節(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{
//如果沒有暫停,則停止移動
Snake.pause();
Snake.paused = true;
}
break;
}
}
}
},
//移動
move: function(){
this.timer = setInterval(function(){
Snake.erase();
Snake.moveOneStep();
Snake.paint();
}, this.speed);
},
//移動一節(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)槌粤艘粋€食物,所以再產(chǎn)生一個食物
this.generateDood();
return;
}
//window.status = this.toString();
var point = this.getNextPos();
//保留第一節(jié)的顏色
var color = this.body[0].color;
//顏色向前移動
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--;
}
//返回一個坐標(biāo)
return {x:x,y:y};
},
//檢查將要移動到的下一步是什么
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;
},
//得到一個坐標(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;
},
//檢查一個坐標(biāo)點(diǎn)有沒有被填充
isCellFilled: function(x,y){
if(this.tbl.rows[y].cells[x].style.backgroundColor == ""){
return false;
}
return true;
},
//重新開始
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="開始/暫停" />點(diǎn)左邊按鈕或按Enter開始/暫停游戲<br />
<input type="button" id="reset" value="重新開始" /><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)文章
bootstrap實(shí)現(xiàn)二級下拉菜單效果
這篇文章主要為大家詳細(xì)介紹了bootstrap實(shí)現(xiàn)二級下拉菜單效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
原生JS實(shí)現(xiàn)圖片無縫滾動方法(附帶封裝的運(yùn)動框架)
下面小編就為大家?guī)硪黄鶭S實(shí)現(xiàn)圖片無縫滾動方法(附帶封裝的運(yùn)動框架)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
JavaScript程序設(shè)計之JS調(diào)試
這篇文章主要介紹了JavaScript程序設(shè)計中的重要環(huán)節(jié):JS調(diào)試,本文通過一個加法器,介紹JS如何調(diào)試,感興趣的小伙伴們可以參考一下2015-12-12
js實(shí)現(xiàn)點(diǎn)擊圖片自動提交action的簡單方法
下面小編就為大家?guī)硪黄猨s實(shí)現(xiàn)點(diǎn)擊圖片自動提交action的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
JS 中實(shí)現(xiàn)一個串型異步函數(shù)隊列
這篇文章主要介紹了JS 中實(shí)現(xiàn)一個串型異步函數(shù)隊列,文章通過async/await 串型請求展開詳情,具有一定的參考價值,需要的朋友可以參考一下2022-07-07
echarts地圖繪制自定義標(biāo)記實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于echarts地圖繪制自定義標(biāo)記實(shí)現(xiàn)的相關(guān)資料,ECharts地圖是一個功能強(qiáng)大的數(shù)據(jù)可視化工具,基于百度ECharts開源項目開發(fā)而成,它主要用于在網(wǎng)頁中展示各種地理數(shù)據(jù)和地圖的信息,需要的朋友可以參考下2023-11-11
JavaScript使用structuredClone實(shí)現(xiàn)深拷貝
在JavaScript中,實(shí)現(xiàn)深拷貝的方式有很多種,每種方式都有其優(yōu)點(diǎn)和缺點(diǎn),今天介紹一種原生JavaScript提供的structuredClone實(shí)現(xiàn)深拷貝,文中通過代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下2024-03-03

