贪吃蛇小游戏源码再回顾

<!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;}.gtitle {font-size: 25px;font-weight: bold;}#gnum {color: red;}
</style>
</head><body><div id="main"><!-- 按钮 --><h1>贪吃蛇游戏</h1><input class="btn" type="button" value="开始游戏" id="begin"><input class="btn" type="button" value="暂停游戏" id="pause"><span class="gtitle">第<span id="gnum">1</span>关</span></div><!-- 贪吃蛇游戏设计 --><script>var main = document.getElementById('main');/* 画布格子是否开启 */var showcanvas = false;/* 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});//判断一下几倍if (this.body.length > l.slength) {l.set();}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);}//创建级别function Level() {this.num = 1; //第几级别this.speek = 300; //速度加快this.slength = 8; //每个关的长度判断this.set = function() {this.num++;if (this.speek <= 50) {this.speek = 50;} else {this.speek -= 50;}//可以自己定义this.slength += 3;this.display();start();}this.display = function() {document.getElementById('gnum').innerHTML = this.num;}}var l = new Level();l.display();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; //变量可以提升/* 第一部分 */function start() {clearInterval(timer);timer = setInterval(function() {snake.run();}, l.speek)}/* 第一部分开始 */document.getElementById('begin').onclick = function() {start();}/*第一部分 暂停 */document.getElementById('pause').onclick = function() {clearInterval(timer);timer = setInterval(function() {}, 300)}
</script>
</body>
​
</html>

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/416620.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

idea2020.03打开项目注释变为显示模式,大于等于等符号也变为中文符号

新版的idea2020.03方法上的注释变为左侧一个长竖线&#xff0c;右侧直接是展示模式的内容&#xff0c; 大于等于等符号变为对应的全角符号≤≥&#xff1d;≠&#xff0c;感觉好奇怪。 怎么让他显示回去呢&#xff1f; 解决办法 File——Close Project 进入idea启动页面&#…

快速排序2

算法&#xff1a; 1、从数列中挑出一个元素&#xff0c;称为 "基准"&#xff08;pivot&#xff09;&#xff0c; 2、重新排序数列&#xff0c;所有元素比基准值小的摆放在基准前面&#xff0c;所有元素比基准值大的摆在基准的后面&#xff08;相同的数可以到任一边&a…

前端学习(2442):解决跨域问题

1.同源策略如下&#xff1a; URL说明是否允许通信http://www.a.com/a.js http://www.a.com/b.js同一域名下允许http://www.a.com/lab/a.js http://www.a.com/script/b.js同一域名下不同文件夹允许http://www.a.com:8000/a.js http://www.a.com/b.js同一域名&#xff0c;不同端…

idea2020.03 lombok异常

提示找不到lombok生成的那些getter和setter方法。 解决办法 插件lombok升级到最新版本&#xff0c;不行就卸载安装下&#xff0c;当前版本为0.32-EAP 项目依赖的lombok升级到1.18.16版本。 File——Invalidate Cache / Restart&#xff08;清除IDEA缓存&#xff0c;重启&#x…

【bzoj3224】普通平衡树——treap

我的第一道treap题目&#xff0c;treap的模版题。 代码是对着hzw的敲的&#xff0c;一边敲一边理解。。。 主要是熟悉一下treap的各种基本操作&#xff0c;详细细节看代码。 #include<cstdio> #include<iostream> #include<cstring> #include<cstdlib>…

md5加密 兼容.netcore5

md5加密 支持.netcore5 using System; using System.Security.Cryptography; namespace MyApp.HelloWorld {class Program{static void Main(string[] args){Console.WriteLine(MD5Encrypt("123456"));Console.WriteLine("Hello World!");}/// <su…

【转载】安卓开发者在使用deepin15.4时可能会遇到的问题

本文转载自&#xff1a;https://bbs.deepin.org/forum.php?modviewthread&tid138244&extra 1、在你安装好Android Studio前别忘了先安装default-jdk2、终端提示&#xff1a;Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettingsgasp 解决方法&#xff1a…

.net core linux安装

手动安装 https://docs.microsoft.com/zh-cn/dotnet/core/install/linux-scripted-manual#manual-install 进入.net core 5.0 https://dotnet.microsoft.com/download/dotnet/5.0 再点击linux x64版本下载 https://download.visualstudio.microsoft.com/download/pr/7f736160-…

【转】java提高篇(二)-----理解java的三大特性之继承

【转】java提高篇(二)-----理解java的三大特性之继承 原文地址&#xff1a;http://www.cnblogs.com/chenssy/p/3354884.html 在《Think in java》中有这样一句话&#xff1a;复用代码是Java众多引人注目的功能之一。但要想成为极具革命性的语言&#xff0c;仅仅能够复制代码并对…

google AviatorEvaluator 变量公式计算

添加依赖 <dependency><groupId>com.googlecode.aviator</groupId><artifactId>aviator</artifactId><version>5.2.0</version> </dependency>编写代码 import com.googlecode.aviator.AviatorEvaluator; import com.google…

免费生成https证书以及配置

http升级到https需要在nginx的配置中加入证书信息,查询资料后确定生成证书两种方案第一种:自签名证书,然后开启 CloudFlare 的 CDN 服务 //确定是否安装opensslwhich openssl//如果没有安装,通过apt-get或者yum等方式安装即可sudo apt-get install openssl//生成一个名为“ssl.…

redis 4.0.9 centos7 双机集群安装

开启limits限制 sudo bash -c cat >> /etc/security/limits.conf <<-EOF * soft nofile 65536 * hard nofile 65536 * soft nproc 131072 * hard nproc 131072EOFreboot重启 安装redis 两台机器都运行 yum install -y wget gcc wget http://download.redis.io/…

关于毕业的一些事情

这篇日志&#xff0c;算是日记吧。就当做日记。 今天上午8点開始在8-302举行答辩&#xff0c;关于毕业论文&#xff0c;就是用的原先的智能问答机器人。也还算顺利。无论咋样&#xff0c;过是没问题。 晚上在校园食代举行班级谢师宴&#xff0c;老师来了15位左右&#xff0c;跟…

递归树思路总结思考

递归思路 传入数组 传入单个对象 传入数组 内部先遍历进行单个对象处理&#xff0c; 再递归单个对象的children 传入对象 内部先处理当前对象&#xff0c;再遍历对象children&#xff0c;对每个children进行递归调用。 需要处理返回值的情况 一种是传入对象指针&#xff…

前端学习(2441):删除处理完成

request.js <template> <div class"artical-container"><!--卡片--><el-card class"filter-card"><div slot"header" class"clearfix"><!--面包屑导航--><el-breadcrumb separator-class&quo…

5、用枚举值表示状态、选项、状态码

一、C语言中1、C语言中的枚举类型enum。在以一系列常量来表示错误状态码或可组合的选项时&#xff0c;极宜使用枚举为其命名。2、定义一个enumenum personAgeState { personAgeStateLitter,personAgeStateBigger};property (nonatomic, assign) enum personAgeState personAge;…