Advertisement
FreekBes

TunePlay 1.0 for ComputerCraft

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