<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>游戏初始化界面</title><style>body {margin: 0;padding: 0;}#main {margin: 100px;}.btn {width: 100px;height: 40px;}</style>
</head><body><div id="main"><!-- 按钮 --><input class="btn" type="button" value="开始游戏" id="begin"><input class="btn" type="button" value="暂停游戏" id="pause"></div><!-- 贪吃蛇游戏设计 --><script>var main = document.getElementById('main');/* 画布格子是否开启 */var showcanvas = true;/* atom 原子大小 xnum横向原子数量 ynum纵向原子数量 */function Map(atom, xnum, ynum) {this.atom = atom;this.xnum = xnum;this.ynum = ynum;//声明画布this.canvas = null;//第二部分 创建画布方法this.create = function() {this.canvas = document.createElement('div');this.canvas.style.cssText = 'position:relative;top:40px;border:1px solid red;background:#FAFAFA';this.canvas.style.width = this.atom * this.xnum + 'px'; //画布的宽this.canvas.style.height = this.atom * this.ynum + 'px'; //画布的宽main.appendChild(this.canvas);if (showcanvas) {for (var x = 0; x < xnum; x++) {for (var y = 0; y < ynum; y++) {var a = document.createElement('div');a.style.cssText = "border:1px solid yellow";a.style.width = this.atom + 'px';a.style.height = this.atom + 'px';a.style.backgroundColor = 'green';this.canvas.appendChild(a);a.style.position = 'absolute';a.style.left = x * this.atom + 'px';a.style.top = y * this.atom + 'px';}}}}}/*第四部分 创建蛇 */function Snake(map) {//设置宽度高度this.width = map.atom;this.height = map.atom;/* 蛇的方向 */this.direction = 'right';this.body = [{x: 2,y: 0}, //第一点{x: 1,y: 0}, //第二点{x: 0,y: 0} //第三点];//显示蛇this.display = function() {for (var i = 0; i < this.body.length; i++) {//当吃到食物时候 x==null 不能新建 不然会在00处新建一个if (this.body[i].x != null) {var s = document.createElement('div');//将蛇的节点保存到状态变量中 方便删除使用this.body[i].flag = s;/* console.log(1); *///设置蛇的样式s.style.width = this.width + 'px';s.style.height = this.height + 'px';s.style.backgroundColor = "rgb(" + Math.floor(Math.random() * 200) + "," +Math.floor(Math.random() * 200) + "," +Math.floor(Math.random() * 200) + ")"s.style.position = 'absolute';s.style.left = this.body[i].x * this.width + 'px';s.style.top = this.body[i].y * this.height + 'px';//添加到地图中map.canvas.appendChild(s);}}}/* 让蛇动起来 {x: 2,y: 0}, //第一点{x: 1,y: 0}, //第二点{x: 0,y: 0} //第四点 让蛇改变方向*/this.run = function() {for (var i = this.body.length - 1; i > 0; i--) {this.body[i].x = this.body[i - 1].x;this.body[i].y = this.body[i - 1].y;}//根据方向处理蛇头switch (this.direction) {case "left":this.body[0].x -= 1;break;case "right":this.body[0].x += 1;break;case "up":this.body[0].y -= 1;break;case "down":this.body[0].y += 1;break;}//判断蛇头吃到食物 蛇头坐标和食物坐标重合 第六部分if (this.body[0].x == food.x && this.body[0].y == food.y) {//蛇会加一个this.body.push({x: null,y: null,flag: null});map.canvas.removeChild(food.flag);food = new Food(map);}//判断是否出界if (this.body[0].x < 0 || this.body[0].x > map.xnum - 1 || this.body[0].y < 0 || this.body[0].y > map.ynum - 1) {clearInterval(timer);alert("真笨啊,活活的撞墙了");restart(map, this);return false;}//判断是否和自己重合for (var i = 4; i < this.body.length; i++) {if (this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y) {clearInterval(timer);alert("真笨啊,活活的吃到自己了");restart(map, this);return false;}}/*       console.log(111); *//* this.body[0].x += 1; */for (var i = 0; i < this.body.length; i++) {//不等于空 就删除 当吃到食物的时候 flag等于空if (this.body[i].flag != null) {map.canvas.removeChild(this.body[i].flag);}}this.display();//给body加键盘事件 第五步 给蛇改变方向window.onkeydown = function(e) {var event = e || window.event;switch (event.keyCode) {case 38:if (snake.direction != "down") {snake.direction = "up";}break;case 40:if (snake.direction != "up") {snake.direction = "down";}break;case 37:if (snake.direction != "right") {snake.direction = "left";}break;case 39:if (snake.direction != "left") {snake.direction = "right";}break;}}/* */}}//重新开始游戏 第六部分function restart(map, snake) {for (var i = 0; i < snake.body.length; i++) {map.canvas.removeChild(snake.body[i].flag);}snake.body = [{x: 2,y: 0}, //第一点{x: 1,y: 0}, //第二点{x: 0,y: 0} //第三点];snake.direction = 'right';snake.display();map.canvas.removeChild(food.flag);food = new Food(map);}var map = new Map(20, 40, 20);//创建画布map.create();//构造食物var food = new Food(map);//构建蛇var snake = new Snake(map);snake.display();/* 第三部分 创建食物 map地图对象 */function Food(map) {this.width = map.atom;this.height = map.atom;this.bgcolor = "rgb(" + Math.floor(Math.random() * 200) + "," + Math.floor(Math.random() * 200) + "," +Math.floor(Math.random() * 200) + ")"this.x = Math.floor(Math.random() * map.xnum);this.y = Math.floor(Math.random() * map.ynum);//画出食物this.flag = document.createElement('div');this.flag.style.width = this.width + 'px';this.flag.style.height = this.height + 'px';this.flag.style.backgroundColor = this.bgcolor;this.flag.style.borderRadius = '50%';this.flag.style.position = 'absolute';this.flag.style.left = this.x * this.width + 'px';this.flag.style.top = this.y * this.height + 'px';map.canvas.appendChild(this.flag);}var timer; //变量可以提升/* 第一部分 *//* 第一部分开始 */document.getElementById('begin').onclick = function() {console.log(222);clearInterval(timer);timer = setInterval(function() {snake.run();}, 300)}/*第一部分 暂停 */document.getElementById('pause').onclick = function() {clearInterval(timer);timer = setInterval(function() {}, 300)}</script>
</body></html>

运行结果

前端学习(1684):前端系列实战课程之判断游戏结束相关推荐

  1. 2021年最新版Web前端学习路线图-前端小白入门必读-推荐

    2021年最新版Web前端学习路线图-前端小白入门必读-推荐 Hello,大家好,相信很多学习前端的小伙伴,会有很多的疑惑: 我要学习那些技术? 我要到哪里去学习这些技术呢? 学习这些技术的目的对就业 ...

  2. web前端学习之前端重构方案,来了解一下

    前端技术发展很快,很多项目面临前端部分重构,很开心可以让我进行这次项目前端的重构方案编写,在思考的同时参考了网上很多资料,希望本篇重构方案有一定的完整性,可以带给大家一些在面临重构时有用的东西,同时希 ...

  3. 前端学习(1685):前端系列实战课程之设置难度

    <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" ...

  4. 前端学习(1683):前端系列实战课程之让蛇吃食物变长

    <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" ...

  5. 前端学习(1682):前端系列实战课程之让蛇改变方向

    <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" ...

  6. 前端学习(1681):前端系列实战课程之让蛇动起来

    <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" ...

  7. 前端学习(1680):前端系列实战课程之创建和显示蛇

    <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" ...

  8. 前端学习(1679):前端系列实战课程之为蛇创建食物对象

    <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" ...

  9. 前端学习(1678):前端系列实战课程之声明和创建游戏地图

    <!DOCTYPE html> <html lang="en"><head><meta charset="UTF-8" ...

最新文章

  1. 第二次数据库作业--gui
  2. 深度剖析Kubernetes API Server三部曲 - part 2
  3. JQuery中click() 和onclick()区别
  4. 彻底卸载oracle
  5. 需求条目化:一个让用户故事有效落地的套路
  6. 针对网上的一份netty的面试题之(netty的粘包和拆包)
  7. vue 执行函数this_vue回调函数中this无效
  8. 阿里重磅开源中后台UI解决方案Fusion
  9. idea引不进jdk的包_idea install 时提示jdk的某个jar包的包不存在的问题
  10. 原来我不懂printf
  11. AKKA:大数据下的并发编程模型
  12. php毕业论文总结,毕业设计总结
  13. 光滑曲线_第九章 曲线积分与曲面积分 第六节 高斯公式和斯托克斯公式
  14. [渝粤教育] 南通职业大学 艺术导论2021 参考 资料
  15. 项目管理知识体系指南 (五)
  16. java hotspot server_Java HotSpot(TM)64位服务器VM警告
  17. Python 设计模式 - 建造者模式
  18. 对接商汤摄像头详细步骤
  19. 计算机缓存Cache以及Cache Line详解
  20. 益寿延年,这13种食物真是宝,能延寿10年,赶紧收藏!

热门文章

  1. git忽略某个文件夹
  2. 实验五——循环结构学习总结
  3. UI-UIButton、UILable、UITextField总结
  4. BusyBox编译配置
  5. 重构 改善既有代码的设计:代码的坏
  6. java解析bmp文件
  7. Virtools脚本语言(VSL)教程 - 枚举
  8. 结构体+sort方法
  9. docker导入镜像 liunx_docker扫盲?面试连这都不会就等着挂吧
  10. 计算机二级考试试题在线看,【TOP182015年全国计算机二级考试试题题库.doc文档免费在线阅读材料】...