JavaScript实现简单贪吃蛇效果
作者:huangfuyk. 发布时间:2023-08-13 05:48:08
标签:js,贪吃蛇
本文实例为大家分享了js实现简单贪吃蛇效果的具体代码,供大家参考,具体内容如下
上代码之前,先给大家看一下效果:
是不是想说:我能这样玩一天…
话不多说,代码如下:
<script>
class Map{
constructor(){
// 提前设定将来的地图的样式数据
this.w = 450;
this.h = 250;
this.c = "#DDD";
// 执行创建地图方法
this.createEle();
}
createEle(){
this.mapEle = document.createElement("div");
this.mapEle.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.c};margin:100px auto;position:relative;border:solid 10px #AAA;`;
document.body.appendChild(this.mapEle);
}
}
class Food{
constructor(){
// 提前设定将来的食物的样式数据
this.w = 10;
this.h = 10;
this.c = "red";
this.x = 0;
this.y = 0;
// 执行创建食物方法
this.createEle();
}
createEle(){
this.foodEle = document.createElement("div");
this.foodEle.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.c};position:absolute;left:${this.x * this.w}px;top:${this.y * this.h}px;border-radius:10px`;
m.mapEle.appendChild(this.foodEle);
}
randomPos(){
// 随机位置,随机产生的是格子的位置,不是真正的像素
this.x = random(0,(m.w-this.w) / this.w);
this.y = random(0,(m.h-this.h) / this.h);
// 设置位置时,要换算成像素,然后再生效
this.foodEle.style.left = this.x * this.w + "px";
this.foodEle.style.top = this.y * this.h + "px";
}
}
class Snake{
constructor(){
// 提前设定将来的蛇节的样式数据
this.w = 10;
this.h = 10;
// 因为蛇由多个设计组成,每个蛇节都有自己的独立信息,所以数据结构设计成如下格式
this.body = [{
ele:null,
x:4,
y:3,
c:randomColor()
},{
ele:null,
x:3,
y:3,
c:randomColor()
},{
ele:null,
x:2,
y:3,
c:randomColor()
}];
// 提前设置默认方向
this.d = "right";
// 开始创建蛇节元素,设置样式
this.createEle();
}
createEle(){
// 使用循环多次创建,因为有多个蛇节
for(var i=0;i<this.body.length;i++){
// 创建之前,需要判断元素是否已经存在,如果已经存在,不需要创建
if(!this.body[i].ele){
this.body[i].ele = document.createElement("div");
m.mapEle.appendChild(this.body[i].ele);
}
this.body[i].ele.style.cssText = `width:${this.w}px;height:${this.h}px;background:${this.body[i].c};position:absolute;left:${this.body[i].x * this.w}px;top:${this.body[i].y * this.h}px;border-radius: 10px`;
}
// 延迟之后,开始移动
setTimeout(()=>{
this.move();
},200);
}
move(){
// 从最后一个元素向前找前一个元素的坐标,直到第一个
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.d){
case "left":
this.body[0].x -= 1;
break;
case "right":
this.body[0].x += 1;
break;
case "top":
this.body[0].y -= 1;
break;
case "bottom":
this.body[0].y += 1;
break;
}
// 移动过程中,判断是否撞到边界,任意一个边界都不行
if(this.body[0].x < 0 || this.body[0].y < 0 || this.body[0].x > ((m.w-this.w) / this.w) || this.body[0].y > ((m.h-this.h) / this.h)){
alert("撞墙了");
return;
}
// 移动过程中,判断是否与食物的坐标重复,如果重复
if(this.body[0].x === f.x && this.body[0].y === f.y){
// 给蛇增加一个蛇节
this.body.push({
ele:null,
x:this.body[this.body.length-1].x,
y:this.body[this.body.length-1].y,
c:randomColor()
})
// 刷新食物的坐标
f.randomPos();
}
// 移动过程中,判断蛇头的坐标是否与某个任意一个蛇节的坐标重复
for(var i=1;i<this.body.length;i++){
if(this.body[0].x == this.body[i].x && this.body[0].y == this.body[i].y){
// 如果重复,撞到自己,结束程序
alert("撞到自己了");
return;
}
}
this.createEle();
}
direct(type){
switch(type){
case 37:
if(this.d === "right") break;
this.d = "left";
break;
case 38:
if(this.d === "bottom") break;
this.d = "top";
break;
case 39:
if(this.d === "left") break;
this.d = "right";
break;
case 40:
if(this.d === "top") break;
this.d = "bottom";
break;
}
}
}
function random(a,b){
return Math.round(Math.random()*(a-b)+b)
}
function randomColor(){
return `rgb(${random(0,255)},${random(0,255)},${random(0,255)})`
}
var m = new Map();
var f = new Food();
f.randomPos();
var s = new Snake();
// 当按下键盘时,将按下的键盘的code值,传给蛇的专属处理方法
document.onkeydown = function(eve){
var e = eve || window.event;
var code = e.keyCode || e.which;
s.direct(code);
}
</script>
来源:https://blog.csdn.net/weixin_42881768/article/details/104730982
0
投稿
猜你喜欢
- 什么是异步?含义 :双方不需要共同的时钟,也就是接收方不知道发送方什么时候发送,所以在发送的信息中就要有提示接收方开始接收的信息,如开始位,
- 本文实例讲述了原生JS实现Ajax通过POST方式与PHP进行交互的方法。分享给大家供大家参考,具体如下:一、代码conn.php<?
- 列表对象pop()方法的使用pop() 方法用于移除列表中的一个元素(默认最后一个元素),并且返回该元素的值。语法:verse.pop(in
- 过滤非法的SQL字符的函数代码:'*************************************************
- 有的时候,为了对python文件进行加密,会把python模块编译成.pyd文件,供其他人调用。拿到一个.pyd文件,在没有文档说明的情况下
- 逛网站的时候经常会遇到需要上传图片的操作,这里主要来说下selenium操作上传文件的操作。前提条件:定位的元素必须是type 属性是fil
- 1. 原地交换两个数字Python 提供了一个直观的在一行代码中赋值与交换(变量值)的方法,请参见下面的示例:x,y= 10,20print
- 我们在 Bitcoin SV 上实现了一个数独游戏智能合约,利用之前介绍过的一种合约范式可以将游戏中寻找解题方案的过程外包上链。因为求解数独
- 简介使用faker可以获取很多模拟数据,如:姓名、电话、地址、银行、汽车、条形码、公司、信用卡、email、user_agen等等学会使用这
- 特征降维0维 标量1维 向量2维 矩阵概念降维是指在某些限定条件下,降低随机变量(特征)个数,得到一组“不相关&
- 具体错误:UnicodeEncodeError: 'latin-1' codec can't encode char
- JavaScript是前端开发的主要语言,我们可以通过编写JavaScript程序来判断浏览器的类型及版本。JavaScript判断浏览器类
- Python decimal 模块Python中的浮点数默认精度是15位。Decimal对象可以表示任意精度的浮点数。getcontext函
- 阅读上一篇:FrontPage2002简明教程三:网页布局 网页的强大之处就在它的超链接,在浏览器中通过点击网页中的超链接,可以很方便地打开
- 经常需要读取某个文件夹下所有的图像文件。我使用python写了个简单的代码,读取某个文件夹下某个后缀的文件,将文件名生成为文本(csv格式)
- 1、PHP 中如何正确统计中文字数?这个是困扰我很久的问题,PHP 中有很多函数可以计算字符串的长度,比如下面的例子,分别使用了
- 本文主要是讲解Spark在Windows环境是如何搭建的一、JDK的安装1、1 下载JDK首先需要安装JDK,并且将环境变量配置好,如果已经
- 今天看到同事参与小米的抢购,几经数个星期的尝试,终于抢到了一台小米电视……看了一下小米的抢购流程,似乎可以用程序可破。于是想写点东西玩玩(你
- 1.imutils功能简介imutils是在OPenCV基础上的一个封装,达到更为简结的调用OPenCV接口的目的,它可以轻松的实现图像的平
- index()方法确定字符串str,如果起始索引beg和结束索引end在末尾给出了找到字符串或字符串的一个子串。这个方法与fin