Advertisement
christofer_minestar

JavaSnakeGamecode

Jun 15th, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
4CS 9.27 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <title>JavaSnakeGame v 1.1 beta</title>
  6. <style>
  7. body
  8. {
  9.     background:#000;
  10.     color:#00CCCC;
  11. }
  12. canvas
  13. {
  14.     background:#000;
  15. }
  16. #controls
  17. {
  18.     position:absolute;
  19.     top:0;
  20.     right:0;
  21.     margin:10px;
  22. }
  23. </style>
  24. <script type="text/javascript">
  25. var snake = window.snake || {};
  26. function launchFullscreen(element) {
  27.   if(element.requestFullscreen) {
  28.     element.requestFullscreen();
  29.   } else if(element.mozRequestFullScreen) {
  30.     element.mozRequestFullScreen();
  31.   } else if(element.webkitRequestFullscreen) {
  32.     element.webkitRequestFullscreen();
  33.   } else if(element.msRequestFullscreen) {
  34.     element.msRequestFullscreen();
  35.   }
  36. }
  37. window.onload = function(){
  38.     document.addEventListener("fullscreenchange", function(){snake.game.adjust();});
  39.     document.addEventListener("webkitfullscreenchange", function(){snake.game.adjust();});
  40.     document.addEventListener("mozfullscreenchange", function(){snake.game.adjust();});
  41.     document.addEventListener("MSFullscreenChange", function(){snake.game.adjust();});
  42.  
  43.     snake.game = (function()
  44.     {
  45.         var canvas = document.getElementById('canvas');
  46.         var ctx = canvas.getContext('2d');
  47.         var status=false;
  48.         var score = 0;
  49.         var old_direction = 'right';
  50.         var direction = 'right';
  51.         var block = 10;
  52.         var score = 0;
  53.         var refresh_rate = 250;
  54.         var pos = [[5,1],[4,1],[3,1],[2,1],[1,1]];
  55.         var scoreboard = document.getElementById('scoreboard');
  56.         var control = document.getElementById('controls');
  57.         var keys = {
  58.             37 : 'left',
  59.             38 : 'up',
  60.             39 : 'right',
  61.             40 : 'down'
  62.             };
  63.         function adjust()
  64.         {
  65.             if (document.fullscreenElement || document.webkitFullscreenElement || document.mozFullScreenElement || document.msFullscreenElement )
  66.             {
  67.                 canvas.width=window.innerWidth;
  68.                 canvas.height=window.innerHeight;
  69.                 control.style.display='none';
  70.             }
  71.             else
  72.             {
  73.                 canvas.width=850;
  74.                 canvas.height=600;
  75.                 control.style.display='inline';
  76.             }
  77.         }
  78.         var food = [Math.round(Math.random(4)*(canvas.width - 10)), Math.round(Math.random(4)*(canvas.height - 10)),];
  79.         function todraw()
  80.         {
  81.             for(var i = 0; i < pos.length; i++)
  82.             {
  83.                 draw(pos[i]);
  84.             }
  85.         }
  86.         function giveLife()
  87.         {
  88.             var nextPosition = pos[0].slice();
  89.             switch(old_direction)
  90.             {
  91.                 case 'right':
  92.                     nextPosition[0] += 1;
  93.                     break;
  94.                 case 'left':
  95.                     nextPosition[0] -= 1;
  96.                     break;
  97.                 case 'up':
  98.                     nextPosition[1] -= 1;
  99.                     break;
  100.                 case 'down':
  101.                     nextPosition[1] += 1;
  102.                     break;    
  103.             }
  104.             pos.unshift(nextPosition);
  105.             pos.pop();
  106.         }
  107.         function grow()
  108.         {
  109.             var nextPosition = pos[0].slice();
  110.             switch(old_direction)
  111.             {
  112.                 case 'right':
  113.                     nextPosition[0] += 1;
  114.                     break;
  115.                 case 'left':
  116.                     nextPosition[0] -= 1;
  117.                     break;
  118.                 case 'up':
  119.                     nextPosition[1] -= 1;
  120.                     break;
  121.                 case 'down':
  122.                     nextPosition[1] += 1;
  123.                     break;    
  124.             }
  125.             pos.unshift(nextPosition);
  126.         }
  127.         function loop()
  128.         {
  129.             ctx.clearRect(0,0,canvas.width,canvas.height);
  130.             todraw();
  131.             giveLife();
  132.             feed();
  133.             if(is_catched(pos[0][0]*block,pos[0][1]*block,block,block,food[0],food[1],10,10))
  134.             {
  135.                 score += 10;
  136.                 createfood();
  137.                 scoreboard.innerHTML = score;
  138.                 grow();
  139.                 if(refresh_rate > 100)
  140.                 {
  141.                     refresh_rate -=5;
  142.                 }
  143.             }
  144.             snake.game.status = setTimeout(function() { loop(); },refresh_rate);
  145.         }
  146.         window.onkeydown = function(event){
  147.              direction = keys[event.keyCode];
  148.                 if(direction)
  149.                 {
  150.                     setWay(direction);
  151.                     event.preventDefault();
  152.                 }
  153.             };
  154.         function setWay(direction)
  155.         {
  156.             switch(direction)
  157.             {
  158.                 case 'left':
  159.                     if(old_direction!='right')
  160.                     {
  161.                         old_direction = direction;
  162.                     }
  163.                     break;
  164.                 case 'right':
  165.                     if(old_direction!='left')
  166.                     {
  167.                         old_direction = direction;
  168.                     }
  169.                     break;
  170.                 case 'up':
  171.                     if(old_direction!='down')
  172.                     {
  173.                         old_direction = direction;
  174.                     }
  175.                     break;
  176.                 case 'down':
  177.                     if(old_direction!='up')
  178.                     {
  179.                         old_direction = direction;
  180.                     }
  181.                     break;
  182.             }
  183.  
  184.         }
  185.         function feed()
  186.         {
  187.             ctx.beginPath();
  188.             ctx.fillStyle = "#ff0000";
  189.             ctx.fillRect(food[0],food[1],10,10);
  190.             ctx.fill();
  191.             ctx.closePath();
  192.         }
  193.         function createfood()
  194.         {
  195.             food = [Math.round(Math.random(4)*850), Math.round(Math.random(4)*600)];
  196.         }
  197.         function is_catched(ax,ay,awidth,aheight,bx,by,bwidth,bheight) {
  198.             return !(
  199.             ((ay + aheight) < (by)) ||
  200.             (ay > (by + bheight)) ||
  201.             ((ax + awidth) < bx) ||
  202.             (ax > (bx + bwidth))
  203.             );
  204.         }
  205.         function draw(pos)
  206.         {
  207.             var x = pos[0] * block;
  208.             var y = pos[1] * block;
  209.             if(x >= canvas.width || x <= 0 || y >= canvas.height || y<= 0)
  210.             {
  211.                     document.getElementById('pause').disabled='true';
  212.                     snake.game.status=false;
  213.                     ctx.clearRect(0,0,canvas.width,canvas.height);
  214.                     ctx.font='40px san-serif';
  215.                     ctx.fillText('Game Over',300,250);
  216.                     ctx.font = '20px san-serif';
  217.                     ctx.fillStyle='#000000';
  218.                     ctx.fillText('To Play again Refresh the page or click the Restarts button',200,300);
  219.                     throw ('Game Over');
  220.             }
  221.             else
  222.             {
  223.                 ctx.beginPath();
  224.                 ctx.fillStyle='#000000';
  225.                 ctx.fillRect(x,y,block,block);
  226.                 ctx.closePath();
  227.             }
  228.         }
  229.         function pause(elem)
  230.         {
  231.             if(snake.game.status)
  232.             {
  233.                 clearTimeout(snake.game.status);
  234.                 snake.game.status=false;
  235.                 elem.value='Play'
  236.             }
  237.             else
  238.             {
  239.                 loop();
  240.                 elem.value='Pause';
  241.             }
  242.         }
  243.         function begin()
  244.         {
  245.             loop();
  246.         }
  247.         function restart()
  248.         {
  249.             location.reload();
  250.         }
  251.         function start()
  252.         {
  253.             ctx.fillStyle='#000000';
  254.             ctx.fillRect(0,0,canvas.width,canvas.height);
  255.             ctx.fillStyle='#ffffff';
  256.             ctx.font='40px helvatica';
  257.             ctx.fillText('Vignesh',370,140);
  258.             ctx.font='20px san-serif';
  259.             ctx.fillText('presents',395,190);
  260.             ctx.font='italic 60px san-serif';
  261.             ctx.fillText('Feed The Snake',240,280);
  262.             var img = new Image();
  263.             img.onload = function()
  264.             {
  265.                 ctx.drawImage(img,300,300,200,200);
  266.                 ctx.fillRect(410,330,10,10);
  267.             }
  268.             img.src ='snake.png';
  269.         }
  270.         function fullscreen()
  271.         {
  272.             launchFullscreen(canvas);
  273.         }
  274.         return {
  275.             pause: pause,
  276.             restart : restart,
  277.             start : start,
  278.             begin: begin,
  279.             fullscreen : fullscreen,
  280.             adjust : adjust,
  281.         };
  282.     })();
  283.     snake.game.start();
  284. }
  285. </script>
  286. </head>
  287. <body>
  288. <canvas width="850" height="600" id="canvas" style="border:1px solid #333;" onclick="snake.game.begin();">
  289. </canvas>
  290. <div id="controls" style="float:right; text-align:center;">
  291.     <input type="button" id="pause" value="Play" onClick="snake.game.pause(this);" accesskey="p">
  292.     <input type="button" id="restart" value="Restart" onClick="snake.game.restart();">
  293.     <br/><br/>
  294.     <input type="button" id="fullscreen" value="Play Fullscreen" onClick="snake.game.fullscreen();">
  295.     <br/><br/>
  296.     <div style="font-size:24px;">
  297.     Score :
  298.     <span id="scoreboard">0</span>
  299.     </div>
  300. </div>
  301. </body>
  302. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement