JS写的贪吃蛇游戏(个人练习)


Posted in Javascript onJuly 08, 2013

JS贪吃蛇游戏,个人练习之用,放在这备份一下,
JS写的贪吃蛇游戏(个人练习) 

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>JS贪吃蛇-练习</title> 
<style type="text/css"> 
#pannel table { 
border-collapse: collapse; 
} 
#pannel table td { 
border: 1px solid #808080; 
width: 10px; 
height: 10px; 
font-size: 0; 
line-height: 0; 
overflow: hidden; 
} 
#pannel table .snake { 
background-color: green; 
} 
#pannel table .food { 
background-color: blue; 
} 
</style> 
<script type="text/javascript"> 
var Direction = new function () { 
this.UP = 38; 
this.RIGHT = 39; 
this.DOWN = 40; 
this.LEFT = 37; 
}; 
var Common = new function () { 
this.width = 20; /*水平方向方格数*/ 
this.height = 20; /*垂直方向方格数*/ 
this.speed = 250; /*速度 值越小越快*/ 
this.workThread = null; 
}; 
var Main = new function () { 
var control = new Control(); 
window.onload = function () { 
control.Init("pannel"); 
/*开始按钮*/ 
document.getElementById("btnStart").onclick = function () { 
control.Start(); 
this.disabled = true; 
document.getElementById("selSpeed").disabled = true; 
document.getElementById("selSize").disabled = true; 
}; 
/*调速度按钮*/ 
document.getElementById("selSpeed").onchange = function () { 
Common.speed = this.value; 
} 
/*调大小按钮*/ 
document.getElementById("selSize").onchange = function () { 
Common.width = this.value; 
Common.height = this.value; 
control.Init("pannel"); 
} 
}; 
}; 
/*控制器*/ 
function Control() { 
this.snake = new Snake(); 
this.food = new Food(); 
/*初始化函数,创建表格*/ 
this.Init = function (pid) { 
var html = []; 
html.push("<table>"); 
for (var y = 0; y < Common.height; y++) { 
html.push("<tr>"); 
for (var x = 0; x < Common.width; x++) { 
html.push('<td id="box_' + x + "_" + y + '"> </td>'); 
} 
html.push("</tr>"); 
} 
html.push("</table>"); 
this.pannel = document.getElementById(pid); 
this.pannel.innerHTML = html.join(""); 
}; 
/*开始游戏 - 监听键盘、创建食物、刷新界面线程*/ 
this.Start = function () { 
var me = this; 
this.MoveSnake = function (ev) { 
var evt = window.event || ev; 
me.snake.SetDir(evt.keyCode); 
}; 
try { 
document.attachEvent("onkeydown", this.MoveSnake); 
} catch (e) { 
document.addEventListener("keydown", this.MoveSnake, false); 
} 
this.food.Create(); 
Common.workThread = setInterval(function () { 
me.snake.Eat(me.food); me.snake.Move(); 
}, Common.speed); 
}; 
} 
/*蛇*/ 
function Snake() { 
this.isDone = false; 
this.dir = Direction.RIGHT; 
this.pos = new Array(new Position()); 
/*移动 - 擦除尾部,向前移动,判断游戏结束(咬到自己或者移出边界)*/ 
this.Move = function () { 
document.getElementById("box_" + this.pos[0].X + "_" + this.pos[0].Y).className = ""; 
//所有 向前移动一步 
for (var i = 0; i < this.pos.length - 1; i++) { 
this.pos[i].X = this.pos[i + 1].X; 
this.pos[i].Y = this.pos[i + 1].Y; 
} 
//重新设置头的位置 
var head = this.pos[this.pos.length - 1]; 
switch (this.dir) { 
case Direction.UP: 
head.Y--; 
break; 
case Direction.RIGHT: 
head.X++; 
break; 
case Direction.DOWN: 
head.Y++; 
break; 
case Direction.LEFT: 
head.X--; 
break; 
} 
this.pos[this.pos.length - 1] = head; 
//遍历画蛇,同时判断游戏结束 
for (var i = 0; i < this.pos.length; i++) { 
var isExits = false; 
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) { 
isExits = true; 
break; 
} 
if (isExits) { this.Over();/*咬自己*/ break; } 
var obj = document.getElementById("box_" + this.pos[i].X + "_" + this.pos[i].Y); 
if (obj) obj.className = "snake"; else { this.Over();/*移出边界*/ break; } 
} 
this.isDone = true; 
}; 
/*游戏结束*/ 
this.Over = function () { 
clearInterval(Common.workThread); 
alert("游戏结束!"); 
} 
/*吃食物*/ 
this.Eat = function (food) { 
var head = this.pos[this.pos.length - 1]; 
var isEat = false; 
switch (this.dir) { 
case Direction.UP: 
if (head.X == food.pos.X && head.Y == food.pos.Y + 1) isEat = true; 
break; 
case Direction.RIGHT: 
if (head.Y == food.pos.Y && head.X == food.pos.X - 1) isEat = true; 
break; 
case Direction.DOWN: 
if (head.X == food.pos.X && head.Y == food.pos.Y - 1) isEat = true; 
break; 
case Direction.LEFT: 
if (head.Y == food.pos.Y && head.X == food.pos.X + 1) isEat = true; 
break; 
} 
if (isEat) { 
this.pos[this.pos.length] = new Position(food.pos.X, food.pos.Y); 
food.Create(this.pos); 
} 
}; 
/*控制移动方向*/ 
this.SetDir = function (dir) { 
switch (dir) { 
case Direction.UP: 
if (this.isDone && this.dir != Direction.DOWN) { this.dir = dir; this.isDone = false; } 
break; 
case Direction.RIGHT: 
if (this.isDone && this.dir != Direction.LEFT) { this.dir = dir; this.isDone = false; } 
break; 
case Direction.DOWN: 
if (this.isDone && this.dir != Direction.UP) { this.dir = dir; this.isDone = false; } 
break; 
case Direction.LEFT: 
if (this.isDone && this.dir != Direction.RIGHT) { this.dir = dir; this.isDone = false; } 
break; 
} 
}; 
} 
/*食物*/ 
function Food() { 
this.pos = new Position(); 
/*创建食物 - 随机位置创建立*/ 
this.Create = function (pos) { 
document.getElementById("box_" + this.pos.X + "_" + this.pos.Y).className = ""; 
var x = 0, y = 0, isCover = false; 
/*排除蛇的位置*/ 
do { 
x = parseInt(Math.random() * (Common.width - 1)); 
y = parseInt(Math.random() * (Common.height - 1)); 
isCover = false; 
if (pos instanceof Array) { 
for (var i = 0; i < pos.length; i++) { 
if (x == pos[i].X && y == pos[i].Y) { 
isCover = true; 
break; 
} 
} 
} 
} while (isCover); 
this.pos = new Position(x, y); 
document.getElementById("box_" + x + "_" + y).className = "food"; 
}; 
} 
function Position(x, y) { 
this.X = 0; 
this.Y = 0; 
if (arguments.length >= 1) this.X = x; 
if (arguments.length >= 2) this.Y = y; 
} 
</script> 
</head> 
<body> 
<div id="pannel" style="margin-bottom: 10px;"></div> 
<select id="selSize"> 
<option value="20">20*20</option> 
<option value="30">30*30</option> 
<option value="40">40*40</option> 
</select> 
<select id="selSpeed"> 
<option value="500">速度-慢</option> 
<option value="250" selected="selected">速度-中</option> 
<option value="100">速度-快</option> 
</select> 
<input type="button" id="btnStart" value="开始" /> 
</body> 
</html>
Javascript 相关文章推荐
javascript 面向对象编程基础:继承
Aug 21 Javascript
使用ExtJS技术实现的拖动树结点
Aug 05 Javascript
jQuery实现的淡入淡出二级菜单效果代码
Sep 15 Javascript
JavaScript遍历Json串浏览器输出的结果不统一问题
Nov 03 Javascript
Angular将填入表单的数据渲染到表格的方法
Sep 22 Javascript
JavaScript ES6中的简写语法总结与使用技巧
Dec 30 Javascript
JS实现选项卡效果的代码实例
May 20 Javascript
前端Vue项目详解--初始化及导航栏
Jun 24 Javascript
解决layui数据表格Date日期格式的回显Object的问题
Sep 19 Javascript
使用PreloadJS加载图片资源的基础方法详解
Feb 03 Javascript
浅谈vue的第一个commit分析
Jun 08 Javascript
vue2的 router在使用过程中遇到的一些问题
Apr 13 Vue.js
jquery click([data],fn)使用方法实例介绍
Jul 08 #Javascript
js 得到文件后缀(通过正则实现)
Jul 08 #Javascript
php 中序列化和json使用介绍
Jul 08 #Javascript
浅析JS刷新框架中的其他页面 &amp;&amp; JS刷新窗口方法汇总
Jul 08 #Javascript
解析javascript 浏览器关闭事件
Jul 08 #Javascript
JS Jquery 遍历,筛选页面元素 自动完成(实现代码)
Jul 08 #Javascript
如何使用JS获取IE上传文件路径(IE7,8)
Jul 08 #Javascript
You might like
Windows下利用Gvim写PHP产生中文乱码问题解决方法
2011/04/20 PHP
thinkphp3.2.2前后台公用类架构问题分析
2014/11/25 PHP
php一个文件搞定微信jssdk配置
2016/12/12 PHP
PHP封装的非对称加密RSA算法示例
2018/05/28 PHP
php获取微信基础接口凭证Access_token
2018/08/23 PHP
php文件上传原理与实现方法详解
2019/12/20 PHP
node.js中的fs.existsSync方法使用说明
2014/12/17 Javascript
D3.js实现散点图和气泡图的方法详解
2016/09/21 Javascript
Vue.js第二天学习笔记(vue-router)
2016/12/01 Javascript
jQuery UI制作选项卡(tabs)
2016/12/13 Javascript
layui中layer前端组件实现图片显示功能的方法分析
2017/10/13 Javascript
vue的常用组件操作方法应用分析
2018/04/13 Javascript
详解vue路由篇(动态路由、路由嵌套)
2019/01/27 Javascript
vue插件mescroll.js实现移动端上拉加载和下拉刷新
2019/03/07 Javascript
jQuery表单选择器用法详解
2019/08/22 jQuery
webpack常用构建优化策略小结
2019/11/21 Javascript
[47:55]Ti4第二日主赛事败者组 NaVi vs EG 1
2014/07/20 DOTA
[53:10]Secret vs Pain 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
python实现登陆知乎获得个人收藏并保存为word文件
2015/03/16 Python
Python教程之全局变量用法
2016/06/27 Python
Python虚拟环境virtualenv的安装与使用详解
2017/05/28 Python
浅析Python pandas模块输出每行中间省略号问题
2018/07/03 Python
python 实现倒排索引的方法
2018/12/25 Python
python中append实例用法总结
2019/07/30 Python
Python实现快速排序的方法详解
2019/10/25 Python
python 中值滤波,椒盐去噪,图片增强实例
2019/12/18 Python
英国领先的隐形眼镜在线供应商:Lenstore.co.uk
2019/11/24 全球购物
全球最受追捧的运动服品牌领先数字目的地:Stylerunner
2020/11/25 全球购物
仓库门卫岗位职责
2013/12/22 职场文书
会计电算化应届生自荐信
2014/02/25 职场文书
公安局负责人查摆问题及整改方案
2014/09/27 职场文书
小区的门卫岗位职责
2014/10/01 职场文书
儿园租房协议书范本
2014/12/02 职场文书
初中差生评语
2014/12/29 职场文书
运动会3000米加油稿
2015/07/21 职场文书
python爬不同图片分别保存在不同文件夹中的实现
2021/04/02 Python