Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Find the speaker peripheral
- local speaker = peripheral.find("speaker")
- if not speaker then
- print("No speaker found! Attach a speaker peripheral.")
- return
- end
- -- Function to display the help menu
- local function showHelpMenu()
- term.clear() -- Clear the terminal for clarity
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- print("Virtual Piano - Help Menu")
- print("Commands:")
- print("help - Show this help menu")
- print("start [instrument] - Run the piano with the given instrument")
- print("help start - Show how to start the piano with an instrument")
- print("help instruments - Lists all available instruments")
- print("help keys - Lists the keys used on the piano")
- print("Type 'start [instrument]' to start the piano.")
- end
- -- Function to list available instruments
- local function listInstruments()
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- print("Available Instruments:")
- print("bass - Bass (String Bass)")
- print("snare - Snare Drum")
- print("hat - Clicks and Sticks (Hihat)")
- print("basedrum - Bass Drum (Kick)")
- print("bell - Bells (Glockenspiel)")
- print("flute - Flute")
- print("chime - Chimes")
- print("guitar - Guitar")
- print("xylophone - Xylophone")
- print("iron_xylophone - Iron Xylophone (Vibraphone)")
- print("cow_bell - Cow Bell")
- print("didgeridoo - Didgeridoo")
- print("bit - Bit (Square wave)")
- print("banjo - Banjo")
- print("pling - Pling (Electric piano)")
- print("harp - Harp/Piano")
- end
- -- Function to show the keys used
- local function showKeys()
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- print("Keys Used on the Virtual Piano:")
- print("A - C (Starting Note)")
- print("W - C#")
- print("S - D")
- print("E - D#")
- print("D - E")
- print("F - F")
- print("T - F#")
- print("G - G")
- print("Y - G#")
- print("H - A")
- print("U - A#")
- print("J - B")
- print("K - C (Next Octave)")
- end
- -- Ask the player for their choice of action
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- print("Virtual Piano - Type 'help' for instructions.")
- while true do
- print("\nEnter a command:")
- local input = read()
- if input == "help" then
- showHelpMenu()
- elseif input == "help start" then
- term.clear()
- term.setTextColor(colors.white)
- term.setCursorPos(1, 1)
- print("help start:")
- print("To start the piano, type 'start [instrument]'. For example:")
- print("start pling")
- print("This will run the piano with the 'pling' sound.")
- elseif input == "help instruments" then
- listInstruments()
- elseif input == "help keys" then
- showKeys()
- elseif input:match("^start (.+)$") then
- instrument = input:match("^start (.+)$")
- -- Now you can set the instrument and run the piano
- print("Starting the piano with the instrument: " .. instrument)
- -- You can call the piano running function here, depending on how your piano setup is written
- -- For example, you could call the function that starts the piano with the selected instrument:
- -- runPiano(instrument)
- break -- exit the loop and start the piano
- else
- print("Invalid command. Type 'help' for a list of available commands.")
- end
- end
- -- Define brown color for the "piano"
- local pianoColor = colors.brown
- -- Key-to-note mappings (for one octave, starting from C)
- local keyToNote = {
- [keys.a] = { pitch = 12, x = 2 }, -- C
- [keys.w] = { pitch = 13, x = 5 }, -- C#
- [keys.s] = { pitch = 14, x = 8 }, -- D
- [keys.e] = { pitch = 15, x = 11 }, -- D#
- [keys.d] = { pitch = 16, x = 14 }, -- E
- [keys.f] = { pitch = 17, x = 18 }, -- F
- [keys.t] = { pitch = 18, x = 21 }, -- F#
- [keys.g] = { pitch = 19, x = 24 }, -- G
- [keys.y] = { pitch = 20, x = 27 }, -- G#
- [keys.h] = { pitch = 21, x = 30 }, -- A
- [keys.u] = { pitch = 22, x = 33 }, -- A#
- [keys.j] = { pitch = 23, x = 36 }, -- B
- [keys.k] = { pitch = 24, x = 40 }, -- C (next octave)
- }
- -- Global key widths for consistency
- local whiteKeyWidth = 4
- local blackKeyWidth = 3
- local whiteKeyHeight = 10
- local blackKeyHeight = 6
- -- Function to draw the piano layout on the term
- local function drawPianoKeys()
- term.setBackgroundColor(pianoColor)
- term.clear() -- Ensure we clear the term before drawing new layout
- paintutils.drawFilledBox(1, 1, 50, 12, pianoColor) -- Draw the piano box
- term.setCursorPos(2, 1)
- term.setTextColor(colors.white)
- term.write("Virtual Piano - Current key: " .. instrument)
- local whiteKeyX = 2
- local blackKeyX = 0
- local keyPositions = {}
- -- Draw white keys first
- for i = 1, 14 do -- 14 keys in total (7 white, 5 black)
- -- Draw white key
- paintutils.drawFilledBox(whiteKeyX, 4, whiteKeyX + whiteKeyWidth - 1, 4 + whiteKeyHeight - 1, colors.white)
- paintutils.drawBox(whiteKeyX, 4, whiteKeyX + whiteKeyWidth - 1, 4 + whiteKeyHeight - 1, colors.gray)
- -- Store positions for white keys
- table.insert(keyPositions, {x = whiteKeyX, isBlackKey = false})
- -- Move the white key X position for the next key
- whiteKeyX = whiteKeyX + whiteKeyWidth
- end
- -- Now draw black keys on top of the white keys
- whiteKeyX = 2 -- Reset white key X position for black key calculations
- for i = 1, 14 do
- -- Draw black key (in between specific white keys)
- 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
- -- Black key is positioned in the middle of the white keys (no space)
- blackKeyX = whiteKeyX + whiteKeyWidth - 1 -- Black key placed right after white key
- -- Draw black key
- paintutils.drawFilledBox(blackKeyX, 4, blackKeyX + blackKeyWidth - 2, 4 + blackKeyHeight - 1, colors.black)
- -- Store positions for black keys
- table.insert(keyPositions, {x = blackKeyX, isBlackKey = true})
- end
- -- Move the white key X position for the next key
- whiteKeyX = whiteKeyX + whiteKeyWidth
- end
- end
- -- Function to highlight a pressed key
- local function highlightKey(note, isBlackKey)
- local color = isBlackKey and colors.red or colors.lightGray
- if isBlackKey then
- -- Black key highlight with precise integer values
- paintutils.drawFilledBox(note.x, 4, note.x + blackKeyWidth - 2, 4 + blackKeyHeight - 1, color)
- else
- -- White key highlight with precise integer values
- paintutils.drawFilledBox(note.x, 4, note.x + whiteKeyWidth - 1, 4 + whiteKeyHeight - 1, color)
- end
- end
- -- Draw initial keys once at the start
- drawPianoKeys()
- -- Main loop to listen for keypresses and play notes
- while true do
- local event, key = os.pullEvent("key")
- local note = keyToNote[key]
- if note then
- local isBlackKey = (key == keys.w or key == keys.e or key == keys.t or key == keys.y or key == keys.u)
- -- Highlight the key on the term
- highlightKey(note, isBlackKey)
- speaker.playNote(instrument, 1, note.pitch)
- -- Add a small delay to make it feel responsive
- sleep(0.05)
- -- Redraw the piano keys layout after highlighting the pressed key
- drawPianoKeys()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement