JS 스네이크 게임 개인 연습용으로 여기에 넣어 백업해두세요 코드 복사 코드는 다음과 같습니다. <머리> JS贪吃蛇-练习 <br>#pannel 테이블 { <br>border-collapse: 축소; <br>} <br>#pannel 테이블 td { <br>테두리: 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 방향 = 새 함수() { <br>this.UP = 38; <br>this.RIGHT = 39; <br>this.DOWN = 40; <br>this.LEFT = 37; <br>}; <br>var Common = 새 함수() { <br>this.width = 20; /*平方向方格数*/ <br>this.height = 20; /*垂直方向方格数*/ <br>this.speed = 250; /*速島 值越小越快*/ <br>this.workThread = null; <br>}; <br>var Main = 새 함수() { <br>var control = 새 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>공통.높이 = this.value; <br>control.Init("패널"); <br>} <br>}; <br>}; <br>/*控 제조사*/ <br>function Control() { <br>this.snake = new Snake(); <br>this.food = 새로운 음식(); <br>/*初始化函数,创建表格*/ <br>this.Init = function (pid) { <br>var html = []; <br>html.push("<테이블>"); <br>for (var y = 0; 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>/*开始游戏 - 监听键盘, 创建food, 刷新界face线程*/ <br>this.Start = function () { <br>var me = this; <br>this.MoveSnake = 함수(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 = 방향.RIGHT; <br>this.pos = new Array(new Position()); <br>/*移动 - 擦除尾부,向前移动,判断游戏结束(咬到自己或者移take边界)*/ <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 - 1; i ) { <BR>this.pos[i].X = this.pos[ 나는 1].X; <BR>this.pos[i].Y = this.pos[i 1].Y; <BR>} <BR>//중요한 새로운 정보 <BR>var head = this.pos[this.pos.length - 1]; <BR>스위치(this.dir) { <BR>case Direction.UP: <BR>head.Y--; <BR>휴식; <BR>케이스 방향.RIGHT: <BR>head.X ; <BR>휴식; <BR>case 방향.DOWN: <BR>head.Y ; <BR>휴식; <BR>케이스 방향.LEFT: <BR>head.X--; <BR>휴식; <BR>} <BR>this.pos[this.pos.length - 1] = 머리; <BR>//遍历画蛇,同时判断游戏结束 <BR>for (var i = 0; i < this.pos.length; i ) { <BR>var isExits = false; <BR>for (var j = i 1; j < this.pos.length; j ) <BR>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();/*移take边界*/ break; } <BR>} <BR>this.isDone = true; <BR>}; <BR>/*游戏结束*/ <BR>this.Over = function () { <BR>clearInterval(Common.workThread); <BR>alert("游戏结束!"); <BR>} <BR>/*吃식물*/ <BR>this.Eat = 함수(음식) { <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) isEat = true ; <BR>break; <BR>case Direction.DOWN: <BR>if (head.X == food.pos.X && head.Y == food.pos.Y - 1) isEat = true; break; <BR>case Direction.LEFT: <BR>if (head.Y == food.pos.Y && head.X == food.pos.X 1) isEat = <BR>break; } <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>/*控system移动方向*/ <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 = 거짓; } <BR>휴식; <BR>case Direction.LEFT: <BR>if (this.isDone && this.dir != Direction.RIGHT) { this.dir = dir; this.isDone = 거짓; } <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 = parsInt(Math.random() * (Common.height - 1)); <BR>isCover = 거짓; <BR>if (pos 인스턴스of 배열) { <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 = "food"; <BR>}; <BR>} <BR>함수 위치(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*40 速島-慢 速島-中 速degree-快