Guest User

Maze ChatGPT

a guest
Sep 20th, 2023
171
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 1 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Maze Game</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="400" height="400"></svg>
  15. <script>
  16. const svg = document.getElementById('gameCanvas');
  17. const cellSize = 20;
  18. const mazeWidth = Math.floor(svg.getAttribute('width') / cellSize);
  19. const mazeHeight = Math.floor(svg.getAttribute('height') / cellSize);
  20. const maze = generateMaze(mazeWidth, mazeHeight);
  21. const player = createPlayer(0, 0);
  22.  
  23. function generateMaze(width, height) {
  24. const maze = new Array(height);
  25. for (let row = 0; row < height; row++) {
  26. maze[row] = new Array(width).fill(true);
  27. }
  28.  
  29. const stack = [];
  30. let currentCell = { x: 0, y: 0 };
  31.  
  32. while (true) {
  33. const neighbors = getUnvisitedNeighbors(currentCell, maze);
  34. if (neighbors.length > 0) {
  35. stack.push(currentCell);
  36. const nextCell = neighbors[Math.floor(Math.random() * neighbors.length)];
  37. removeWall(currentCell, nextCell, maze);
  38. currentCell = nextCell;
  39. } else if (stack.length > 0) {
  40. currentCell = stack.pop();
  41. } else {
  42. break;
  43. }
  44. }
  45.  
  46. return maze;
  47. }
  48.  
  49. function getUnvisitedNeighbors(cell, maze) {
  50. const { x, y } = cell;
  51. const neighbors = [];
  52.  
  53. if (x > 1) neighbors.push({ x: x - 2, y });
  54. if (x < mazeWidth - 2) neighbors.push({ x: x + 2, y });
  55. if (y > 1) neighbors.push({ x, y: y - 2 });
  56. if (y < mazeHeight - 2) neighbors.push({ x, y: y + 2 });
  57.  
  58. return neighbors.filter((neighbor) => maze[neighbor.y][neighbor.x]);
  59. }
  60.  
  61. function removeWall(cellA, cellB, maze) {
  62. const middleX = (cellA.x + cellB.x) / 2;
  63. const middleY = (cellA.y + cellB.y) / 2;
  64. maze[cellA.y][cellA.x] = false;
  65. maze[middleY][middleX] = false;
  66. maze[cellB.y][cellB.x] = false;
  67. }
  68.  
  69. function createPlayer(x, y) {
  70. const player = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
  71. player.setAttribute('cx', x * cellSize + cellSize / 2);
  72. player.setAttribute('cy', y * cellSize + cellSize / 2);
  73. player.setAttribute('r', cellSize / 2 - 2);
  74. player.setAttribute('fill', 'blue');
  75. svg.appendChild(player);
  76. return player;
  77. }
  78.  
  79. function movePlayer(dx, dy) {
  80. const playerX = parseFloat(player.getAttribute('cx'));
  81. const playerY = parseFloat(player.getAttribute('cy'));
  82. const newX = playerX + dx * cellSize;
  83. const newY = playerY + dy * cellSize;
  84.  
  85. if (newX >= 0 && newX < svg.getAttribute('width') && newY >= 0 && newY < svg.getAttribute('height')) {
  86. player.setAttribute('cx', newX);
  87. player.setAttribute('cy', newY);
  88. }
  89. }
  90.  
  91. // Handle keyboard input
  92. document.addEventListener('keydown', (event) => {
  93. if (event.key === 'ArrowLeft') {
  94. movePlayer(-1, 0);
  95. } else if (event.key === 'ArrowRight') {
  96. movePlayer(1, 0);
  97. } else if (event.key === 'ArrowUp') {
  98. movePlayer(0, -1);
  99. } else if (event.key === 'ArrowDown') {
  100. movePlayer(0, 1);
  101. }
  102. });
  103.  
  104. // Draw the maze
  105. for (let row = 0; row < mazeHeight; row++) {
  106. for (let col = 0; col < mazeWidth; col++) {
  107. if (maze[row][col]) {
  108. const wall = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  109. wall.setAttribute('x', col * cellSize);
  110. wall.setAttribute('y', row * cellSize);
  111. wall.setAttribute('width', cellSize);
  112. wall.setAttribute('height', cellSize);
  113. wall.setAttribute('fill', 'black');
  114. svg.appendChild(wall);
  115. }
  116. }
  117. }
  118. </script>
  119. </body>
  120. </html>
  121.  
Advertisement
Add Comment
Please, Sign In to add comment