Advertisement
Guest User

TunePlay 1.0 for ComputerCraft

a guest
Feb 1st, 2024
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.95 KB | Source Code | 0 0
  1. -- (C) 2023 Freekeh99, All Rights Reserved.
  2.  
  3. -- Text functions
  4. local function centerText(text, y)
  5.   local x = (term.getSize() - string.len(text)) / 2
  6.   term.setCursorPos(x, y)
  7.   term.write(text)
  8. end
  9.  
  10. local function optionsText(key, text, y)
  11.   local x = 3
  12.   term.setCursorPos(x, y)
  13.   term.write("[" .. key .. "] " .. text)
  14. end
  15.  
  16. -- Draw the main menu.
  17. -- It displays the program name and waits for the user to press Enter.
  18. local function drawMenu()
  19.   term.clear()
  20.   term.setCursorPos(1, 1)
  21.   centerText("Welcome to TunePlay", 1)
  22.   centerText("Press Enter to view options", 3)
  23.   centerText("Version 1.0", 6)
  24. end
  25.  
  26. -- Draw the options menu on Enter press.
  27. local function drawOptionsMenu()
  28.   term.clear()
  29.   term.setCursorPos(1, 1)
  30.   centerText("TunePlay Options", 1)
  31.   optionsText("1", "Choose a song", 3)
  32.   optionsText("2", "Start playback", 4)
  33.   optionsText("3", "Stop playback", 5)
  34.   optionsText("4", "Back to main menu", 6)
  35.   optionsText("5", "Exit", 7)
  36. end
  37.  
  38. -- Array of all the songs available.
  39. songs = {
  40.     "music.creative",
  41.     "music.credits",
  42.     "music.dragon",
  43.     "music.end",
  44.     "music.game",
  45.     "music.menu",
  46.     "music.nether.basalt_deltas",
  47.     "music.nether.crimson_forest",
  48.     "music.nether.nether_wastes",
  49.     "music.nether.soul_sand_valley",
  50.     "music.nether.warped_forest",
  51.     "music.overworld.deep_dark",
  52.     "music.overworld.dripstone_caves",
  53.     "music.overworld.frozen_peaks",
  54.     "music.overworld.grove",
  55.     "music.overworld.jagged_peaks",
  56.     "music.overworld.lush_caves",
  57.     "music.overworld.meadow",
  58.     "music.overworld.old_growth_taiga",
  59.     "music.overworld.snowy_slopes",
  60.     "music.overworld.stony_peaks",
  61.     "music.overworld.swamp",
  62.     "music.under_water",
  63.     "music_disc.11",
  64.     "music_disc.13",
  65.     "music_disc.5",
  66.     "music_disc.blocks",
  67.     "music_disc.cat",
  68.     "music_disc.chirp",
  69.     "music_disc.far",
  70.     "music_disc.mall",
  71.     "music_disc.mellohi",
  72.     "music_disc.otherside",
  73.     "music_disc.pigstep",
  74.     "music_disc.stal",
  75.     "music_disc.strad",
  76.     "music_disc.wait",
  77.     "music_disc.ward"
  78. }
  79.  
  80. song_titles = {
  81.     "Shuffle creative music",
  82.     "C418 - Alpha",
  83.     "C418 - Dragon Fight",
  84.     "C418 - The End",
  85.     "Shuffle survival music",
  86.     "Shuffle menu music",
  87.     "Shuffle Nether Basalt Deltas music",
  88.     "Shuffle Nether Crimson Forest music",
  89.     "Shuffle Nether Nether Wastes music",
  90.     "Shuffle Nether Soul Sand Valley music",
  91.     "Shuffle Nether Warped Forest music",
  92.     "Shuffle underwater music",
  93.     "Shuffle Deep Dark music",
  94.     "Shuffle Dripstone Caves music",
  95.     "Shuffle Frozen Peaks music",
  96.     "Shuffle Grove music",
  97.     "Shuffle Jagged Peaks music",
  98.     "Shuffle Lush Caves music",
  99.     "Shuffle Meadow music",
  100.     "Shuffle Old Growth Taiga music",
  101.     "Shuffle Snowy Slopes music",
  102.     "Shuffle Stony Peaks music",
  103.     "Shuffle Swamp music",
  104.     "C418 - 11",
  105.     "C418 - 13",
  106.     "Samuel Aberg - 5",
  107.     "C418 - Blocks",
  108.     "C418 - Cat",
  109.     "C418 - Chirp",
  110.     "C418 - Far",
  111.     "C418 - Mall",
  112.     "C418 - Mellohi",
  113.     "Lena Raine - Otherside",
  114.     "Lena Raine - Pigstep",
  115.     "C418 - Stal",
  116.     "C418 - Strad",
  117.     "C418 - Wait",
  118.     "C418 - Ward"
  119. }
  120.  
  121. -- Draw music list, paginated
  122. local function drawMusicList(page)
  123.   term.clear()
  124.   term.setCursorPos(1, 1)
  125.   centerText("Choose a song", 1)
  126.   local i = 1
  127.   local y = 3
  128.   local max = 10
  129.   local start = (page - 1) * max + 1
  130.   local stop = page * max
  131.   for i = start, stop do
  132.     if i > #songs then
  133.       break
  134.     end
  135.     optionsText(i, song_titles[i], y)
  136.     y = y + 1
  137.   end
  138.   if page > 1 then
  139.     optionsText("p", "Previous page", y)
  140.   end
  141.   if stop < #songs then
  142.     optionsText("n", "Next page", y + 1)
  143.   end
  144.   optionsText("b", "Back to options", y + 2)
  145. end
  146.  
  147. -- Play a song
  148. local function playSong(song)
  149.   term.clear()
  150.   term.setCursorPos(1, 1)
  151.   centerText("Now playing", 1)
  152.   centerText(song_titles[song], 3)
  153.   term.setCursorPos(1, 5)
  154.   term.write("Press Enter to stop playback")
  155.   local handle = peripheral.find("speaker")
  156.   handle.playDisk(songs[song])
  157.   while true do
  158.     local event, key = os.pullEvent("key")
  159.     if key == keys.enter then
  160.       handle.stop()
  161.       break
  162.     end
  163.   end
  164. end
  165.  
  166. -- Main loop
  167. local function main()
  168.   drawMenu()
  169.   while true do
  170.     local event, key = os.pullEvent("key")
  171.     if key == keys.enter then
  172.       drawOptionsMenu()
  173.       while true do
  174.         local event, key = os.pullEvent("key")
  175.         if key == keys["1"] then
  176.           local page = 1
  177.           while true do
  178.             drawMusicList(page)
  179.             local event, key = os.pullEvent("key")
  180.             if key == keys["p"] then
  181.               if page > 1 then
  182.                 page = page - 1
  183.               end
  184.             elseif key == keys["n"] then
  185.               if page < math.ceil(#songs / 10) then
  186.                 page = page + 1
  187.               end
  188.             elseif key == keys["b"] then
  189.               break
  190.             else
  191.               if key >= 1 and key <= #songs then
  192.                 playSong(key)
  193.               end
  194.             end
  195.           end
  196.         elseif key == keys["2"] then
  197.           local handle = peripheral.find("speaker")
  198.           handle.playDisk("music.creative")
  199.         elseif key == keys["3"] then
  200.           local handle = peripheral.find("speaker")
  201.           handle.stop()
  202.         elseif key == keys["4"] then
  203.           drawMenu()
  204.           break
  205.         elseif key == keys["5"] then
  206.           return
  207.         end
  208.       end
  209.     end
  210.   end
  211. end
  212.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement