Advertisement
Guest User

Untitled

a guest
Aug 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.52 KB | None | 0 0
  1. <html>
  2. <head>
  3. <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
  4. <script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
  5. <style type="text/css">
  6. #stage {
  7. height: 100%;
  8. width: auto;
  9. position: fixed;
  10. transform: translate(-50%, -50%);
  11. left: 50%;
  12. top: 50%;
  13. }
  14.  
  15. body {
  16. background-color: darkgrey;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="stage"></canvas>
  22.  
  23. <script>
  24. var Game = {
  25. blocksIn: 0,
  26. gamewidth: 20,
  27. gameheight: 23,
  28. canvas: false,
  29. size: 20,
  30.  
  31. init: function() {
  32.  
  33. this.canvas = $('#stage');
  34.  
  35. this.canvas.attr({
  36. width: this.gamewidth * this.size,
  37. height: this.gameheight * this.size
  38. });
  39.  
  40. this.loop();
  41. this.bindings();
  42.  
  43. },
  44.  
  45. loop: function() {
  46.  
  47. // this.clearCanvas(); // wipe the canvas
  48.  
  49. // position of painter
  50. var posx = 0;
  51. var posy = 0;
  52.  
  53. //looping through the stage to paint canvas
  54. this.stage.forEach(function(row) {
  55. //looping through each letter in stage for rows
  56. row.split("").forEach(function(cell) {
  57. this.paint(posx, posy, cell);
  58. posx = posx + 1;
  59. }.bind(this));
  60.  
  61. // reset for next loop
  62. posx = 0;
  63. posy = posy + 1;
  64. }.bind(this));
  65. },
  66.  
  67. clearCanvas: function() { // might not be needed
  68. var c = document.getElementById("stage");
  69. c.getContext("2d").clearRect(0, 0, c.width, c.height);
  70. },
  71.  
  72. paint: function(x, y, what) {
  73. var c = document.getElementById("stage");
  74. var ctx = c.getContext("2d");
  75. ctx.fillStyle = this.sprites[what];
  76. ctx.fillRect(x * this.size, y * this.size, this.size, this.size);
  77. },
  78.  
  79. getChar: function(x, y) {
  80. return this.stage[y].charAt(x);
  81. },
  82.  
  83. setChar: function(char, x, y) {
  84. var row = this.stage[y];
  85. this.stage[y] = row.substr(0, x) + char + row.substr(x + 1);
  86. },
  87.  
  88. getPosition: function(char) {
  89. // position of painter
  90. var x = 0;
  91. var y = 0;
  92. var pos = {};
  93.  
  94. //looping through the stage to determine position
  95. this.stage.forEach(function(row) {
  96. row.split("").forEach(function(cell) {
  97. if (cell == char) pos = {
  98. x, y
  99. };
  100. x = x + 1;
  101. }.bind(this));
  102. x = 0;
  103. y = y + 1;
  104. }.bind(this));
  105. return pos;
  106. },
  107.  
  108. bindings: function() {
  109. document.addEventListener('keyup', function(e) {
  110.  
  111. var pos = this.getPosition('p');
  112. var check = this.getPosition('p');
  113.  
  114. var key = e.key;
  115.  
  116. if (key == "w") check.y--;
  117. if (key == "s") check.y++;
  118. if (key == "a") check.x--;
  119. if (key == "d") check.x++;
  120.  
  121. if (this.getChar(check.x, check.y) == "o") {
  122. // perform the character movement
  123. this.setChar('p', check.x, check.y);
  124. this.setChar('o', pos.x, pos.y);
  125. }
  126.  
  127. // boulder detection
  128. if (this.getChar(check.x, check.y) == "b") {
  129.  
  130. // if player bumps into boulder, let's see if we can push it
  131. var rock = {
  132. x: check.x,
  133. y: check.y
  134. }
  135.  
  136. if (key == "w") check.y--;
  137. if (key == "s") check.y++;
  138. if (key == "a") check.x--;
  139. if (key == "d") check.x++;
  140.  
  141. if (this.getChar(check.x, check.y) == "o") {
  142. this.setChar('b', check.x, check.y);
  143. this.setChar('p', rock.x, rock.y);
  144. this.setChar('o', pos.x, pos.y);
  145. }
  146.  
  147. if (this.getChar(check.x, check.y) == "e") {
  148. this.setChar('e', check.x, check.y);
  149. this.setChar('p', rock.x, rock.y);
  150. this.setChar('o', pos.x, pos.y);
  151. this.BlocksIn++;
  152. }
  153.  
  154. }
  155. this.loop();
  156. }.bind(this), false);
  157. },
  158.  
  159. stage: ['wwwwwwwwwwwwwwwwwwww',
  160. 'woooooooooooooooooow',
  161. 'wooooooowwwwwoooooow',
  162. 'wooooooowooowoooooow',
  163. 'wooooooowoeowoooooow',
  164. 'wooooooowooowoooooow',
  165. 'wooooooowooowoooooow',
  166. 'wooooooowooowoooooow',
  167. 'wooooooowooowoooooow',
  168. 'wooooooowooowoooooow',
  169. 'wooooooowooowoooooow',
  170. 'wooooooowooowoooooow',
  171. 'wooooooowooowoooooow',
  172. 'wooooooowooowoooooow',
  173. 'wooooooowooowoooooow',
  174. 'wooooooowooowoooooow',
  175. 'wooooooowobowoooooow',
  176. 'wooooooowooowoooooow',
  177. 'wooooooowopowoooooow',
  178. 'wooooooowooowoooooow',
  179. 'woooooooooooooooooow',
  180. 'woooooooooooooooooow',
  181. 'wwwwwwwwwwwwwwwwwwww',
  182. ],
  183.  
  184. sprites: {
  185. p: "blue",
  186. w: "darkgrey",
  187. o: "beige",
  188. b: "green",
  189. e: "black"
  190. }
  191. }
  192.  
  193. var game = $.extend({}, Game).init();
  194. </script>
  195. </body>
  196. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement