Guest User

Untitled

a guest
Jun 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. <html>
  2. <body onkeydown="keydown();" onkeyup="keyup();">
  3. <canvas id= "mybox" canvas height="500px" width="600px"></canvas>
  4. <script type= "text/javascript">
  5.  
  6. var ctx = document.getElementById('mybox').getContext('2d');
  7. var controlleft=0; var controlright=0; var controlup=0; var controldown=0; var controlspace=0;
  8.  
  9. var bullet = {};
  10. bullet.x =100;
  11. bullet.y =100;
  12. bullet.On = true;
  13.  
  14. /*bullet.fire = function fire()
  15. {
  16. if (controlspace == 1)
  17. {
  18. bullet.On = true;
  19. }
  20.  
  21. };
  22. */
  23.  
  24. /*bullet.draw = function draw()
  25. {
  26.  
  27.  
  28.  
  29. ctx.fillStyle = "Red";
  30. ctx.fillRect = (bullet.x,bullet.y,10,10);
  31.  
  32. };
  33. */
  34.  
  35.  
  36. var tank = {};
  37. tank.x = 10;
  38. tank.y = 10;
  39. tank.Cannonx = 50;
  40. tank.Cannony = 15;
  41. tank.Right = true;
  42. tank.Left = false;
  43. tank.Down = false;
  44. tank.Up = false;
  45.  
  46. tank.draw = function draw()
  47. {
  48. ctx.fillStyle="blue";
  49. ctx.fillRect(tank.x,tank.y,50,50);
  50.  
  51. ctx.fillStyle="blue";
  52. ctx.fillRect((tank.x + tank.Cannonx), (tank.y + tank.Cannony), 20,20);
  53. };
  54.  
  55. tank.move = function move()
  56. {
  57. if (controlup == 1)
  58. {
  59. tank.y -= 10;
  60. tank.Up = true;
  61. tank.Right = false;
  62. tank.Down = false;
  63. tank.Left = false;
  64. }
  65. if (controldown == 1)
  66. {
  67. tank.y += 10;
  68. tank.Up = false;
  69. tank.Right = false;
  70. tank.Down = true;
  71. tank.Left = false;
  72. }
  73. if (controlleft == 1)
  74. {
  75. if (controldown == 0)
  76. {
  77. if (controlup == 0)
  78. {
  79. tank.x -= 10;
  80. tank.Up = false;
  81. tank.Right = false;
  82. tank.Down = false;
  83. tank.Left = true;
  84. }
  85. }
  86. }
  87. if (controlright ==1)
  88. {
  89. if (controldown == 0)
  90. {
  91. if (controlup == 0)
  92. {
  93. tank.x += 10;
  94. tank.Up = false
  95. tank.Right = true;
  96. tank.Down = false;
  97. tank.Left = false;
  98. }
  99. }
  100. }
  101. };
  102.  
  103. tank.turn = function turn()
  104. {
  105. if (tank.Right == true)
  106. {
  107. tank.Cannonx = 50;
  108. tank.Cannony = 15;
  109. }
  110. if (tank.Left == true)
  111. {
  112. tank.Cannonx = -20;
  113. tank.Cannony = 15;
  114. }
  115. if (tank.Up == true)
  116. {
  117. tank.Cannonx = 15;
  118. tank.Cannony = -20;
  119. }
  120. if (tank.Down == true)
  121. {
  122. tank.Cannonx = 15;
  123. tank.Cannony = 50;
  124. }
  125. };
  126.  
  127.  
  128.  
  129.  
  130.  
  131. setInterval ("draw();",33);
  132. function draw()
  133. {
  134.  
  135. CreateBackground();
  136. tank.draw();
  137. tank.move();
  138. tank.turn();
  139.  
  140. ctx.fillStyle = "blue";
  141. ctx.fillRect = (100,100,10,10);
  142.  
  143. //bullet.draw();
  144. //bullet.fire();
  145. }
  146.  
  147.  
  148.  
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155.  
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165. function keydown()
  166. {
  167. if (event.keyCode == 37)
  168. {
  169. controlleft = 1;
  170.  
  171. }
  172. if (event.keyCode == 39 )
  173. {
  174. controlright = 1;
  175.  
  176. }
  177. if (event.keyCode == 38)
  178. {
  179. controlup = 1;
  180. }
  181. if (event.keyCode == 40){controldown = 1;}
  182. if (event.keyCode == 32){controlspace = 1;}
  183.  
  184. }
  185. //when the keys are hit, the variables that control movement are set to 1
  186. function keyup()
  187. {
  188. if (event.keyCode == 37){controlleft = 0;}
  189. if (event.keyCode == 39){controlright = 0;}
  190. if (event.keyCode == 38){controlup = 0;}
  191. if (event.keyCode == 40){controldown = 0;}
  192. if (event.keyCode == 32){controlspace = 0;}
  193.  
  194. }
  195. //function responsible for keyboard interface, when key is released, buttons stop working
  196.  
  197. function CreateBackground()
  198. {
  199. ctx.fillStyle="black";
  200. ctx.fillRect(0,0,800,800);
  201. }
  202.  
  203. </script>
  204. </body>
  205. </html>
Add Comment
Please, Sign In to add comment