fbMarcel

piano

Feb 27th, 2021 (edited)
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.21 KB | None | 0 0
  1. --[[
  2. Monitor: 5x3 blocks
  3. programmed in:
  4.   CC:T for Fabric (ComputerCraft: Tweaked) v1.91.2
  5.   Minecraft 1.16.4
  6.   fabric 0.10.8
  7. ]]
  8. -- variables
  9. local instnr = 14
  10. local notation = true
  11. local volume = tonumber(1)
  12. local s = peripheral.find("speaker")
  13. local instruments = {
  14. "bass",
  15. "snare",
  16. "hat",
  17. "bell",
  18. "flute",
  19. "chime",
  20. "guitar",
  21. "xylophone",
  22. "iron_xylophone",
  23. "cow_bell",
  24. "didgeridoo",
  25. "bit",
  26. "banjo",
  27. "pling",
  28. "harp"
  29. }
  30. local instname = {
  31. "Bass",
  32. "Snare",
  33. "Hat",
  34. "Bell",
  35. "Flute",
  36. "Chime",
  37. "Guitar",
  38. "Xylophone",
  39. "Iron Xylophone",
  40. "Cow Bell",
  41. "Didgeridoo",
  42. "Bit",
  43. "Banjo",
  44. "Piano",
  45. "Harp",
  46. }
  47. -- basic functions
  48. function toint(n)
  49.   local s = tostring(n)
  50.   local i, j = s:find('%.')
  51.   if i then
  52.     return tonumber(s:sub(1,i-1))
  53.   else
  54.     return n
  55.   end
  56. end
  57.  
  58. -- draw keyboard keys
  59. function drawkeys()
  60.   term.setBackgroundColor(colors.lightBlue)
  61.   paintutils.drawFilledBox(3,7,48,17)
  62.   term.setBackgroundColor(colors.white)
  63.   paintutils.drawFilledBox(5,12,46,16)
  64.   term.setBackgroundColor(colors.lightGray)
  65.   paintutils.drawFilledBox(8,12,10,16)
  66.   paintutils.drawFilledBox(14,12,16,16)
  67.   paintutils.drawFilledBox(20,12,22,16)
  68.   paintutils.drawFilledBox(26,12,28,16)
  69.   paintutils.drawFilledBox(32,12,34,16)
  70.   paintutils.drawFilledBox(38,12,40,16)
  71.   paintutils.drawFilledBox(44,12,46,16)
  72.   term.setBackgroundColor(colors.black)
  73.   paintutils.drawFilledBox(4,8,5,11)
  74.   paintutils.drawFilledBox(7,8,8,11)
  75.   paintutils.drawFilledBox(10,8,11,11)
  76.   paintutils.drawFilledBox(16,8,17,11)
  77.   paintutils.drawFilledBox(19,8,20,11)
  78.   paintutils.drawFilledBox(25,8,26,11)
  79.   paintutils.drawFilledBox(28,8,29,11)
  80.   paintutils.drawFilledBox(31,8,32,11)
  81.   paintutils.drawFilledBox(37,8,38,11)
  82.   paintutils.drawFilledBox(40,8,41,11)
  83.   paintutils.drawFilledBox(46,8,47,11)
  84. end
  85.  
  86. -- draw letters on keyboard
  87. function drawletters()
  88.   term.setBackgroundColor(colors.black)
  89.   term.setTextColor(colors.white)
  90.   term.setCursorPos(4,9)
  91.   write("0")
  92.   term.setCursorPos(4,11)
  93.   write("F#")
  94.   term.setCursorPos(7,9)
  95.   write("2")
  96.   term.setCursorPos(7,11)
  97.   write("G#")
  98.   term.setCursorPos(10,9)
  99.   write("4")
  100.   term.setCursorPos(10,11)
  101.   write("A#")
  102.   term.setCursorPos(16,9)
  103.   write("7")
  104.   term.setCursorPos(16,11)
  105.   write("C#")
  106.   term.setCursorPos(19,9)
  107.   write("9")
  108.   term.setCursorPos(19,11)
  109.   write("D#")
  110.   term.setCursorPos(25,9)
  111.   write("12")
  112.   term.setCursorPos(25,11)
  113.   write("F#")
  114.   term.setCursorPos(28,9)
  115.   write("14")
  116.   term.setCursorPos(28,11)
  117.   write("G#")
  118.   term.setCursorPos(31,9)
  119.   write("16")
  120.   term.setCursorPos(31,11)
  121.   write("A#")
  122.   term.setCursorPos(37,9)
  123.   write("19")
  124.   term.setCursorPos(37,11)
  125.   write("C#")
  126.   term.setCursorPos(40,9)
  127.   write("21")
  128.   term.setCursorPos(40,11)
  129.   write("D#")
  130.   term.setCursorPos(46,9)
  131.   write("24")
  132.   term.setCursorPos(46,11)
  133.   write("F#")
  134.   term.setBackgroundColor(colors.white)
  135.   term.setTextColor(colors.black)
  136.   term.setCursorPos(6,13)
  137.   write("1")
  138.   term.setCursorPos(6,15)
  139.   write("G")
  140.   term.setCursorPos(12,13)
  141.   write("5")
  142.   term.setCursorPos(12,15)
  143.   write("B")
  144.   term.setCursorPos(18,13)
  145.   write("8")
  146.   term.setCursorPos(18,15)
  147.   write("D")
  148.   term.setCursorPos(23,13)
  149.   write("11")
  150.   term.setCursorPos(24,15)
  151.   write("F")
  152.   term.setCursorPos(29,13)
  153.   write("15")
  154.   term.setCursorPos(30,15)
  155.   write("A")
  156.   term.setCursorPos(35,13)
  157.   write("18")
  158.   term.setCursorPos(36,15)
  159.   write("C")
  160.   term.setCursorPos(41,13)
  161.   write("22")
  162.   term.setCursorPos(42,15)
  163.   write("E")
  164.   term.setBackgroundColor(colors.lightGray)
  165.   term.setTextColor(colors.black)
  166.   term.setCursorPos(9,13)
  167.   write("3")
  168.   term.setCursorPos(9,15)
  169.   write("A")
  170.   term.setCursorPos(15,13)
  171.   write("6")
  172.   term.setCursorPos(15,15)
  173.   write("C")
  174.   term.setCursorPos(20,13)
  175.   write("10")
  176.   term.setCursorPos(21,15)
  177.   write("E")
  178.   term.setCursorPos(26,13)
  179.   write("13")
  180.   term.setCursorPos(27,15)
  181.   write("G")
  182.   term.setCursorPos(32,13)
  183.   write("17")
  184.   term.setCursorPos(33,15)
  185.   write("B")
  186.   term.setCursorPos(38,13)
  187.   write("20")
  188.   term.setCursorPos(39,15)
  189.   write("D")
  190.   term.setCursorPos(44,13)
  191.   write("23")
  192.   term.setCursorPos(45,15)
  193.   write("F")
  194. end
  195.  
  196. -- Main Screen Draw Function
  197. function mainscreen()
  198. -- draw background
  199. term.setBackgroundColor(colors.cyan)
  200. term.clear()
  201. -- draw keyboard keys
  202. drawkeys()
  203. -- draw letters on keyboards
  204. drawletters()
  205. -- draw volume
  206. term.setBackgroundColor(colors.purple)
  207. term.setTextColor(colors.white)
  208. paintutils.drawFilledBox(3,2,5,3)
  209. paintutils.drawFilledBox(15,2,17,3)
  210. term.setCursorPos(4,3)
  211. write("-")
  212. term.setCursorPos(16,3)
  213. write("+")
  214. term.setBackgroundColor(colors.cyan)
  215. term.setCursorPos(7,2)
  216. write("Volume:")
  217. term.setCursorPos(9,3)
  218. write("100")
  219. -- draw notation
  220. term.setBackgroundColor(colors.lightBlue)
  221. term.setTextColor(colors.black)
  222. paintutils.drawFilledBox(20,2,27,3)
  223. term.setCursorPos(20,2)
  224. write("Notation")
  225. term.setTextColor(colors.green)
  226. term.setCursorPos(22,3)
  227. write("ON")
  228. -- draw close button
  229. term.setBackgroundColor(colors.black)
  230. term.setTextColor(colors.red)
  231. term.setCursorPos(50,1)
  232. write("X")
  233. -- draw instruments
  234. term.setBackgroundColor(colors.red)
  235. term.setTextColor(colors.white)
  236. paintutils.drawFilledBox(1,17,2,18)
  237. paintutils.drawFilledBox(49,17,50,18)
  238. term.setCursorPos(1,18)
  239. write("<")
  240. term.setCursorPos(50,18)
  241. write(">")
  242. term.setBackgroundColor(colors.cyan)
  243. term.setCursorPos(13,18)
  244. write("Instrument: ")
  245. write(instname[instnr])
  246. -- draw logo
  247. term.setBackgroundColor(colors.orange)
  248. term.setTextColor(colors.black) --start
  249. paintutils.drawLine(31,1,31,6)
  250. paintutils.drawPixel(32,1)
  251. paintutils.drawPixel(32,3)
  252. paintutils.drawPixel(33,2) --P
  253. paintutils.drawPixel(35,1)
  254. paintutils.drawLine(35,3,35,6) --i
  255. paintutils.drawLine(37,4,37,5)
  256. paintutils.drawLine(39,3,39,6)
  257. paintutils.drawPixel(38,3)
  258. paintutils.drawPixel(38,6) --a
  259. paintutils.drawLine(41,3,41,6)
  260. paintutils.drawPixel(42,3)
  261. paintutils.drawLine(43,4,43,6) --n
  262. paintutils.drawLine(45,4,45,5)
  263. paintutils.drawPixel(46,3)
  264. paintutils.drawPixel(46,6)
  265. paintutils.drawLine(47,4,47,5) --o
  266. -- end
  267. end
  268.  
  269. -- main program
  270. mainscreen()
  271.  
  272. -- touch input
  273. while true do
  274.   local event, par, x, y = os.pullEvent()
  275.   if event == "mouse_click" then
  276.     if y>=8 and y<=11 then
  277.       -- black keys
  278.       if x==4 or x==5 then
  279.         s.playNote(instruments[instnr],volume,0)
  280.       elseif x==7 or x==8 then
  281.         s.playNote(instruments[instnr],volume,2)
  282.       elseif x==10 or x==11 then
  283.         s.playNote(instruments[instnr],volume,4)
  284.       elseif x==16 or x==17 then
  285.         s.playNote(instruments[instnr],volume,7)
  286.       elseif x==19 or x==20 then
  287.         s.playNote(instruments[instnr],volume,9)
  288.       elseif x==25 or x==26 then
  289.         s.playNote(instruments[instnr],volume,12)
  290.       elseif x==28 or x==29 then
  291.         s.playNote(instruments[instnr],volume,14)
  292.       elseif x==31 or x==32 then
  293.         s.playNote(instruments[instnr],volume,16)
  294.       elseif x==37 or x==38 then
  295.         s.playNote(instruments[instnr],volume,19)
  296.       elseif x==40 or x==41 then
  297.         s.playNote(instruments[instnr],volume,21)
  298.       elseif x==46 or x==47 then
  299.         s.playNote(instruments[instnr],volume,24)
  300.       end
  301.     elseif y>=12 and y<=16 then
  302.       -- white keys
  303.       if x>=5 and x<=7 then
  304.         s.playNote(instruments[instnr],volume,1)
  305.       elseif x>=8 and x<=10 then
  306.         s.playNote(instruments[instnr],volume,3)
  307.       elseif x>=11 and x<=13 then
  308.         s.playNote(instruments[instnr],volume,5)
  309.       elseif x>=14 and x<=16 then
  310.         s.playNote(instruments[instnr],volume,6)
  311.       elseif x>=17 and x<=19 then
  312.         s.playNote(instruments[instnr],volume,8)
  313.       elseif x>=20 and x<=22 then
  314.         s.playNote(instruments[instnr],volume,10)
  315.       elseif x>=23 and x<=25 then
  316.         s.playNote(instruments[instnr],volume,11)
  317.       elseif x>=26 and x<=28 then
  318.         s.playNote(instruments[instnr],volume,13)
  319.       elseif x>=29 and x<=31 then
  320.         s.playNote(instruments[instnr],volume,15)
  321.       elseif x>=32 and x<=34 then
  322.         s.playNote(instruments[instnr],volume,17)
  323.       elseif x>=35 and x<=37 then
  324.         s.playNote(instruments[instnr],volume,18)
  325.       elseif x>=38 and x<=40 then
  326.         s.playNote(instruments[instnr],volume,20)
  327.       elseif x>=41 and x<=43 then
  328.         s.playNote(instruments[instnr],volume,22)
  329.       elseif x>=44 and x<=46 then
  330.         s.playNote(instruments[instnr],volume,23)
  331.       end
  332.     elseif y==17 or y==18 then
  333.       if x>=1 and x<=2 then
  334.         if instnr == 1 then
  335.           instnr = 15
  336.         else
  337.           instnr = instnr - 1
  338.         end
  339.       elseif x>=49 and x<=50 then
  340.         if instnr == 15 then
  341.           instnr = 1
  342.         else
  343.           instnr = instnr + 1
  344.         end
  345.       end
  346.       term.setBackgroundColor(colors.cyan)
  347.       term.setTextColor(colors.white)
  348.       term.setCursorPos(25,18)
  349.       write("                ")
  350.       term.setCursorPos(25,18)
  351.       write(instname[instnr])
  352.     elseif y==2 or y==3 then
  353.       local suc = false
  354.       if x>=3 and x<=5 then
  355.         if volume >= 0.2 then
  356.           volume = volume - 0.1
  357.         end
  358.         suc = true
  359.       elseif x>=15 and x<=17 then
  360.         if volume <= 0.9 then
  361.           volume = volume + 0.1
  362.         end
  363.         suc = true
  364.       end
  365.       if suc then
  366.         term.setBackgroundColor(colors.cyan)
  367.         term.setTextColor(colors.white)
  368.         term.setCursorPos(9,3)
  369.         write("   ")
  370.         term.setCursorPos(9,3)
  371.         local vol = toint(volume*100)
  372.         write(vol)
  373.         local vol2 = toint(volume*10)
  374.         s.playNote(instruments[instnr],volume,vol2)
  375.       end
  376.       if x>=20 and x<=27 then
  377.         if notation then
  378.           notation = false
  379.           term.setBackgroundColor(colors.lightBlue)
  380.           term.setTextColor(colors.red)
  381.           term.setCursorPos(22,3)
  382.           write("OFF")
  383.           drawkeys()
  384.         else
  385.           notation = true
  386.           term.setBackgroundColor(colors.lightBlue)
  387.           term.setTextColor(colors.green)
  388.           term.setCursorPos(22,3)
  389.           write("ON ")
  390.           drawletters()
  391.         end
  392.       end
  393.     elseif x==50 and y==1 then
  394.       term.setBackgroundColor(colors.black)
  395.       term.setTextColor(colors.white)
  396.       term.clear()
  397.       term.setCursorPos(1,1)
  398.       break
  399.     end
  400.   end
  401. end
Add Comment
Please, Sign In to add comment