Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2014
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.58 KB | None | 0 0
  1.  
  2. <!DOCTYPE HTML>
  3. <html>
  4. <body>
  5.  
  6. <canvas id="e" width="300" height="300" tabindex="0" style="border:1px solid #c3c3c3;">
  7. Your browser does not support the canvas element.
  8. </canvas>
  9.  
  10. <script type="text/javascript">
  11.   var canvas = document.getElementById("e");
  12.   canvas.addEventListener( "keydown", doKeyDown, true);
  13.   var cxt = canvas.getContext("2d");
  14.  
  15.   var mapArray =  [ [0,0,0],
  16.       [0,0,1],
  17.       [0,1,0]  ];
  18.      
  19.   var cellSize = 100;
  20.   var mainChar = [1,0]; // y , x
  21.  
  22.   var timeCounter = 1.0;
  23.  
  24.   function doKeyDown(e) {
  25.  
  26.  if (e.keyCode == 38) {
  27.   if(mainChar[1]>0 && mapArray[mainChar[0]][mainChar[1]-1] == 0){
  28.   mainChar[1]-=1;
  29.   }
  30.  }
  31.  if (e.keyCode == 40) {
  32.   if(mainChar[1]<2 && mapArray[mainChar[0]][mainChar[1]+1] == 0){
  33.   mainChar[1]+=1;
  34.  }
  35. }
  36. if (e.keyCode == 37) {
  37.  if(mainChar[0]>0 && mapArray[mainChar[0]-1][mainChar[1]] == 0){
  38.   mainChar[0]-=1;
  39.   }
  40.  }
  41.  if (e.keyCode == 39) {
  42.   if(mainChar[0]<2 && mapArray[mainChar[0]+1][mainChar[1]] == 0){
  43.   mainChar[0]+=1;
  44.  }
  45. }
  46.  
  47. }
  48.  
  49.  function draw(){
  50.   cxt.clearRect(0,0,300,300);
  51.  
  52.   for(var i in mapArray){
  53.  for (var j in mapArray[i]){
  54.   if (mapArray[i][j] == 1){
  55.    cxt.fillStyle="#DDDDDD";
  56.    cxt.fillRect(i*cellSize,j*cellSize,100,100);
  57.   }
  58.  }
  59.   }
  60.  
  61.   cxt.fillStyle="#FFCC00";
  62.   cxt.beginPath();
  63.  
  64.   if (timeCounter%2 == 0){
  65.    cxt.arc(mainChar[0]*cellSize+50,mainChar[1]*cellSize+50,50,0,2.0*Math.PI,true);
  66.   }
  67.  
  68.  cxt.closePath();
  69.  cxt.fill();
  70.  timeCounter += 1;
  71.  
  72.  }
  73.  
  74.  setInterval(draw,100);
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83. </script>
  84. </body>
  85. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement