Advertisement
DarioCas

HTML Snake Game

Jun 22nd, 2019
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.         <canvas id="gc" width="600" height="600"></canvas>
  2. <script>
  3. window.onload=function() {
  4.     canv=document.getElementById("gc");
  5.     ctx=canv.getContext("2d");
  6.     document.addEventListener("keydown",keyPush);
  7.     setInterval(game,1000/10);
  8.     setInterval(superblocks,2000);
  9.     setInterval(obstaclemove,4550);
  10. }
  11. px=py=13;
  12. gs=20;
  13. tc=30;
  14. ax=Math.floor(Math.random()*tc);
  15. ay=Math.floor(Math.random()*tc);
  16. apx=Math.floor(Math.random()*tc);
  17. apy=Math.floor(Math.random()*tc);
  18. porx=Math.floor(Math.random()*tc);
  19. pory=Math.floor(Math.random()*tc);
  20. sx=Math.floor(Math.random()*tc);
  21. sy=Math.floor(Math.random()*tc);
  22. ox=Math.floor(Math.random()*tc);
  23. oy=Math.floor(Math.random()*tc);
  24. obx=Math.floor(Math.random()*tc);
  25. oby=Math.floor(Math.random()*tc);
  26. obsx=Math.floor(Math.random()*tc);
  27. obsy=Math.floor(Math.random()*tc);
  28. obstx=Math.floor(Math.random()*tc);
  29. obsty=Math.floor(Math.random()*tc);
  30. score=0;
  31. highscore=0;
  32. scoreten=false;
  33. xv=yv=0;
  34. trail=[];
  35. tail = 4;
  36. function game() {
  37.     px+=xv;
  38.     py+=yv;
  39.     if(px<0) {
  40.         tail = 4;
  41.         xv=0;yv=0;
  42.         px=13;py=13;
  43.         if(score>highscore) {
  44.             highscore=score;
  45.         }
  46.     }
  47.     if(px>tc-1) {
  48.         tail = 4;
  49.         xv=0;yv=0;
  50.         px=13;py=13;
  51.         if(score>highscore) {
  52.             highscore=score;
  53.         }
  54.     }
  55.     if(py<0) {
  56.         tail = 4;
  57.         xv=0;yv=0;
  58.         px=13;py=13;
  59.         if(score>highscore) {
  60.             highscore=score;
  61.         }
  62.     }
  63.     if(py>tc-1) {
  64.         tail = 4;
  65.         xv=0;yv=0;
  66.         px=13;py=13;
  67.         if(score>highscore) {
  68.             highscore=score;
  69.         }
  70.     }
  71.  
  72.     document.getElementById("score").innerHTML ="Score: " + score;
  73.     document.getElementById("score").style.font = "bold 25px arial";
  74.  
  75.     document.getElementById("highscore").innerHTML ="Highscore: " + highscore;
  76.     document.getElementById("highscore").style.font = "bold 20px arial";
  77.  
  78.     ctx.fillStyle="green";
  79.     ctx.fillRect(0,0,canv.width,canv.height);
  80.  
  81.     ctx.fillStyle="lime";
  82.     for(var i=0;i<trail.length;i++) {
  83.         ctx.fillRect(trail[i].x*gs,trail[i].y*gs,gs-2,gs-2);
  84.         if(trail[i].x==px && trail[i].y==py) {
  85.             tail = 4;
  86.             xv=0;yv=0;
  87.             px=13;py=13;
  88.             if(score>highscore) {
  89.                 highscore=score;
  90.             }
  91.         }
  92.     }
  93.     trail.push({x:px,y:py});
  94.     while(trail.length>tail) {
  95.     trail.shift();
  96.     }
  97.     if(ax==px && ay==py) {
  98.         tail++;
  99.         score++;
  100.         ax=Math.floor(Math.random()*tc);
  101.         ay=Math.floor(Math.random()*tc);
  102.     }
  103.     if(apx==px && apy==py) {
  104.         tail++;
  105.         score++;
  106.         apx=Math.floor(Math.random()*tc);
  107.         apy=Math.floor(Math.random()*tc);
  108.     }
  109.     ctx.fillStyle="red";
  110.     ctx.fillRect(ax*gs,ay*gs,gs-2,gs-2);
  111.  
  112.     ctx.fillStyle="red";
  113.     ctx.fillRect(apx*gs,apy*gs,gs-2,gs-2);
  114.  
  115.     if (px==porx && py==pory) {
  116.         px=Math.floor(Math.random()*tc);
  117.         py=Math.floor(Math.random()*tc);
  118.         porx=Math.floor(Math.random()*tc);
  119.         pory=Math.floor(Math.random()*tc);
  120.     }
  121.  
  122.     ctx.fillStyle="white";
  123.     ctx.fillRect(porx*gs,pory*gs,gs-2,gs-2);
  124.  
  125.     ctx.fillStyle="darkslategray";
  126.     ctx.fillRect(ox*gs,oy*gs,gs-2,gs-2);
  127.  
  128.     ctx.fillStyle="darkslategray";
  129.     ctx.fillRect(obx*gs,oby*gs,gs-2,gs-2);
  130.  
  131.     ctx.fillStyle="darkslategray";
  132.     ctx.fillRect(obstx*gs,obsty*gs,gs-2,gs-2);
  133.  
  134.     ctx.fillStyle="darkslategray";
  135.     ctx.fillRect(obsx*gs,obsy*gs,gs-2,gs-2);
  136.  
  137.     ctx.fillStyle="gold";
  138.     ctx.fillRect(sx*gs,sy*gs,gs-2,gs-2);
  139.  
  140.     if(px==sx && py==sy) {
  141.         tail++;
  142.         tail++;
  143.         score++;
  144.         score++;
  145.         sx=Math.floor(Math.random()*tc);
  146.         sy=Math.floor(Math.random()*tc);
  147.     }
  148.  
  149.     if(px==ox && py==oy) {
  150.         tail = 4;
  151.         xv=0;yv=0;
  152.         px=py=13;
  153.         if(score>highscore) {
  154.             highscore=score;
  155.         }
  156.     }
  157.     if(px==obsx && py==obsy) {
  158.         tail = 4;
  159.         xv=0;yv=0;
  160.         px=py=13;
  161.         if(score>highscore) {
  162.             highscore=score;
  163.         }
  164.     }
  165.     if(px==obx && py==oby) {
  166.         tail = 4;
  167.         xv=0;yv=0;
  168.         px=py=13;
  169.         if(score>highscore) {
  170.             highscore=score;
  171.         }
  172.     }
  173.     if(px==obstx && py==obsty) {
  174.         tail = 4;
  175.         xv=0;yv=0;
  176.         px=py=13;
  177.         if(score>highscore) {
  178.             highscore=score;
  179.         }
  180.     }
  181.  
  182.     document.getElementById("credits").innerHTML ="Programmed by: Dario Casciato";
  183.     document.getElementById("credits").style.font = "10px arial"
  184.  
  185. }
  186. function keyPush(evt) {
  187.     switch(evt.keyCode) {
  188.         case 37:
  189.             if(xv==0 && yv==0) {
  190.                 score=0;
  191.             }
  192.             xv=-1;yv=0;
  193.             break;
  194.         case 38:
  195.             if(xv==0 && yv==0) {
  196.                 score=0;
  197.             }
  198.             xv=0;yv=-1;
  199.             break;
  200.         case 39:
  201.             if(xv==0 && yv==0) {
  202.                 score=0;
  203.             }
  204.             xv=1;yv=0;
  205.             break;
  206.         case 40:
  207.             if(xv==0 && yv==0) {
  208.                 score=0;
  209.             }
  210.             xv=0;yv=1;
  211.             break;
  212.         case 65:
  213.             tail++;
  214.             score++;
  215.             break;
  216.     }
  217. }
  218. function superblocks() {
  219.  
  220.     sx=Math.floor(Math.random()*tc);
  221.     sy=Math.floor(Math.random()*tc);
  222.  
  223. }
  224. function obstaclemove() {
  225.     obstchoice=Math.floor(Math.random() * 4);
  226.     obstachoice=obstchoice=Math.floor(Math.random() * 4);
  227.     if(obstchoice==0) {
  228.         if(obstachoice==0) {
  229.             ox++;
  230.         }
  231.         if(obstachoice==1) {
  232.             ox--;
  233.         }
  234.         if(obstachoice==2) {
  235.             oy++;
  236.         }
  237.         if(obstachoice==3) {
  238.             oy--;
  239.         }
  240.     }
  241.     if(obstchoice==1) {
  242.         if(obstachoice==0) {
  243.             obx++;
  244.         }
  245.         if(obstachoice==1) {
  246.             obx--;
  247.         }
  248.         if(obstachoice==2) {
  249.             oby++;
  250.         }
  251.         if(obstachoice==3) {
  252.             oby--;
  253.         }
  254.     }
  255.     if(obstchoice==2) {
  256.         if(obstachoice==0) {
  257.             obsx++;
  258.         }
  259.         if(obstachoice==1) {
  260.             obsx--;
  261.         }
  262.         if(obstachoice==2) {
  263.             obsy++;
  264.         }
  265.         if(obstachoice==3) {
  266.             obsy--;
  267.         }
  268.     }
  269.     if(obstchoice==3) {
  270.         if(obstachoice==0) {
  271.             obstx++;
  272.         }
  273.         if(obstachoice==1) {
  274.             obstx--;
  275.         }
  276.         if(obstachoice==2) {
  277.             obsty++;
  278.         }
  279.         if(obstachoice==3) {
  280.             obsty--;
  281.         }
  282.     }
  283.  
  284. }
  285. </script>
  286. <p id="score"></p>
  287. <p id="highscore"></p>
  288. <p id="credits"></p>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement