Advertisement
JereTheJuggler

simon.lua

Nov 26th, 2021
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.67 KB | None | 0 0
  1. term.clear()
  2. local duration = .5
  3. local width,height = term.getSize()
  4. local green = window.create(term.current(),1,1,(width-1)/2,(height-1)/2)
  5. green.setBackgroundColor(colors.green)
  6. green.clear()
  7. local red = window.create(term.current(),2+(width-1)/2,1,(width-1)/2,(height-1)/2)
  8. red.setBackgroundColor(colors.red)
  9. red.clear()
  10. local yellow = window.create(term.current(),1,2+(height-1)/2,(width-1)/2,(height-1)/2)
  11. yellow.setBackgroundColor(colors.yellow)
  12. yellow.clear()
  13. local blue = window.create(term.current(),2+(width-1)/2,2+(height-1)/2,(width-1)/2,(height-1)/2)
  14. blue.setBackgroundColor(colors.blue)
  15. blue.clear()
  16. local infoPanel = window.create(term.current(),1+math.floor((width/2)-(11/2)),1+math.floor((height/2)-(7/2)),11,7)
  17. infoPanel.setBackgroundColor(colors.black)
  18. infoPanel.clear()
  19. local infoX,infoY = infoPanel.getPosition()
  20. local startButton = window.create(term.current(),infoX+3,infoY+1,5,1)
  21. startButton.setBackgroundColor(colors.red)
  22. startButton.setTextColor(colors.black)
  23. startButton.setCursorPos(1,1)
  24. startButton.write("START")
  25. local closeButton = window.create(term.current(),infoX+3,infoY+5,5,1)
  26. closeButton.setBackgroundColor(colors.red)
  27. closeButton.setTextColor(colors.black)
  28. closeButton.setCursorPos(1,1)
  29. closeButton.write("CLOSE")
  30. local scoreLabel = window.create(term.current(),infoX+1,infoY+3,9,1)
  31. local pattern = {}
  32. local score = 0
  33. local quit = false
  34. local buttons = {green,red,yellow,blue,infoPanel,startButton,closeButton}
  35. local function refreshScore ()
  36.   scoreLabel.clear()
  37.   scoreLabel.setVisible(true)
  38.   scoreLabel.setCursorPos(1+math.floor((scoreLabel.getSize()/2)-(string.len(tostring(score))/2)),1)
  39.   scoreLabel.write(tostring(score))
  40. end
  41. local function playSound (sound,freq)
  42.   local note = peripheral.find("note_block")
  43.   if note == nil then
  44.     return
  45.   end
  46.   note.playSound(sound,freq,1)
  47. end
  48. local function playBeep (freq,dur)
  49.   if dur ~= nil then
  50.     local time = 0
  51.     while time < dur do
  52.       playSound("note.harp",freq)
  53.       sleep(.1)
  54.       time = time + .1
  55.     end
  56.     return
  57.   end
  58.   parallel.waitForAny(function () while true do playSound("note.harp",freq) sleep(.1) end end,
  59.                       function () os.pullEvent("mouse_up") end)
  60.  
  61. end
  62. local function lightUp (c)
  63.   for i=1,10 do
  64.     c.setCursorPos(1,i)
  65.     c.write("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
  66.   end
  67.   infoPanel.redraw()
  68.   closeButton.redraw()
  69.   scoreLabel.redraw()
  70. end
  71. local function getClick (timeout)
  72.   local buttonClicked = nil
  73.   parallel.waitForAny(
  74.     function ()
  75.       if not timeout then
  76.         while true do
  77.           sleep(1)
  78.         end
  79.       end
  80.       sleep(5)
  81.       return
  82.     end,
  83.     function ()
  84.       while true do
  85.         local event,b,x,y = os.pullEvent("mouse_click")
  86.         for i=table.getn(buttons),1,-1 do
  87.           local xMin,yMin = buttons[i].getPosition()
  88.           local w,h = buttons[i].getSize()
  89.           local xMax = xMin + w - 1
  90.           local yMax = yMin + h - 1
  91.           if x <= xMax and y <= yMax and x >= xMin and y >= yMin then
  92.             if buttons[i] == infoPanel then
  93.               break
  94.             end
  95.             buttonClicked = buttons[i]
  96.             return
  97.           end
  98.         end
  99.       end
  100.     end
  101.   )
  102.   return buttonClicked
  103. end
  104. local function beep (c,dur)
  105.   local freq
  106.   lightUp(c)
  107.   if c == green then
  108.     freq = .9
  109.   elseif c == red then
  110.     freq = 1.32
  111.   elseif c == blue then
  112.     freq = 1.2
  113.   elseif c == yellow then
  114.     freq = 1.4
  115.   end
  116.   playBeep(freq,dur)
  117.   c.clear()
  118.   infoPanel.redraw()
  119.   startButton.redraw()
  120.   closeButton.redraw()
  121.   scoreLabel.redraw()
  122.   if dur ~= nil then
  123.     sleep(.375)
  124.   end
  125. end
  126. local function playPattern ()
  127.   table.insert(pattern,table.getn(pattern)+1,1+math.floor(math.random()*4))
  128.   for i=1,table.getn(pattern) do
  129.     beep(buttons[pattern[i]],duration)
  130.   end
  131.   return
  132. end
  133. while true do
  134.   startButton.setVisible(true)
  135.   while true do
  136.     local c = getClick(false)
  137.     if c == startButton then
  138.       pattern = {}
  139.       score = 0
  140.       refreshScore()
  141.       break
  142.     elseif c == closeButton then
  143.       quit = true
  144.       break
  145.     end
  146.   end
  147.   startButton.setVisible(false)
  148.   if quit then
  149.     break
  150.   end
  151.   local failed = false
  152.   while not failed do
  153.     playPattern()
  154.     --parallel.waitForAny(playPattern(),
  155.     --  function ()
  156.     --    while true do
  157.     --      local c = getClick(false)
  158.     --      if c == closeButton then
  159.     --        quit = true
  160.     --        return
  161.     --      end
  162.     --    end
  163.     --  end)
  164.     playSound("note.hat",1.32)
  165.     if quit then
  166.       break
  167.     end
  168.     for i=1,table.getn(pattern) do
  169.       local m
  170.       while true do
  171.         m = getClick(true)
  172.         if m == nil then
  173.           failed = true
  174.           break
  175.         elseif m == closeButton then
  176.           quit = true
  177.           break
  178.         elseif m == green or m == red or m == yellow or m == blue then
  179.           if m == buttons[pattern[i]] then
  180.             beep(m)
  181.             break
  182.           else
  183.             failed = true
  184.             break
  185.           end
  186.         end
  187.       end
  188.       if failed or quit then
  189.         break
  190.       end
  191.     end
  192.     if quit then
  193.       break
  194.     elseif failed then
  195.       lightUp(green)
  196.       lightUp(red)
  197.       lightUp(blue)
  198.       lightUp(yellow)
  199.       for p=1,15 do
  200.         playSound("note.bass",.9)
  201.         sleep(.1)
  202.       end
  203.       green.clear()
  204.       red.clear()
  205.       yellow.clear()
  206.       blue.clear()
  207.       infoPanel.redraw()
  208.       closeButton.redraw()
  209.       scoreLabel.redraw()
  210.     else
  211.       playSound("note.hat",.9)
  212.       score = score+1
  213.       refreshScore()
  214.       sleep(1.5)
  215.     end
  216.   end
  217. end
  218. term.setBackgroundColor(colors.black)
  219. term.setCursorPos(1,1)
  220. term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement