Guest User

Untitled

a guest
Jan 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. <html>
  2. <body onkeydown="keydown();" onkeyup="keyup();">
  3. <canvas id= "mybox" canvas height="500px" width="600px"></canvas>
  4.  
  5.  
  6. <script type="text/javascript">
  7. var Killx; var Killy;
  8. var Wallx; var Wally;
  9. var Playerx=0; var Playery=0;
  10. var controlleft; var controlright; var controlup; var controldown;
  11. var ctx = document.getElementById('mybox').getContext('2d');
  12.  
  13.  
  14. setInterval ("draw();",33);
  15. function draw()
  16. {
  17.  
  18. CreateBackground();
  19. CreatePlayer(0,0);
  20. MovePlayer();
  21.  
  22. CreateKill(250,300);
  23. CreateKill(200,200);
  24. CreateWall(0,250);
  25. kill();
  26. }
  27.  
  28. function CreateBackground()
  29. {
  30. ctx.fillStyle="black";
  31. ctx.fillRect(0,0,800,800);
  32. }
  33. //creates the background for the game
  34.  
  35. function CreateKill(Killx, Killy)
  36. {
  37. ctx.fillStyle="red";
  38. ctx.fillRect(Killx,Killy,25,25);
  39. }
  40. //this function creates kill blocks which if touched, will cause game over
  41.  
  42. function CreateWall(Wallx, Wally)
  43. {
  44. ctx.fillStyle="blue";
  45. ctx.fillRect(Wallx,Wally,25,25);
  46. }
  47. //this function creates wall blocks intended to limit passage way without killing player
  48.  
  49.  
  50. function CreatePlayer()
  51. {
  52. ctx.fillStyle="green";
  53. ctx.fillRect(Playerx,Playery,50,50);
  54. }
  55. //this function creates the player block
  56.  
  57. function MovePlayer()
  58. {
  59. if (controlleft == 1){Playerx -= 5;}
  60. if (controlright == 1) {Playerx += 5;}
  61. if (controlup == 1) {Playery -= 5;}
  62. if (controldown == 1) {Playery += 5;}
  63. }
  64. //this function moves the player block
  65.  
  66. function keydown()
  67. {
  68. if (event.keyCode == 37){controlleft = 1;}
  69. if (event.keyCode == 39 ){controlright = 1;}
  70. if (event.keyCode == 38) {controlup = 1;}
  71. if (event.keyCode == 40){controldown = 1;}
  72. }
  73. //when the keys are hit, the variables that control movement are set to 1
  74.  
  75. function keyup()
  76. {
  77. if (event.keyCode == 37){controlleft = 0;}
  78. if (event.keyCode == 39){controlright = 0;}
  79. if (event.keyCode == 38){controlup = 0;}
  80. if (event.keyCode == 40){controldown = 0;}
  81. }
  82. //when the keys are released, the variables that control movement are set to 0
  83.  
  84. function kill()
  85. {
  86. var a = (Playery) - (Killy);
  87. var b = (Playerx) - (Killx);
  88. var c = Math.sqrt((a)*(a)+(b)*(b));
  89. if (c < 100000000000000000) {Playery = 0; Playerx = 0;}
  90. }
  91. //if the player touches a kill block, the player is reset to the start
  92.  
  93.  
  94. </script>
  95. </body>
  96. </html>
Add Comment
Please, Sign In to add comment