JS貪食蛇遊戲,個人練習之用,放在這備份一下, 複製代碼 代碼如下: JS贪吃蛇-练习 <br>#pannel table { <br>border-collapse: 折りたたむ; <br>} <br>#pannel table td { <br>border: 1px ソリッド #808080; <br>幅: 10px; <br>高さ: 10px; <br>フォントサイズ: 0; <br>行の高さ: 0; <br>オーバーフロー: 非表示; <br>} <br>#pannel table .snake { <br>背景色: 緑; <br>} <br>#pannel table .food { <br>背景色: 青; <br>} <br> <br>var Direction = new function () { <br>this.UP = 38; <br>this.RIGHT = 39; <br>this.DOWN = 40; <br>this.LEFT = 37; <br>}; <br>var Common = new function () { <br>this.width = 20; /*水平方向方格*/ <br>this.height = 20; /*垂直方向方格*/ <br>this.speed = 250; /* 速度值越小越快*/ <br>this.workThread = null; <br>}; <br>var Main = new function () { <br>var control = new Control(); <br>window.onload = function () { <br>control.Init("pannel"); <br>/*开始按钮*/ <br>document.getElementById("btnStart").onclick = function () { <br>control.Start(); <br>this.disabled = true; <br>document.getElementById("selSpeed").disabled = true; <br>document.getElementById("selSize").disabled = true; <br>}; <br>/*调速度按钮*/ <br>document.getElementById("selSpeed").onchange = function () { <br>Common.speed = this.value; <br>} <br>/*调大小按钮*/ <br>document.getElementById("selSize").onchange = function () { <br>Common.width = this.value; <br>Common.height = this.value; <br>control.Init("パネル"); <br>} <br>}; <br>}; <br>/*制御制器*/ <br>function Control() { <br>this.snake = new Snake(); <br>this.food = new Food(); <br>/*初開始化関数数,创建表格*/ <br>this.Init = function (pid) { <br>var html = []; <br>html.push(""); <br>for (var y = 0; y <common.height y>html.push("<tr>"); <br>for (var x = 0; x < Common.width; x ) { <BR>html.push('<td id="box_' x "_" y '"> </td> '); <br>} <br>html.push("</tr>"); <br>} <br>html.push("</table>"); <br>this.pannel = document.getElementById(pid); <br>this.pannel.innerHTML = html.join(""); <br>}; <br>/*开始游戏 - 监听键盘、创建食品、刷新界面線程*/ <br>this.Start = function () { <br>var me = this; <br>this.MoveSnake = function (ev) { <br>var evt = window.event || ev; <br>me.snake.SetDir(evt.keyCode); <br>}; <br>try { <br>document.attachEvent("onkeydown", this.MoveSnake); <br>} catch (e) { <br>document.addEventListener("keydown", this.MoveSnake, false); <br>} <br>this.food.Create(); <br>Common.workThread = setInterval(function () { <br>me.snake.Eat(me.food); me.snake.Move(); <br>}, Common.speed); <br>}; <br>} <br>/*蛇*/ <br>function Snake() { <br>this.isDone = false; <br>this.dir = Direction.RIGHT; <br>this.pos = new Array(new Position()); <br>/*移動 - 擦除尾部,方向前移動,判断游戏结束(咬到自己或者移出边界)*/ <br>this.Move = function () { <br>document.getElementById("box_" this .pos[0].X "_" this.pos[0].Y).className = ""; <br>//すべての方向前移動一步 <br>for (var i = 0; i <this.pos.length i>this.pos[i].X = this.pos[ i 1].X; <br>this.pos[i].Y = this.pos[i 1].Y; <br>} <br>// 新たに設置する位置 <br>var head = this.pos[this.pos.length - 1]; <br>switch (this.dir) { <br>case Direction.UP: <br>head.Y--; <br>休憩; <br>case Direction.RIGHT: <br>head.X ; <br>休憩; <br>case Direction.DOWN: <br>head.Y ; <br>休憩; <br>case Direction.LEFT: <br>head.X--; <br>休憩; <br>} <br>this.pos[this.pos.length - 1] = 頭; <br>//遍历画蛇,同時判断游戏结束 <br>for (var i = 0; i <this.pos.length i>var isExits = false; <br>for (var j = i 1; j <this.pos.length j>if (this.pos[j].X == this.pos[i].X && this.pos[ j].Y == this.pos[i].Y) { <br>isExits = true; <br>休憩; <br>} <br>if (isExits) { this.Over();/*咬自己*/ Break; <br>var obj = document.getElementById("box_" this.pos[i].X "_" this.pos[i].Y); <br>if (obj) obj.className = "ヘビ"; else { this.Over();/*移出边界*/ Break; } <br>} <br>this.isDone = true; <br>}; <br>/*游戏结束*/ <br>this.Over = function () { <br>clearInterval(Common.workThread); <br>alert("游戏结束!"); <br>} <br>/*吃食物*/ <br>this.Eat = function (food) { <br>var head = this.pos[this.pos.length - 1]; <br> var isEat = false; <br>switch (this.dir) { <br>case Direction.UP: <br>if (head.X == food.pos.X && head.Y == food.pos.Y 1 ) isEat = true; <br>break; <br>case Direction.RIGHT: <br>if (head.Y == food.pos.Y && head.X == food.pos.X - 1) ; <br>break; <br>case Direction.DOWN: <br>if (head.X == food.pos.X && head.Y - 1) isEat = true; Break; <br>case Direction.LEFT: <br>if (head.Y == food.pos.Y && head.X == food.pos.X 1) isEat = true; <br>if (isEat) { <br>this.pos[this.pos.length] = new Position(food.pos.X, food.pos.Y); <br>food.Create(this.pos); ; <br>} <br>}; <br>/*制御移動方向*/ <br>this.SetDir = function (dir) { <br>switch (dir) { <br>case Direction.UP: <br>if (this.isDone && this.dir != Direction.DOWN) { this.dir = dir; this.isDone = false; } <br>break; <br>case Direction.RIGHT: <br>if (this. isDone && this.dir != Direction.LEFT) { this.dir = dir; this.isDone = false } <br>break; <br>case Direction.DOWN: <br>if (this.isDone && this.dir != Direction.UP) { this.dir = dir; this.isDone = false; } <br>休憩; <br>case Direction.LEFT: <br>if (this.isDone && this.dir != Direction.RIGHT) { this.dir = dir; this.isDone = false; } <br>休憩; <br>} <br>}; <br>} <br>/*食品*/ <br>function Food() { <br>this.pos = new Position(); <br>/*创建食品 - 随机位置创建立*/ <br>this.Create = function (pos) { <br>document.getElementById("box_" this.pos.X "_" this.pos.Y) .className = ""; <br>var x = 0、y = 0、isCover = false; <br>/*蛇の位置*/ <br>do { <br>x = parseInt(Math.random() * (Common.width - 1)); <br>y = parseInt(Math.random() * (Common.height - 1)); <br>isCover = false; <br>if (配列の pos インスタンス) { <br>for (var i = 0; i < pos.length; i ) { <BR>if (x == pos[i].X && y == pos[ i].Y) {<BR>isCover = true; <BR>休憩; <BR>} <BR>} <BR>} <BR>} while (isCover); <BR>this.pos = 新しい位置(x, y); <BR>document.getElementById("box_" x "_" y).className = "食べ物"; <BR>}; <BR>} <BR>関数 Position(x, y) { <BR>this.X = 0; <BR>this.Y = 0; <BR>if (arguments.length >= 1) this.X = x; <BR>if (arguments.length >= 2) this.Y = y; <BR>} <BR> 20*20 30*30 40*40 速度-低速 速度-中 速度-快