Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft Maze Game

Sep 4th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. -- Maze Generation Using Depth-First Search
  2. local width, height = 20, 20
  3. local maze = {}
  4. local visited = {}
  5.  
  6. for y = 1, height do
  7. maze[y] = {}
  8. visited[y] = {}
  9. for x = 1, width do
  10. maze[y][x] = 1 -- 1 = wall
  11. visited[y][x] = false
  12. end
  13. end
  14.  
  15. local directions = {
  16. {x = 2, y = 0},
  17. {x = -2, y = 0},
  18. {x = 0, y = 2},
  19. {x = 0, y = -2}
  20. }
  21.  
  22. function generateMaze(x, y)
  23. visited[y][x] = true
  24. maze[y][x] = 0 -- 0 = path
  25.  
  26. local shuffled = {}
  27. for i = 1, #directions do
  28. shuffled[i] = directions[i]
  29. end
  30. for i = #shuffled, 2, -1 do
  31. local j = math.random(i)
  32. shuffled[i], shuffled[j] = shuffled[j], shuffled[i]
  33. end
  34.  
  35. for _, dir in ipairs(shuffled) do
  36. local nx, ny = x + dir.x, y + dir.y
  37. local mx, my = x + dir.x / 2, y + dir.y / 2
  38. if nx > 0 and ny > 0 and nx <= width and ny <= height and not visited[ny][nx] then
  39. maze[my][mx] = 0
  40. generateMaze(nx, ny)
  41. end
  42. end
  43. end
  44.  
  45. math.randomseed(os.time())
  46. generateMaze(2, 2)
  47.  
  48. -- Player and goal
  49. local playerX, playerY = 2, 2
  50. local goalX, goalY = width - 1, height - 1
  51.  
  52. -- Limited view settings
  53. local viewWidth, viewHeight = 10, 10
  54.  
  55. -- Function to draw the maze with player centered
  56. function drawMaze()
  57. term.clear()
  58.  
  59. local startX = math.max(1, playerX - math.floor(viewWidth / 2))
  60. local startY = math.max(1, playerY - math.floor(viewHeight / 2))
  61. local endX = math.min(width, playerX + math.floor(viewWidth / 2))
  62. local endY = math.min(height, playerY + math.floor(viewHeight / 2))
  63.  
  64. -- Adjust for view dimensions
  65. local viewXStart = math.max(1, playerX - math.floor(viewWidth / 2))
  66. local viewYStart = math.max(1, playerY - math.floor(viewHeight / 2))
  67.  
  68. for y = startY, endY do
  69. for x = startX, endX do
  70. local screenX = x - viewXStart + 1
  71. local screenY = y - viewYStart + 1
  72. if x == playerX and y == playerY then
  73. term.setCursorPos(screenX, screenY)
  74. term.write("P") -- Player
  75. elseif x == goalX and y == goalY then
  76. term.setCursorPos(screenX, screenY)
  77. term.write("G") -- Goal
  78. elseif maze[y][x] == 1 then
  79. term.setCursorPos(screenX, screenY)
  80. term.write("#") -- Wall
  81. else
  82. term.setCursorPos(screenX, screenY)
  83. term.write(" ") -- Path
  84. end
  85. end
  86. end
  87. end
  88.  
  89. -- Function to move the player
  90. function movePlayer(dx, dy)
  91. local newX = playerX + dx
  92. local newY = playerY + dy
  93.  
  94. if newX > 0 and newX <= width and newY > 0 and newY <= height and maze[newY][newX] == 0 then
  95. playerX = newX
  96. playerY = newY
  97. end
  98. end
  99.  
  100. -- Main game loop
  101. while true do
  102. drawMaze()
  103. local event, key = os.pullEvent("key")
  104.  
  105. if key == keys.w then
  106. movePlayer(0, -1)
  107. elseif key == keys.s then
  108. movePlayer(0, 1)
  109. elseif key == keys.a then
  110. movePlayer(-1, 0)
  111. elseif key == keys.d then
  112. movePlayer(1, 0)
  113. end
  114.  
  115. if playerX == goalX and playerY == goalY then
  116. term.clear()
  117. print("Congratulations, you won!")
  118. break
  119. end
  120. end
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement