Advertisement
Flild

lua code for mc

Feb 20th, 2025
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.69 KB | Gaming | 0 0
  1. -- Game settings
  2. local WIDTH, HEIGHT = 10, 8
  3. local DIAMOND_CHANCE = 0.15
  4. local LAVA_CHANCE = 0.10
  5. local GAME_SPEED = 0.5
  6.  
  7. -- Initialize game field
  8. local function generateField()
  9.     local field = {}
  10.     for y = 1, HEIGHT do
  11.         field[y] = {}
  12.         for x = 1, WIDTH do
  13.             if math.random() < DIAMOND_CHANCE then
  14.                 field[y][x] = "💎"
  15.             elseif math.random() < LAVA_CHANCE then
  16.                 field[y][x] = "🌋"
  17.             else
  18.                 field[y][x] = "·"
  19.             end
  20.         end
  21.     end
  22.     field[1][1] = "🐢"  -- Starting position
  23.     return field
  24. end
  25.  
  26. -- Draw game field
  27. local function draw(field, score, fuel)
  28.     term.clear()
  29.     print("=== Turtle Miner ===")
  30.     print("Score: "..score.." Fuel: "..fuel)
  31.    
  32.     for y = 1, HEIGHT do
  33.         local line = ""
  34.         for x = 1, WIDTH do
  35.             line = line .. field[y][x] .. " "
  36.         end
  37.         print(line)
  38.     end
  39. end
  40.  
  41. -- Main game loop
  42. local function gameLoop()
  43.     local score = 0
  44.     local posX, posY = 1, 1
  45.     local field = generateField()
  46.    
  47.     turtle.refuel(1)  -- Initial fuel
  48.    
  49.     while true do
  50.         local fuel = turtle.getFuelLevel()
  51.         field[posY][posX] = "·"
  52.        
  53.         -- Input handling
  54.         local event, key = os.pullEvent("key")
  55.         if key == keys.up and posY > 1 then
  56.             posY = posY - 1
  57.         elseif key == keys.down and posY < HEIGHT then
  58.             posY = posY + 1
  59.         elseif key == keys.left and posX > 1 then
  60.             posX = posX - 1
  61.         elseif key == keys.right and posX < WIDTH then
  62.             posX = posX + 1
  63.         elseif key == keys.space then
  64.             turtle.dig()  -- Dig block
  65.             fuel = fuel - 1
  66.         end
  67.        
  68.         -- Cell checks
  69.         if field[posY][posX] == "💎" then
  70.             score = score + 10
  71.             turtle.suck()  -- Collect diamond
  72.         elseif field[posY][posX] == "🌋" then
  73.             print("You hit lava! Game over!")
  74.             return
  75.         end
  76.        
  77.         -- Fuel management
  78.         fuel = fuel - 0.5
  79.         turtle.refuel(0)  -- Auto-refuel from inventory
  80.        
  81.         -- Update field
  82.         field[posY][posX] = "🐢"
  83.         draw(field, score, fuel)
  84.        
  85.         -- Lose conditions
  86.         if fuel <= 0 then
  87.             print("Out of fuel!")
  88.             return
  89.         end
  90.        
  91.         sleep(GAME_SPEED)
  92.     end
  93. end
  94.  
  95. -- Start game
  96. term.clear()
  97. print("Welcome to Turtle Miner!")
  98. print("Controls:")
  99. print("Arrow keys - movement")
  100. print("Space - dig block")
  101. print("Collect diamonds (💎) and avoid lava (🌋)")
  102. print("Press any key to start...")
  103. os.pullEvent("key")
  104. gameLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement