Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[ Startup file for the computer,
- sends signal to turtle to receive
- table of names of the discs,
- then creates a menu of which to play
- ]]
- --[[ Variables ]]--
- rednet.open("left")
- turtleID = 4
- t_songs = {}
- sX, sY = term.getSize()
- running = true
- -- Tables
- t_songPlaying = {"Stop", "Replay"}
- local function colorW(...)
- local curColor
- for i=1, #arg do -- arg is ...
- if type(arg[i]) == 'number' then
- curColor = arg[i]
- else
- if curColor then
- term.setTextColor(curColor)
- end
- write(arg[i])
- end
- end
- end
- local function menu(_table)
- local sel = 1
- while true do
- tempSpace = 0
- yPos = 7
- if sel > #_table then sel = 1 end
- if sel < 1 then sel = #_table end
- for i = 1,#_table do
- if i == 10 then tempSpace = 23 yPos = 7 end
- term.setCursorPos(3 + tempSpace, yPos)
- yPos = yPos + 1
- if sel == i then
- colorW(colours.red, "[ ", colours.lightBlue, _table[i], colours.red, " ]")
- else
- colorW(" ", colours.lightBlue, _table[i], " ")
- end
- end
- local e, key = os.pullEvent("key")
- if key == 200 then -- up key
- sel = sel-1
- elseif key == 208 then -- down key
- sel = sel+1
- elseif key == 28 then
- return _table[sel], sel
- end
- end
- end
- function cWrite(text, x, y, colour)
- colour = colour or colours.black
- term.setTextColour(colour)
- term.setCursorPos(x, y)
- write(text)
- end
- function drawBackground()
- -- Print background
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.white)
- term.clear()
- term.setCursorPos(1,1)
- bg = paintutils.loadImage("background")
- paintutils.drawImage(bg, 1, 1)
- t = "Music disk player"
- term.setCursorPos( (sX - #t)/2, 2)
- term.setTextColour(colours.lime)
- term.setBackgroundColour(colours.cyan)
- write(t)
- term.setBackgroundColour(colours.grey)
- end
- function songPlaying(songName)
- drawBackground()
- text = "Playing " .. songName
- cWrite(text, (sX-#text)/2, 5, colours.red)
- nSelected = 1
- while true do
- for i = 1, #t_songPlaying do
- term.setCursorPos(i*15, 7)
- if nSelected == i then
- colorW(colours.red, "[", colours.lime, t_songPlaying[i], colours.red, "]")
- else
- colorW(" ", colours.lime, t_songPlaying[i], " ")
- end
- end
- e, key = os.pullEvent("key")
- if key == keys.left and nSelected > 1 then nSelected = nSelected - 1
- elseif key == keys.right and nSelected < #t_songPlaying then nSelected = nSelected + 1
- elseif key == keys.enter then break end
- end
- if nSelected == 1 then
- -- Code to stop playing the song
- rednet.send(turtleID, "stop")
- drawBackground()
- text = "Last played song: " .. songName
- cWrite(text, (sX - #text)/2, 5, colours.red)
- elseif nSelected == 2 then
- -- Code to restart the disk
- rednet.send(turtleID, "restart")
- sleep(1)
- return songPlaying(songName)
- end
- end
- function getDiskTitles()
- drawBackground()
- t_songs = {} -- reset the table of songs
- term.setBackgroundColour(colours.grey)
- cWrite("Getting information from turtle...", 3, 5, colours.black)
- rednet.send(turtleID, "getDiskTitles")
- repeat id, message = rednet.receive() until id == turtleID
- if message == "no discs" then
- cWrite("No disks in turtle. ", 3, 5, colours.black)
- term.setCursorPos(3, 6)
- colorW(colours.lightBlue, "[Try again]")
- repeat e, k = os.pullEvent() until k == keys.enter
- return getDiskTitles()
- else
- t_songs = textutils.unserialize(message)
- table.insert(t_songs, "Exit")
- table.insert(t_songs, "Get Titles")
- end
- drawBackground()
- end
- drawBackground()
- getDiskTitles()
- while running do
- term.setBackgroundColour(colours.grey)
- songName, songNumber = menu(t_songs)
- if songName == "Exit" then
- term.setBackgroundColour(colours.black)
- term.setTextColour(colours.white)
- term.setCursorPos(1, 1)
- term.clear()
- running = false
- elseif songName == "Get Titles" then
- getDiskTitles()
- else
- drawBackground()
- cWrite("Attempting to play " .. songName, 3, 5, colours.red)
- rednet.send(turtleID, songName)
- repeat id, message = rednet.receive() until id == turtleID
- if message == "songFound" then
- songPlaying(songName)
- elseif message == "songNotFound" then
- drawBackground()
- text = "Unable to find " .. songName
- cWrite(text, (sX-#text)/2, 5, colours.red)
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment