这篇文章主要介绍了关于使用js实现贪吃蛇游戏的代码,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下
13行常规(700bytes)
shortest snake game.html
压缩后的500bytes(当然两处document还是可以用eval压缩的)
index.500bytes.html
之前很火的20行代码地址(有bug)(900bytes)
hj7jay/article/details/51011269
一维数组700char
(0,0)位置的蛇身用0表示,(0,1)用1,(1,0)用10表示,以此类推
因为就13行js,
第4行 是声明
第5行 比较难理解,可以把?:运算符,拆分为4行if语句;可以参考下面的 二维数组 的版本
第9行 0|x 和 ~~x 和 x>>0 都能去除x(number)的尾数
应该算易读了
<!doctype html><canvas id="1" width="400" height="400"></canvas> <script> let dir=1,food=3,snk=[1,0],ctx=document.getelementbyid("1").getcontext("2d")document.onkeydown=e=>{dir = snk[0]-snk[1]==-(tmp = [-1,-10,1,10][e.keycode-37]||dir)?dir:tmp }setinterval(()=>{ snk.unshift(head = snk[0] + dir) if(head!=food) snk.pop() else while(snk.includes(food=0|math.random()*10*10)) ; if(snk.indexof(head,1)!=-1||(dir==1&&head%10==0)||(dir==-1&&head%10==9)||head<0||head>=100) return document.write(0&snk.shift()) //死亡记录蛇长 for(let i=0; i<100; i++){ ctx.fillstyle = '#0'+(food==i)*9910+snk.includes(i)*1990 ctx.fillrect(i%10*40,(i-i%10)*4, 40,40) }},100) </script>
颜色效果
<!doctype html><canvas id="1" width="400" height="400"></canvas><script>let dir=1,food=3,snk=[1,0],n_=0, ctx=document.getelementbyid("1").getcontext("2d")document.onkeydown=e=>{ dir =snk[0]-snk[1]==-(tmp = [-1,-10,1,10][e.keycode-37]||dir)?dir:tmp }setinterval(()=>{ snk.unshift(head = snk[0] + dir) if(head!=food) snk.pop() else while(snk.includes(food=0|math.random()*10*10)) ; if(snk.indexof(head,1)!=-1||(dir==1&&head%10==0)||(dir==-1&&head%10==9)||head<0||head>=100) return alert("died"+ ++n_+"times") //死亡记录死亡次数 for(let i=0 ; i<100; i++){ ctx.fillstyle = '#0'+~~((food===i)*13000*math.random())+~~(snk.includes(i)*3000*math.random()) ctx.fillrect(i%10*40,(i-i%10)*4, 40,40) }},120)</script>
说明
如果自己要写的话:要注意两点
蛇尾应该比蛇头先消失,蛇头应该比食物先生成,
蛇不能走当前相反的方向
可以用长度为4的蛇进行测试
代码风格
省去没必要的标签
https://google.github.io/styleguide/htmlcssguide.html#optional_tags
if() return只用一行
https://google.github.io/styleguide/cppguide.html
除了键盘响应那里用3目运算符能省3行之外,其他地方都没必要用
二维数组
以上的都是用一维数组实现的,下面的用二维数组写;要简化也能简化到17行以内900char以内(比20行的那个短就是了),不过没有必要
带注释的1100char
<!doctype html><canvas id="1" width="400" height="400" style="border: 1px solid "></canvas><script> ctx = document.getelementbyid("1").getcontext("2d") //canvasrenderingcontext2d inferface let len = 10, dir = 2, dirnow ; //dirnow 后面解释 food = [3, 0]; snake = [[0, 0], [1, 0]] //食物的坐标,蛇身的坐标用snake数组记录 map = {'0,0':'#52a', '1,0':'#52a'} //用来记录绘图颜色的 地图 dirmat = [[-1, 0], [0, -1], [1, 0], [0, 1]] //方向矩阵 paireq = ((p1, p2) => p1[0] == p2[0] && p1[1] == p2[1]) //检测 两数对 是否相等的函数 document.onkeydown = e =>{ if (37 <= e.keycode == e.keycode < 41 && dirnow != ( (e.keycode - 35) % 4) ) //确定是 方向键 并且 保证方向与当前运动方向相反 dir = e.keycode -37 } !function () { head = snake[snake.length-1].map((x, i) => x + dirmat[dirnow=dir][i]); //得到头部接下来的移动位置 if (!paireq(head, food)) map[snake.shift()]='#fff' //必须先删尾巴,才能加入头部,吃没吃到食物是唯一判断标准 if (snake.some(x=>paireq(x,head)) || !head.every(x => 0<=x == x < len)) //判断蛇头是否撞到蛇身或墙壁 return document.write("game over") //这样调用document.write会把页面全部清空 snake.push(head); //可以加入头部 while (snake.some(x => paireq(x, food))) //加入新头后, 生成食物更方便 food = [~~(math.random() * len), ~~(math.random() * len)]; //因为js没有整形的概念, ~~ 现在相当于向原点舍去 map[head] = '#52a' ; map[food] = '#ad5' for( k in map){ ctx.fillstyle= map[k] ctx.fillrect(parseint(k[0])*40,parseint(k[2])*40,40,40) //e.g. k="1,3",也因此地图大小限制为10 } settimeout(arguments.callee, 100); //100ms后调用此函数一次 }()</script>
七彩的(主要是比较好看)1100char
<!doctype html><canvas id="1" width="400" height="400" style="border: 1px solid " ></canvas><script> ctx = document.getelementbyid("1").getcontext("2d") let len = 10, dir = 2, dirnow ;food = [3, 0]; snake = [[0, 0], [1, 0]] map = {'0,0':'fff', '1,0':'fff'} dirmat = [[-1, 0], [0, -1], [1, 0], [0, 1]] paireq = ((p1, p2) => p1[0] == p2[0] && p1[1] == p2[1]) document.onkeydown = e =>{ if (37 <= e.keycode == e.keycode < 41 && dirnow != ( (e.keycode - 35) % 4) )dir = e.keycode -37 } !function () { head = snake[snake.length-1].map((x, i) => x + dirmat[dirnow=dir][i]); if (!paireq(head, food)) map[snake.shift()]='0' if (snake.some(x=>paireq(x,head)) || !head.every(x => 0<=x == x < len)) return document.write("game over") snake.push(head); while (snake.some(x => paireq(x, food)))food = [~~(math.random() * len), ~~(math.random() * len)]; map[head] = map[food] = 'fff' for( k in map){ ctx.fillstyle='#'+(0xfff-~~(parseint(map[k],16)*math.random())).tostring(16) ctx.fillrect(parseint(k[0])*40,parseint(k[2])*40,46,43) } settimeout(arguments.callee, 100); }()</script>
以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注!
相关推荐:
javascript如何实现文件的下载功能
利用javascript中发出http请求的方法
以上就是使用js实现贪吃蛇游戏的代码的详细内容。
