Guest User

ChatGPT Tetris SVG

a guest
Sep 20th, 2023
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.68 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Tetris Clone</title>
  5. <style>
  6. svg {
  7. border: 1px solid black;
  8. display: block;
  9. margin: 0 auto;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <svg id="gameCanvas" width="200" height="400"></svg>
  15. <script>
  16. const svg = document.getElementById('gameCanvas');
  17. const board = [];
  18. const tetrominoes = [
  19. [
  20. [1, 1, 1, 1], // I-shape
  21. ],
  22. [
  23. [1, 1], // O-shape
  24. [1, 1],
  25. ],
  26. [
  27. [1, 1, 1], // T-shape
  28. [0, 1, 0],
  29. ],
  30. [
  31. [1, 1, 1], // L-shape
  32. [1, 0, 0],
  33. ],
  34. [
  35. [1, 1, 1], // J-shape
  36. [0, 0, 1],
  37. ],
  38. [
  39. [1, 1, 0], // S-shape
  40. [0, 1, 1],
  41. ],
  42. [
  43. [0, 1, 1], // Z-shape
  44. [1, 1, 0],
  45. ],
  46. ];
  47.  
  48. let currentTetromino = getRandomTetromino();
  49. let tetrominoX = 0;
  50. let tetrominoY = 0;
  51. let score = 0;
  52. let gameInterval;
  53.  
  54. // Create the game board (20x10 grid)
  55. for (let i = 0; i < 20; i++) {
  56. board[i] = Array(10).fill(0);
  57. }
  58.  
  59. // Get a random tetromino
  60. function getRandomTetromino() {
  61. const index = Math.floor(Math.random() * tetrominoes.length);
  62. return tetrominoes[index];
  63. }
  64.  
  65. // Draw the game board
  66. function drawBoard() {
  67. svg.innerHTML = '';
  68. for (let row = 0; row < 20; row++) {
  69. for (let col = 0; col < 10; col++) {
  70. if (board[row][col]) {
  71. const block = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  72. block.setAttribute('x', col * 20);
  73. block.setAttribute('y', row * 20);
  74. block.setAttribute('width', 20);
  75. block.setAttribute('height', 20);
  76. block.setAttribute('fill', 'blue');
  77. svg.appendChild(block);
  78. }
  79. }
  80. }
  81. }
  82.  
  83. // Draw the current tetromino
  84. function drawTetromino() {
  85. for (let row = 0; row < currentTetromino.length; row++) {
  86. for (let col = 0; col < currentTetromino[row].length; col++) {
  87. if (currentTetromino[row][col]) {
  88. const block = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  89. block.setAttribute('x', (tetrominoX + col) * 20);
  90. block.setAttribute('y', (tetrominoY + row) * 20);
  91. block.setAttribute('width', 20);
  92. block.setAttribute('height', 20);
  93. block.setAttribute('fill', 'blue');
  94. svg.appendChild(block);
  95. }
  96. }
  97. }
  98. }
  99.  
  100. // Check if the current tetromino can move to the specified position
  101. function canMove(dx, dy) {
  102. for (let row = 0; row < currentTetromino.length; row++) {
  103. for (let col = 0; col < currentTetromino[row].length; col++) {
  104. if (currentTetromino[row][col]) {
  105. const newX = tetrominoX + col + dx;
  106. const newY = tetrominoY + row + dy;
  107. if (
  108. newX < 0 ||
  109. newX >= 10 ||
  110. newY >= 20 ||
  111. (newY >= 0 && board[newY][newX])
  112. ) {
  113. return false;
  114. }
  115. }
  116. }
  117. }
  118. return true;
  119. }
  120.  
  121. // Move the current tetromino down
  122. function moveDown() {
  123. if (canMove(0, 1)) {
  124. tetrominoY++;
  125. } else {
  126. mergeTetromino();
  127. clearLines();
  128. currentTetromino = getRandomTetromino();
  129. tetrominoX = 0;
  130. tetrominoY = 0;
  131. if (!canMove(0, 0)) {
  132. // Game over
  133. clearInterval(gameInterval);
  134. alert('Game Over. Score: ' + score);
  135. }
  136. }
  137. }
  138.  
  139. // Merge the current tetromino into the game board
  140. function mergeTetromino() {
  141. for (let row = 0; row < currentTetromino.length; row++) {
  142. for (let col = 0; col < currentTetromino[row].length; col++) {
  143. if (currentTetromino[row][col]) {
  144. const x = tetrominoX + col;
  145. const y = tetrominoY + row;
  146. if (y >= 0) {
  147. board[y][x] = 1;
  148. }
  149. }
  150. }
  151. }
  152. }
  153.  
  154. // Clear completed lines
  155. function clearLines() {
  156. for (let row = 0; row < 20; row++) {
  157. if (board[row].every((block) => block)) {
  158. board.splice(row, 1);
  159. board.unshift(Array(10).fill(0));
  160. score += 100;
  161. }
  162. }
  163. }
  164.  
  165. // Handle keyboard input
  166. document.addEventListener('keydown', (event) => {
  167. if (event.key === 'ArrowLeft' && canMove(-1, 0)) {
  168. tetrominoX--;
  169. } else if (event.key === 'ArrowRight' && canMove(1, 0)) {
  170. tetrominoX++;
  171. } else if (event.key === 'ArrowDown') {
  172. moveDown();
  173. } else if (event.key === 'ArrowUp') {
  174. rotateTetromino();
  175. }
  176. drawBoard();
  177. drawTetromino();
  178. });
  179.  
  180. // Rotate the current tetromino
  181. function rotateTetromino() {
  182. const originalTetromino = currentTetromino;
  183. const rotatedTetromino = [];
  184. const rows = originalTetromino.length;
  185. const cols = originalTetromino[0].length;
  186. for (let col = 0; col < cols; col++) {
  187. rotatedTetromino[col] = [];
  188. for (let row = 0; row < rows; row++) {
  189. rotatedTetromino[col][row] = originalTetromino[row][cols - col - 1];
  190. }
  191. }
  192. if (canRotate(rotatedTetromino)) {
  193. currentTetromino = rotatedTetromino;
  194. }
  195. }
  196.  
  197. // Check if the current tetromino can rotate
  198. function canRotate(rotatedTetromino) {
  199. for (let row = 0; row < rotatedTetromino.length; row++) {
  200. for (let col = 0; col < rotatedTetromino[row].length; col++) {
  201. if (rotatedTetromino[row][col]) {
  202. const newX = tetrominoX + col;
  203. const newY = tetrominoY + row;
  204. if (
  205. newX < 0 ||
  206. newX >= 10 ||
  207. newY >= 20 ||
  208. (newY >= 0 && board[newY][newX])
  209. ) {
  210. return false;
  211. }
  212. }
  213. }
  214. }
  215. return true;
  216. }
  217.  
  218. // Start the game loop
  219. gameInterval = setInterval(() => {
  220. moveDown();
  221. drawBoard();
  222. drawTetromino();
  223. }, 1000);
  224.  
  225. </script>
  226. </body>
  227. </html>
  228.  
Advertisement
Add Comment
Please, Sign In to add comment