最近一直在看javascript,但是发现不了动力。就开始想找动力,于是在网上找到了一个用js写的贪吃蛇游戏。奈何还不会用git,就只能先这样保存着。哈哈哈,这也算第一篇博客了,以后会坚持用自己的代码写博客的,下面的代码是别人写的,具体在哪找的我也忘了。。。by the way,写一些小游戏真的是学习的动力啊,很有趣。

<!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>

转载于:https://www.cnblogs.com/ff1997/p/7416554.html

js练习--贪吃蛇(转)相关推荐

  1. eclipse javascript_原生js实现贪吃蛇游戏_javascript技巧

    更新时间:2020年10月26日 11:46:36   作者:leisure-ZL 这篇文章主要为大家详细介绍了原生js实现贪吃蛇小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴 ...

  2. 前端那些事之原生 js实现贪吃蛇篇

    2019独角兽企业重金招聘Python工程师标准>>> 原生js实现贪吃蛇 <!DOCTYPE html> <html lang="en"> ...

  3. 原生JS实现贪吃蛇游戏

    原生JS实现贪吃蛇游戏 贪吃蛇游戏(原生JavaScript) 贪吃蛇游戏思路分析 游戏思想: 面向对象的思想 三个js文件分别代表三个对象 三个对象相互独立,在HTML中控制全局 使用面向对象思想的 ...

  4. 好玩的小游戏系列 (一)基于html+js 原生贪吃蛇

    一朵花如果只被用来观赏那只呈现出它的外在意义只是它生命的一部分若是不能够将其内在更实质的美发挥出来充其量也不过就是一朵死的花而已. 目录 一.前言 二.代码介绍 三.效果显示 四.编码实现 index ...

  5. 用JS实现贪吃蛇小游戏

    疫情期间呆在家里无聊,于是深入学习了一下JS.自己也模仿写一个贪吃蛇的小游戏,本人刚接触JS不久如有错误,望评论区指出. 因为一般贪吃蛇游戏中的CSS样式较为简单,所以这里主要讲一下JS的写法. 食物 ...

  6. 原生JS实现贪吃蛇——项目总结

    项目准备 项目展示图 建立新文件夹,新建出images CSS JS 三个文件夹,并在根目录下创建出index.html 将下列素材图片转到images文件中 接下来可以开始着手操作了 HTML结构 ...

  7. 利用HTML5 canvas元素+原生JS编写贪吃蛇

    我们先来看一个最简单的例子 利用canvas动画编写贪吃蛇:https://clacier.github.io/tcs/ 一.什么是canvas? canvas是HTML5中新增加的一个元素,专门用于 ...

  8. 纯JS实现贪吃蛇——超上瘾小游戏!!!

    效果图 最近使用 JS 写了一个贪吃蛇游戏,效果如下: 谷歌浏览器点击直接在线玩 前言 贪吃蛇作为一款经典又简单的小游戏,每个人都玩过.实现一个贪吃蛇游戏基本具有以下功能: 棋盘(也被称作 " ...

  9. HTML+js实现贪吃蛇小游戏(内含完整代码)

    案例分析 看图拆解游戏 首先我们根据图片上的内容把这个游戏拆解成几个部分去单独看: 最外面的大盒子包裹着内容加边框限制蛇的活动范围,整个范围可以看成由许多小方格排列构成的,例如这样子的:: 两个按钮, ...

最新文章

  1. 破除Odoo 菜单栏提示 99+
  2. 比特币是什么,看这篇就懂了
  3. 关于C#的强制转换和尝试转换的方法
  4. 测试PF_RING DNA驱动
  5. Go基础编程:复合类型—切片slice
  6. 如何理解边沿触发器和脉冲触发器?
  7. git的版本回溯(git想要退回到之前写过的某一个版本)
  8. 手机图形计算器matlab,图形计算器Mathlab
  9. 豆瓣电影Top250信息爬取并保存到excel文件中!
  10. 【安全开发】IOS安全编码规范
  11. three.js创建简单的法向贴图
  12. 【解决方案】AI视频结构化智能安防平台EasyCVR保护小区居民安全智能监控方案
  13. 最实惠又容易上手的STM32的学习板,你确定不了解一下吗
  14. Matlab二值图像进行轮廓提取
  15. copy-to-clipboard 的拷贝使用
  16. 【随笔】学会拒绝别人,聪明地说不,学会独处
  17. 服务器获取真实客户端 IP
  18. Java实现头像上传
  19. The skin directory does not point to a valid skin虚拟机配置
  20. MATLAB递归将数字一个个输出,数米粒个数和每个米粒面积的matlab算法实现(递归)。 | 学步园...

热门文章

  1. java swing 案例详解_《Java Swing图形界面开发与案例详解》PDF_IT教程网
  2. cass展点不在原位置,cass中打开一副图后,通过绘图处理-——展高程点,怎么之前的图形就不显示了,,只剩下高程点!!...
  3. linux nfsnobody用户,处理CentOS 5.5 x64 配置NFS服务过程中nfsnobody用户造成的问题
  4. C语言中定义变量位置
  5. QPushButton hover配置
  6. LeetCode 112. 路径总和 、113. 路径总和 II 思考分析
  7. strictmath_Java StrictMath ceil()方法与示例
  8. Java ResourceBundle keySet()方法及示例
  9. Java布尔类toString()方法及示例
  10. Java IdentityHashMap isEmpty()方法与示例