Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.76 KB | None | 0 0
  1. // Ai's first game yay~!
  2. // Code name uwu~
  3.  
  4. var context, controller, rectangle, loop, floor;
  5. var randomNumberBetween0and19 = Math.floor(Math.random() * 300);
  6. var powerups = ["Speed", "Jump"]
  7.  
  8. function xdist(x1,x2) {
  9. return Math.abs(x1-x2)
  10. }
  11. function ydist(y1,y2) {
  12. return Math.abs(y1-y2)
  13. }
  14.  
  15. /*
  16. context ~ that's our window size!
  17. controller ~ handles our controls; keyboard checks and stuff
  18. rectangle ~ this is our character yay!! just the dimensions but yay!
  19. loop ~ this is our game loop!!
  20. */
  21.  
  22. context = document.querySelector("canvas").getContext("2d");
  23.  
  24. context.canvas.height = 360;
  25. context.canvas.width = 800;
  26.  
  27. rectangle = {
  28.  
  29. height:32,
  30. jumping:true, // jumping is true when they are in the air!
  31. width:32,
  32. x:360, // center of canvas!
  33. x_velocity:0,
  34. y:randomNumberBetween0and19,
  35. y_velocity:0,
  36. grav:1.1,
  37. wrap:800,
  38. prevframe: "false",
  39. jumps:0,
  40. maxjumps:1,
  41. speed:3
  42.  
  43. };
  44.  
  45. controller = {
  46.  
  47. left:false,
  48. right:false,
  49. up:false,
  50. keyListener:function(event) {
  51. var key_state = (event.type == "keydown")?true:false;
  52. // Checks whether a key is being held! If it's being held down, set to true. If not, false.
  53.  
  54. switch(event.keyCode) {
  55.  
  56. case 37: //left key
  57. controller.left = key_state;
  58. break;
  59. case 32: //spacebar
  60. controller.up = key_state;
  61. break;
  62. case 39: //right key
  63. controller.right = key_state;
  64. break;
  65.  
  66. }
  67. }
  68. };
  69.  
  70. floor = {
  71.  
  72. floor:1,
  73. flooronestring:"Welcome to SkillWrapper!\n\nHold right to continue!",
  74. floortwostring:"Random powerups are inside yellow blocks. Pick this one up!",
  75. floorthreestring:"Dodge any scary things! You can bounce on them to kill them, too!",
  76. floorfourstring:"Good luck!",
  77. floorreset:"false",
  78. // handling powerup stuff too i guess??
  79. powerupwidth:16,
  80. powerupheight:16,
  81. powerx:64,
  82. powery:64,
  83. powerupexist:"false",
  84. powerupname: powerups[Math.floor(Math.random() * powerups.length)],
  85. powerupcreate: "false",
  86. createthisturn: "false"
  87.  
  88.  
  89. }
  90.  
  91. loop = function() {
  92.  
  93. // floor code
  94. if (floor.floor == 2 && floor.powerupcreate == "false") {
  95. floor.powerupexist = "true"
  96. floor.powerx = 200
  97. floor.powery = 280
  98. floor.powerupname = "Speed"
  99. floor.powerupcreate = "true"
  100. }
  101.  
  102. if (xdist(rectangle.x,floor.powerx) <= 16 && floor.powerupexist == "true") {
  103. if (ydist(rectangle.y,floor.powery) <= 16) {
  104.  
  105. if (floor.powerupname == "Speed" && floor.powerupexist == "true") {
  106. rectangle.speed += .5
  107. floor.powerupexist = "false"
  108. }
  109. else if (floor.powerupname == "Jump") {
  110. rectangle.maxjumps += 1
  111. }
  112.  
  113. }
  114. }
  115. if (controller.up && rectangle.prevframe == "false") {
  116.  
  117. if (rectangle.jumps >= 1) {
  118. rectangle.prevframe = "true";
  119. rectangle.y_velocity = 0;
  120. if (rectangle.jumps >= 1) {
  121. rectangle.y_velocity -= 17;
  122. }
  123. rectangle.jumps -= 1;
  124. rectangle.jumping = true;
  125. }
  126. }
  127. else if (rectangle.prevframe == "true") {
  128. if (controller.up) {
  129. rectangle.prevframe = "true"
  130. }
  131. else {
  132. rectangle.prevframe = "false"
  133. }
  134. }
  135.  
  136. if (controller.left) {
  137.  
  138. rectangle.x_velocity = -rectangle.speed;
  139.  
  140. }
  141.  
  142. if (controller.right) {
  143.  
  144. rectangle.x_velocity = rectangle.speed;
  145.  
  146. }
  147.  
  148. rectangle.y_velocity += rectangle.grav;//gravity yay!
  149. rectangle.x += rectangle.x_velocity;
  150. rectangle.y += rectangle.y_velocity;
  151. rectangle.x_velocity = 0
  152. if (rectangle.y_velocity >= 15) {
  153. rectangle.y_velocity = 15
  154. }
  155.  
  156.  
  157. // if rectangle is falling below floor line
  158. if (rectangle.y > 360 - 48 - 32) {
  159. rectangle.jumping = false;
  160. rectangle.y = 360 - 48 - 32;
  161. rectangle.y_velocity = 0;
  162. rectangle.jumps = rectangle.maxjumps;
  163. }
  164.  
  165. if (rectangle.x < -32) {
  166.  
  167. rectangle.x = -32;
  168.  
  169. }
  170. else if (rectangle.x > 800) {
  171.  
  172. floor.floor += 1
  173. floor.floorreset = "true"
  174. rectangle.x = -32;
  175.  
  176. }
  177.  
  178.  
  179. context.fillStyle = "#fadadd";
  180. context.fillRect(0,0,800,320);//x, y, width, height
  181. context.fillStyle = "#FF007F";// hex for red
  182. context.beginPath();
  183. context.rect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
  184. context.fill();
  185. context.strokeStyle = "#DE1738";
  186. context.lineWidth = 4;
  187. context.beginPath();
  188. context.moveTo(0, 312);
  189. context.lineTo(800,312);
  190. context.stroke();
  191. context.font = "20px Verdana";
  192. context.fillText("Floor: " + floor.floor, 600, 32)
  193. if (floor.floor == 1) {
  194. context.fillText(floor.flooronestring, 64,160)
  195. }
  196. else if (floor.floor == 2) {
  197. context.fillText(floor.floortwostring, 64,160)
  198. }
  199. else if (floor.floor == 3) {
  200. context.fillText(floor.floorthreestring, 64,160)
  201. }
  202. else if (floor.floor == 4) {
  203. context.fillText(floor.floorfourstring, 64,160)
  204. }
  205. if (floor.powerupexist == "true") {
  206. context.fillStyle = "#fef65b"
  207. context.beginPath();
  208. context.rect(floor.powerx,floor.powery,floor.powerupwidth,floor.powerupheight);
  209. context.fill();
  210. }
  211.  
  212. // call update when browser is ready to redraw!
  213. window.requestAnimationFrame(loop);
  214.  
  215. };
  216.  
  217. window.addEventListener("keydown", controller.keyListener);
  218. window.addEventListener("keyup", controller.keyListener);
  219. window.requestAnimationFrame(loop);
  220. /*
  221. These were slightly misleading but I understand them!
  222. What this is doing is setting event listeners to check when you are **HOLDING** a key down.
  223. Up at the listener, it'll run the function, and if the key is pressed down, it'll be set to true.
  224. If the key is no longer being held down, it'll set itself to false. This'll let you check whether a key is being held
  225. And properly identify whether it is left, right, or up to execute on it.
  226. I initially thought it was checking the arrow key up and down, lol!!
  227. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement