13行js写贪吃蛇游戏

匿名 (未验证) 提交于 2019-12-02 21:53:52

700char

13

4

0

  1. <!DOCTYPE html>
  2. <canvas id="1" width="400" height="400"></canvas>
  3. <script>
  4. let dir=1,food=3,snk=[1,0],ctx=document.getElementById("1").getContext("2d")
  5. document.onkeydown=e=>{dir =  snk[0]-snk[1]==-(tmp = [-1,-10,1,10][e.keyCode-37]||dir)?dir:tmp }
  6. setInterval(()=>{
  7.     snk.unshift(Head = snk[0] + dir)
  8.     if(Head!=food) snk.pop()
  9.     else while(snk.includes(food=0|Math.random()*10*10)) ;
  10.     if(snk.indexOf(Head,1)!=-1||(dir==1&&Head%10==0)||(dir==-1&&Head%10==9)||Head<0||Head>=100)
  11.         return document.write(0&snk.shift()) //
  12.     for(let i=0; i<100; i++){
  13.         ctx.fillStyle = '#0'+(food==i)*9910+snk.includes(i)*1990
  14.         ctx.fillRect(i%10*40,(i-i%10)*4, 40,40)
  15.     }
  16. },100)
  17. </script>

  1. <!DOCTYPE html>
  2. <canvas id="1" width="400" height="400"></canvas>
  3. <script>
  4. let dir=1,food=3,snk=[1,0],n_=0, ctx=document.getElementById("1").getContext("2d")
  5. document.onkeydown=e=>{ dir =snk[0]-snk[1]==-(tmp = [-1,-10,1,10][e.keyCode-37]||dir)?dir:tmp }
  6. setInterval(()=>{
  7.     snk.unshift(Head = snk[0] + dir)
  8.     if(Head!=food) snk.pop()
  9.     else while(snk.includes(food=0|Math.random()*10*10)) ;
  10.     if(snk.indexOf(Head,1)!=-1||(dir==1&&Head%10==0)||(dir==-1&&Head%10==9)||Head<0||Head>=100)
  11.         return alert("died"+ ++n_+"times") //
  12.     for(let i=0 ; i<100; i++){
  13.         ctx.fillStyle = '#0'+~~((food===i)*13000*Math.random())+~~(snk.includes(i)*3000*Math.random())
  14.         ctx.fillRect(i%10*40,(i-i%10)*4, 40,40)
  15.     }
  16. },120)
  17. </script>

:

  1. ,,
  2. 4

  1. https://google.github.io/styleguide/htmlcssguide.html#Optional_Tags

  2. https://google.github.io/styleguide/cppguide.html

  3. 33

       

900

1100char

  1. <!DOCTYPE html>
  2. <canvas id="1" width="400" height="400" style="border: 1px solid "></canvas>
  3. <script>
  4.     ctx = document.getElementById("1").getContext("2d") //CanvasRenderingContext2D inferface
  5.     let Len = 10, dir = 2, dirNow ; //dirNow
  6.     food = [3, 0]; Snake = [[0, 0], [1, 0]] //,Snake
  7.     Map = {'0,0':'#52a', '1,0':'#52a'} //
  8.     dirMat = [[-1, 0], [0, -1], [1, 0], [0, 1]] //
  9.     pairEq = ((p1, p2) => p1[0] == p2[0] && p1[1] == p2[1]) //
  10.     document.onkeydown = e =>{
  11.         if (37 <= e.keyCode == e.keyCode < 41 && dirNow != ( (e.keyCode - 35) % 4) ) //
  12.             dir = e.keyCode -37
  13.     }
  14.     !function () {
  15.         Head = Snake[Snake.length-1].map((x, i) => x + dirMat[dirNow=dir][i]); //
  16.         if (!pairEq(Head, food))
  17.             Map[Snake.shift()]='#fff' //,,
  18.         if (Snake.some(x=>pairEq(x,Head)) || !Head.every(x => 0<=x == x < Len)) //
  19.             return document.write("Game Over") //document.write
  20.         Snake.push(Head); //
  21.         while (Snake.some(x => pairEq(x, food))) //,
  22.             food = [~~(Math.random() * Len), ~~(Math.random() * Len)]; //js, ~~
  23.         Map[Head] = '#52a' ; Map[food] = '#ad5'
  24.         for( k in Map){
  25.             ctx.fillStyle= Map[k]
  26.             ctx.fillRect(parseInt(k[0])*40,parseInt(k[2])*40,40,40) //e.g. k="1,3",10
  27.         }
  28.         setTimeout(arguments.callee, 100); //100ms
  29.     }()
  30. </script>

()1100char

  1. <!DOCTYPE html>
  2. <canvas id="1" width="400" height="400" style="border: 1px solid " ></canvas>
  3. <script>
  4.     ctx = document.getElementById("1").getContext("2d")
  5.     let Len = 10, dir = 2, dirNow ;
  6. food = [3, 0]; Snake = [[0, 0], [1, 0]]
  7.     Map = {'0,0':'fff', '1,0':'fff'}
  8.     dirMat = [[-1, 0], [0, -1], [1, 0], [0, 1]]
  9.     pairEq = ((p1, p2) => p1[0] == p2[0] && p1[1] == p2[1])
  10.     document.onkeydown = e =>{
  11.         if (37 <= e.keyCode == e.keyCode < 41 && dirNow != ( (e.keyCode - 35) % 4) )
  12. dir = e.keyCode -37
  13.     }
  14.     !function () {
  15.         Head = Snake[Snake.length-1].map((x, i) => x + dirMat[dirNow=dir][i]);
  16.         if (!pairEq(Head, food)) Map[Snake.shift()]='0'
  17.         if (Snake.some(x=>pairEq(x,Head)) || !Head.every(x => 0<=x == x < Len))
  18.             return document.write("Game Over")
  19.         Snake.push(Head);
  20.         while (Snake.some(x => pairEq(x, food)))
  21. food = [~~(Math.random() * Len), ~~(Math.random() * Len)];
  22.         Map[Head] = Map[food] = 'fff'
  23.         for( k in Map){
  24.             ctx.fillStyle='#'+(0xfff-~~(parseInt(Map[k],16)*Math.random())).toString(16)
  25.             ctx.fillRect(parseInt(k[0])*40,parseInt(k[2])*40,46,43)
  26.         }
  27.         setTimeout(arguments.callee, 100);
  28.     }()
  29. </script>

   

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!