Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft Limbo Keys

Aug 18th, 2024 (edited)
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. -- limboKeys.lua
  2.  
  3. -- Key positions on the screen
  4. local keyPositions = {
  5.     {x = 5, y = 5},
  6.     {x = 15, y = 5},
  7.     {x = 25, y = 5},
  8.     {x = 35, y = 5},
  9.     {x = 45, y = 5},
  10.     {x = 5, y = 15},
  11.     {x = 15, y = 15},
  12.     {x = 25, y = 15},
  13. }
  14.  
  15. -- Function to draw a key at a given position with a specified color
  16. local function drawKey(pos, color)
  17.     term.setCursorPos(pos.x, pos.y)
  18.     term.setBackgroundColor(color)
  19.     term.write("    ")  -- Draw a 4x1 block
  20.     term.setBackgroundColor(colors.black)
  21. end
  22.  
  23. -- Function to initialize keys as red
  24. local function initializeKeys()
  25.     term.clear()
  26.     for _, pos in ipairs(keyPositions) do
  27.         drawKey(pos, colors.red)
  28.     end
  29. end
  30.  
  31. -- Function to randomly shuffle the keys
  32. local function shuffleKeys()
  33.     local shuffledPositions = {}
  34.     for _, pos in ipairs(keyPositions) do
  35.         table.insert(shuffledPositions, pos)
  36.     end
  37.     -- Shuffle positions
  38.     for i = #shuffledPositions, 2, -1 do
  39.         local j = math.random(i)
  40.         shuffledPositions[i], shuffledPositions[j] = shuffledPositions[j], shuffledPositions[i]
  41.     end
  42.     return shuffledPositions
  43. end
  44.  
  45. -- Function to randomly assign colors to keys
  46. local function assignColors()
  47.     local colorsList = {colors.red, colors.green, colors.blue, colors.yellow, colors.purple, colors.orange, colors.cyan}
  48.     local keyColors = {}
  49.     for _, pos in ipairs(keyPositions) do
  50.         keyColors[pos] = colorsList[math.random(#colorsList)]
  51.     end
  52.     return keyColors
  53. end
  54.  
  55. -- Function to animate key scrambling
  56. local function animateScrambling()
  57.     local numFrames = 10
  58.     for frame = 1, numFrames do
  59.         initializeKeys()
  60.         local shuffledPositions = shuffleKeys()
  61.         local keyColors = assignColors()
  62.  
  63.         -- Draw keys in new positions
  64.         for i, pos in ipairs(shuffledPositions) do
  65.             drawKey(pos, keyColors[pos])
  66.         end
  67.  
  68.         -- Delay to create animation effect
  69.         os.sleep(0.1)
  70.     end
  71. end
  72.  
  73. -- Main function
  74. local function main()
  75.     math.randomseed(os.time())
  76.     initializeKeys()
  77.  
  78.     -- Randomly select the key to turn green
  79.     local greenKeyIndex = math.random(#keyPositions)
  80.     local greenKey = keyPositions[greenKeyIndex]
  81.  
  82.     -- Flash green for 1 second
  83.     drawKey(greenKey, colors.green)
  84.     os.sleep(1)
  85.  
  86.     -- Clear the green key and animate the scrambling
  87.     initializeKeys()
  88.     animateScrambling()
  89.  
  90.     -- Prompt user to press the key that was green
  91.     term.setCursorPos(1, 20)
  92.     term.write("Press the key that was green!")
  93.  
  94.     -- Wait for user input
  95.     local event, key = os.pullEvent("key")
  96.     -- Check if the pressed key matches the green key position
  97.     local pressedPos = nil
  98.     for _, pos in ipairs(keyPositions) do
  99.         if term.getCursorPos() == pos then
  100.             pressedPos = pos
  101.             break
  102.         end
  103.     end
  104.  
  105.     if pressedPos == greenKey then
  106.         term.setCursorPos(1, 21)
  107.         term.write("Correct key!")
  108.     else
  109.         term.setCursorPos(1, 21)
  110.         term.write("Wrong key!")
  111.     end
  112. end
  113.  
  114. main()
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement