Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. const COLORS = {
  2. left: "#118c8b",
  3. right: "#f14d49",
  4. other: "#bca18d",
  5. }
  6.  
  7. function Block(x, y, size, type = "other") {
  8. const FILL_STYLE = COLORS[type];
  9. return {
  10. get x() { return x },
  11. get y() { return y },
  12. draw(ctx) {
  13. ctx.fillStyle = FILL_STYLE;
  14. ctx.fillRect(x, y, size, size);
  15. ctx.strokeRect(x, y, size, size);
  16. },
  17. move(addX, addY) {
  18. x += addX * size;
  19. y += addY * size;
  20. },
  21. };
  22. }
  23.  
  24. const b = new Block(x, y, size, type); // instantiate
  25. // or you no longer need the new token
  26. const b = Block(x, y, size, type);
  27. b.move(1, 0); // move right
  28. filter(a => a < b.y); // access getter
  29.  
  30. const O_PIECE = "####";
  31. const T_PIECE = "### # ";
  32. const BAR_PIECE = "####";
  33. const Z_PIECE = " # ###";
  34. const S_PIECE = " #### ";
  35.  
  36. const pieceData = [
  37. createPiece(O_PIECE,2),
  38. createPiece(T_PIECE,3),
  39. createPiece(S_PIECE,3),
  40. createPiece(Z_PIECE,2),
  41. createPiece(BAR_PIECE,4),
  42. ];
  43.  
  44. function createPiece(layout, width, type = "other") {
  45. const height = layout.length / width;
  46. var i = layout.length;
  47. const build = []
  48. while(i--) {
  49. if(layout[i] === "#") {
  50. const x = i % width;
  51. const y = i / width | 0;
  52. build.push([x, y]);
  53. }
  54. }
  55. return {
  56. builds: [build],
  57. type,
  58. };
  59. }
  60.  
  61. const gMath = {}; // g for game maths
  62. gMath.mod = (n, m) => (n % m + m) % m;
  63. gMath.randInt = (min, max = min + (min = 0)) => Math.floor(Math.random() * (max - min) + min);
  64. gMath.randItem = arr => arr[gMath.randInt(arr.length)];
  65.  
  66. requestAnimationFrame(mainLoop); // will start the animation loop when execution is idle
  67. var frameCounter = 0;
  68. var gameSpeed = 30; // Render every 30 frame or 1/2 second
  69. var updateRender = true; // when true will re-render the game
  70. function mainLoop(time) { // time is a high resolution time supplied by the DOM
  71.  
  72. // call IO code here
  73.  
  74. if (frameCounter % gameSpeed === 0) {
  75. updateGame(); // call the game update
  76. updateRender = true;
  77. }
  78.  
  79. if (updateRender) {
  80. updateRender = false;
  81. draw(); // draw the game
  82. }
  83.  
  84.  
  85. frameCounter ++;
  86. requestAnimationFrame(mainLoop);
  87. }
  88.  
  89. const IO = {
  90. keys: { // list only the keys you need
  91. ArrowLeft: false,
  92. ArrowRight: false,
  93. ArrowDown: false,
  94. Escape: false, // reset or end game
  95. },
  96. clear() {
  97. for(const key of Object.keys(IO.keys) { IO.keys[key] = false }
  98. },
  99. keyboard(event) {
  100. if (IO.keys[event.keyCode] !== undefined) {
  101. IO.keys[event.keyCode] = event.type === "keydown";
  102. event.preventDefault();
  103. }
  104. }
  105. }
  106. addEventListener("keydown", IO.keyboard);
  107. addEventListener("keyup", IO.keyboard);
  108.  
  109. const GAME_DOWN = "ArrowDown"; // bind key to action
  110. const GAME_LEFT = "ArrowLeft"; // bind key to action
  111. const GAME_RIGHT = "ArrowRight"; // bind key to action
  112. const KEY_REPEAT_RATE = 20; // every 20 frames 5 times a second
  113. var rightRepeat = 0;
  114. function doInput() {
  115. // Action while key is down
  116. if (IO.keys[GAME_LEFT]) {
  117. /* do stuff */
  118. }
  119.  
  120. // Action only once per key down
  121. if (IO.keys[GAME_DOWN]) {
  122. IO.keys[GAME_DOWN] = false; // turn off key.
  123. /* do stuff */
  124. }
  125.  
  126. // Action repeats on key down
  127. if (IO.keys[GAME_RIGHT]) {
  128. if (rightRepeat === 0) {
  129. rightRepeat = keyRepeatRate;
  130. /* do stuff */
  131. }
  132. rightRepeat--; // count down to next repeat
  133. } else {
  134. rightRepeat = 0; // reset repeat
  135. }
  136.  
  137. // when changing levels clear the input state so if the user
  138. // is holding a key down it does not effect the new level or game
  139. IO.clear();
  140. }
  141.  
  142. ;((container)=> {
  143. "use strict";
  144. /*
  145. All game code in here
  146.  
  147. */
  148. })(document.body); // the container for your game
  149.  
  150. const gDOM = {}; // g for game DOM
  151. gDOM.create = (tag, props = {}) => Object.assign(document.createElement(tag), props);
  152. gDOM.append = (parent, ...children) => children.reduce((p, c) => (p.appendChild(c), p), parent);
  153.  
  154. const styles = `
  155. tetris--score-display {
  156. /* add style stuff
  157. }
  158. /* and so on */
  159. `;
  160. gDOM.append(container, gDOM.create("style", {innerHTML: styles}));
  161. const canvas = gDOM.create("canvas", {width: gameWidth, height: gameHeight});
  162. const block = gDOM.create("canvas", {width: blockWidth, height: blockHeight});
  163. const score = gDOM.create("div", {textContent: "Score:", className: "tetris--score-display"});
  164. const level = gDOM.create("div", {textContent: "Level:", className: "tetris--level-display"});
  165.  
  166. const ctx = canvas.getContext("2d");
  167. const bctx = block.getContext("2d");
  168.  
  169. gDOM.append(container, canvas, block, score, level);
  170.  
  171. // To set score or level
  172. score.innerText = "Score: " + this.score; // Note you had this.score.toString() the toString is autonomic
  173. level.innerText = "Level: " + this.level;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement