Guest User

Untitled

a guest
Jan 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 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. var a; var b; var c; var d;
  13.  
  14. setInterval ("draw();",33);
  15. function draw()
  16. {
  17.  
  18. CreateBackground();
  19. CreatePlayer();
  20. MovePlayerVertical();
  21. MovePlayerHorizontal();
  22.  
  23.  
  24.  
  25. CreateKill(250,300);
  26. Kill(250,300);
  27. CreateKill(200,200);
  28. Kill(200,200);
  29. CreateWall(250,250);
  30. WallHorizontal(250,250); WallVertical(250,250);
  31. CreateWall(400,200);
  32. WallHorizontal(400,200); WallVertical(400,200);
  33.  
  34.  
  35. }
  36.  
  37. function CreateBackground()
  38. {
  39. ctx.fillStyle="black";
  40. ctx.fillRect(0,0,800,800);
  41. }
  42. //creates the background for the game
  43.  
  44. function CreateKill(Killx, Killy)
  45. {
  46.  
  47. ctx.fillStyle="red";
  48. ctx.fillRect(Killx,Killy,25,25);
  49. }
  50. //this function creates kill blocks which if touched, will cause game over
  51.  
  52. function CreateWall(Wallx, Wally)
  53. {
  54. ctx.fillStyle="blue";
  55. ctx.fillRect(Wallx,Wally,25,25);
  56. }
  57. //this function creates wall blocks intended to limit passage way without killing player
  58.  
  59.  
  60. function CreatePlayer()
  61. {
  62. ctx.fillStyle="green";
  63. ctx.fillRect(Playerx,Playery,25,25);
  64. }
  65. //this function creates the player block
  66.  
  67. function MovePlayerVertical()
  68. {
  69. if (controlup == 1) {Playery -= 5;}
  70. if (controldown == 1) {Playery += 5;}
  71. }
  72. //this function moves the player block
  73.  
  74. function MovePlayerHorizontal()
  75. {
  76. if (controlleft == 1){Playerx -= 5;}
  77. if (controlright == 1) {Playerx += 5;}
  78. }
  79.  
  80.  
  81. function keydown()
  82. {
  83. if (event.keyCode == 37){controlleft = 1;}
  84. if (event.keyCode == 39 ){controlright = 1;}
  85. if (event.keyCode == 38) {controlup = 1;}
  86. if (event.keyCode == 40){controldown = 1;}
  87. }
  88. //when the keys are hit, the variables that control movement are set to 1
  89.  
  90. function keyup()
  91. {
  92. if (event.keyCode == 37){controlleft = 0;}
  93. if (event.keyCode == 39){controlright = 0;}
  94. if (event.keyCode == 38){controlup = 0;}
  95. if (event.keyCode == 40){controldown = 0;}
  96. }
  97. //when the keys are released, the variables that control movement are set to 0
  98.  
  99. function Kill(Killx, Killy)
  100. {
  101. if ( Math.abs(Playery - Killy) < 25 && Math.abs(Playerx - Killx) < 25) { Playery = 0; Playerx = 0;}
  102.  
  103. }
  104.  
  105. function WallVertical(Wallx, Wally)
  106. {
  107. if (controlup == 1)
  108. {
  109. if ( Math.abs(Playery - Wally) < 25 && Math.abs(Playerx - Wallx) < 25 && (Playery - Wally) < 25) {Playery = (Wally + 25);}
  110. }
  111. if (controldown ==1)
  112. {
  113. if ( Math.abs(Playery - Wally) < 25 && Math.abs(Playerx - Wallx) < 25 && (Playery - Wally) > -25) {Playery = (Wally - 25);}
  114. }
  115. }
  116.  
  117. function WallHorizontal(Wallx, Wally)
  118. {
  119. if (controlleft == 1)
  120. {
  121. if ( Math.abs(Playery - Wally) < 25 && Math.abs(Playerx - Wallx) < 25 && (Playerx - Wallx) < 25) {Playerx = (Wallx + 25);}
  122. }
  123. if (controlright ==1)
  124. {
  125. if ( Math.abs(Playery - Wally) < 25 && Math.abs(Playerx - Wallx) < 25 && (Playerx - Wallx) > -25) {Playerx = (Wallx - 25);}
  126. }
  127. }
  128.  
  129.  
  130. //if the player touches a kill block, the player is reset to the start
  131. //problems with the kill function still need to be solved
  132.  
  133.  
  134. </script>
  135. </body>
  136. </html>
Add Comment
Please, Sign In to add comment