Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. <!doctype=html>
  2. <html>
  3. <head>
  4. <tittle>
  5. </tittle>
  6. <style>
  7. body{
  8. background-color: black;
  9. display: flex;
  10. align-items:center;
  11. justify-content: center;
  12. }
  13. canvas{
  14. border:3px solid #fff;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <canvas></canvas>
  20. <script>
  21. //mapa
  22. const block=50;
  23. const map_blocks=5;
  24. const map_size=block*map_blocks;
  25. //canvas
  26. const canvas = document.querySelector('canvas');
  27. const ctx = canvas.getContext('2d');
  28. canvas.width=map_size;
  29. canvas.height=map_size;
  30. var X=canvas.width;
  31. var Y=canvas.height;
  32. //fruit
  33. var fruitX=((Math.floor(Math.random()*map_blocks)+1)*block)-block;
  34. var fruitY=((Math.floor(Math.random()*map_blocks)+1)*block)-block;
  35. var s;
  36.  
  37. function setup()
  38. {
  39. s = new snake();
  40. }
  41. function table()
  42. {
  43. ctx.fillStyle='black';
  44. ctx.fillRect(0,0,X,Y);
  45. for(let i=block; i<=X ; i+=block){
  46. ctx.beginPath();
  47. ctx.moveTo(i,0);
  48. ctx.lineTo(i, Y);
  49. ctx.lineWidth = 5;
  50. ctx.strokeStyle= 'white';
  51. ctx.stroke();
  52. ctx.beginPath();
  53. ctx.moveTo(0,i);
  54. ctx.lineTo(X, i);
  55. ctx.lineWidth = 5;
  56. ctx.strokeStyle= 'white';
  57. ctx.stroke();
  58. }
  59. }
  60.  
  61. function snake()
  62. {
  63. this.x=(X-block)/2;
  64. this.x=(Y-block)/2;
  65. this.xspeed=1;
  66. this.yspeed=0;
  67.  
  68. this.update=function()
  69. {
  70. this.x+=this.xspeed;
  71. this.y+=this.yspeed;
  72. }
  73. this.show = function ()
  74. {
  75. ctx.fillStyle='white';
  76. ctx.fillRect(this.x,this.y,block,block);
  77. }
  78. }
  79. function draw()
  80. {
  81. table();
  82. s.update();
  83. s.show();
  84. }
  85. setup();
  86. setInterval(draw,1000/5);
  87. </script>
  88. </body>
  89. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement