Advertisement
squidingtin

VP

Nov 13th, 2024 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.43 KB | None | 0 0
  1. -- Find the speaker peripheral
  2. local speaker = peripheral.find("speaker")
  3. if not speaker then
  4.     print("No speaker found! Attach a speaker peripheral.")
  5.     return
  6. end
  7.  
  8. -- Function to display the help menu
  9. local function showHelpMenu()
  10.     term.clear()  -- Clear the terminal for clarity
  11.     term.setTextColor(colors.white)
  12.     term.setCursorPos(1, 1)
  13.     print("Virtual Piano - Help Menu")
  14.     print("Commands:")
  15.     print("help         - Show this help menu")
  16.     print("start [instrument] - Run the piano with the given instrument")
  17.     print("help start   - Show how to start the piano with an instrument")
  18.     print("help instruments - Lists all available instruments")
  19.     print("help keys    - Lists the keys used on the piano")
  20.     print("Type 'start [instrument]' to start the piano.")
  21. end
  22.  
  23. -- Function to list available instruments
  24. local function listInstruments()
  25.     term.clear()
  26.     term.setTextColor(colors.white)
  27.     term.setCursorPos(1, 1)
  28.     print("Available Instruments:")
  29.     print("bass - Bass (String Bass)")
  30.     print("snare - Snare Drum")
  31.     print("hat - Clicks and Sticks (Hihat)")
  32.     print("basedrum - Bass Drum (Kick)")
  33.     print("bell - Bells (Glockenspiel)")
  34.     print("flute - Flute")
  35.     print("chime - Chimes")
  36.     print("guitar - Guitar")
  37.     print("xylophone - Xylophone")
  38.     print("iron_xylophone - Iron Xylophone (Vibraphone)")
  39.     print("cow_bell - Cow Bell")
  40.     print("didgeridoo - Didgeridoo")
  41.     print("bit - Bit (Square wave)")
  42.     print("banjo - Banjo")
  43.     print("pling - Pling (Electric piano)")
  44.     print("harp - Harp/Piano")
  45. end
  46.  
  47. -- Function to show the keys used
  48. local function showKeys()
  49.     term.clear()
  50.     term.setTextColor(colors.white)
  51.     term.setCursorPos(1, 1)
  52.     print("Keys Used on the Virtual Piano:")
  53.     print("A - C (Starting Note)")
  54.     print("W - C#")
  55.     print("S - D")
  56.     print("E - D#")
  57.     print("D - E")
  58.     print("F - F")
  59.     print("T - F#")
  60.     print("G - G")
  61.     print("Y - G#")
  62.     print("H - A")
  63.     print("U - A#")
  64.     print("J - B")
  65.     print("K - C (Next Octave)")
  66. end
  67.  
  68. -- Ask the player for their choice of action
  69. term.clear()
  70. term.setTextColor(colors.white)
  71. term.setCursorPos(1, 1)
  72. print("Virtual Piano - Type 'help' for instructions.")
  73.  
  74. while true do
  75.     print("\nEnter a command:")
  76.     local input = read()
  77.  
  78.     if input == "help" then
  79.         showHelpMenu()
  80.     elseif input == "help start" then
  81.         term.clear()
  82.         term.setTextColor(colors.white)
  83.         term.setCursorPos(1, 1)
  84.         print("help start:")
  85.         print("To start the piano, type 'start [instrument]'. For example:")
  86.         print("start pling")
  87.         print("This will run the piano with the 'pling' sound.")
  88.     elseif input == "help instruments" then
  89.         listInstruments()
  90.     elseif input == "help keys" then
  91.         showKeys()
  92.     elseif input:match("^start (.+)$") then
  93.         instrument = input:match("^start (.+)$")
  94.         -- Now you can set the instrument and run the piano
  95.         print("Starting the piano with the instrument: " .. instrument)
  96.         -- You can call the piano running function here, depending on how your piano setup is written
  97.         -- For example, you could call the function that starts the piano with the selected instrument:
  98.         -- runPiano(instrument)
  99.         break  -- exit the loop and start the piano
  100.     else
  101.         print("Invalid command. Type 'help' for a list of available commands.")
  102.     end
  103. end
  104.  
  105. -- Define brown color for the "piano"
  106. local pianoColor = colors.brown
  107.  
  108. -- Key-to-note mappings (for one octave, starting from C)
  109. local keyToNote = {
  110.     [keys.a] = { pitch = 12, x = 2 },  -- C
  111.     [keys.w] = { pitch = 13, x = 5 },  -- C#
  112.     [keys.s] = { pitch = 14, x = 8 },  -- D
  113.     [keys.e] = { pitch = 15, x = 11 }, -- D#
  114.     [keys.d] = { pitch = 16, x = 14 }, -- E
  115.     [keys.f] = { pitch = 17, x = 18 }, -- F
  116.     [keys.t] = { pitch = 18, x = 21 }, -- F#
  117.     [keys.g] = { pitch = 19, x = 24 }, -- G
  118.     [keys.y] = { pitch = 20, x = 27 }, -- G#
  119.     [keys.h] = { pitch = 21, x = 30 }, -- A
  120.     [keys.u] = { pitch = 22, x = 33 }, -- A#
  121.     [keys.j] = { pitch = 23, x = 36 }, -- B
  122.     [keys.k] = { pitch = 24, x = 40 }, -- C (next octave)
  123. }
  124.  
  125. -- Global key widths for consistency
  126. local whiteKeyWidth = 4
  127. local blackKeyWidth = 3
  128. local whiteKeyHeight = 10
  129. local blackKeyHeight = 6
  130.  
  131. -- Function to draw the piano layout on the term
  132. local function drawPianoKeys()
  133.     term.setBackgroundColor(pianoColor)
  134.     term.clear()  -- Ensure we clear the term before drawing new layout
  135.     paintutils.drawFilledBox(1, 1, 50, 12, pianoColor)  -- Draw the piano box
  136.     term.setCursorPos(2, 1)
  137.     term.setTextColor(colors.white)
  138.     term.write("Virtual Piano - Current key: " .. instrument)
  139.  
  140.     local whiteKeyX = 2
  141.     local blackKeyX = 0
  142.     local keyPositions = {}
  143.  
  144.     -- Draw white keys first
  145.     for i = 1, 14 do  -- 14 keys in total (7 white, 5 black)
  146.         -- Draw white key
  147.         paintutils.drawFilledBox(whiteKeyX, 4, whiteKeyX + whiteKeyWidth - 1, 4 + whiteKeyHeight - 1, colors.white)
  148.         paintutils.drawBox(whiteKeyX, 4, whiteKeyX + whiteKeyWidth - 1, 4 + whiteKeyHeight - 1, colors.gray)
  149.  
  150.         -- Store positions for white keys
  151.         table.insert(keyPositions, {x = whiteKeyX, isBlackKey = false})
  152.  
  153.         -- Move the white key X position for the next key
  154.         whiteKeyX = whiteKeyX + whiteKeyWidth
  155.     end
  156.  
  157.     -- Now draw black keys on top of the white keys
  158.     whiteKeyX = 2  -- Reset white key X position for black key calculations
  159.     for i = 1, 14 do
  160.         -- Draw black key (in between specific white keys)
  161.         if i == 1 or i == 3 or i == 4 or i == 6 or i == 8 or i == 10 or i == 11 or i == 13 then
  162.             -- Black key is positioned in the middle of the white keys (no space)
  163.             blackKeyX = whiteKeyX + whiteKeyWidth - 1  -- Black key placed right after white key
  164.             -- Draw black key
  165.             paintutils.drawFilledBox(blackKeyX, 4, blackKeyX + blackKeyWidth - 2, 4 + blackKeyHeight - 1, colors.black)
  166.  
  167.             -- Store positions for black keys
  168.             table.insert(keyPositions, {x = blackKeyX, isBlackKey = true})
  169.         end
  170.  
  171.         -- Move the white key X position for the next key
  172.         whiteKeyX = whiteKeyX + whiteKeyWidth
  173.     end
  174. end
  175.  
  176. -- Function to highlight a pressed key
  177. local function highlightKey(note, isBlackKey)
  178.     local color = isBlackKey and colors.red or colors.lightGray
  179.     if isBlackKey then
  180.         -- Black key highlight with precise integer values
  181.         paintutils.drawFilledBox(note.x, 4, note.x + blackKeyWidth - 2, 4 + blackKeyHeight - 1, color)
  182.     else
  183.         -- White key highlight with precise integer values
  184.         paintutils.drawFilledBox(note.x, 4, note.x + whiteKeyWidth - 1, 4 + whiteKeyHeight - 1, color)
  185.     end
  186. end
  187.  
  188. -- Draw initial keys once at the start
  189. drawPianoKeys()
  190.  
  191. -- Main loop to listen for keypresses and play notes
  192. while true do
  193.     local event, key = os.pullEvent("key")
  194.     local note = keyToNote[key]
  195.    
  196.  
  197.     if note then
  198.         local isBlackKey = (key == keys.w or key == keys.e or key == keys.t or key == keys.y or key == keys.u)
  199.  
  200.         -- Highlight the key on the term
  201.         highlightKey(note, isBlackKey)
  202.         speaker.playNote(instrument, 1, note.pitch)
  203.  
  204.         -- Add a small delay to make it feel responsive
  205.         sleep(0.05)
  206.  
  207.         -- Redraw the piano keys layout after highlighting the pressed key
  208.         drawPianoKeys()
  209.     end
  210. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement