Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- term.clear()
- local duration = .5
- local width,height = term.getSize()
- local green = window.create(term.current(),1,1,(width-1)/2,(height-1)/2)
- green.setBackgroundColor(colors.green)
- green.clear()
- local red = window.create(term.current(),2+(width-1)/2,1,(width-1)/2,(height-1)/2)
- red.setBackgroundColor(colors.red)
- red.clear()
- local yellow = window.create(term.current(),1,2+(height-1)/2,(width-1)/2,(height-1)/2)
- yellow.setBackgroundColor(colors.yellow)
- yellow.clear()
- local blue = window.create(term.current(),2+(width-1)/2,2+(height-1)/2,(width-1)/2,(height-1)/2)
- blue.setBackgroundColor(colors.blue)
- blue.clear()
- local infoPanel = window.create(term.current(),1+math.floor((width/2)-(11/2)),1+math.floor((height/2)-(7/2)),11,7)
- infoPanel.setBackgroundColor(colors.black)
- infoPanel.clear()
- local infoX,infoY = infoPanel.getPosition()
- local startButton = window.create(term.current(),infoX+3,infoY+1,5,1)
- startButton.setBackgroundColor(colors.red)
- startButton.setTextColor(colors.black)
- startButton.setCursorPos(1,1)
- startButton.write("START")
- local closeButton = window.create(term.current(),infoX+3,infoY+5,5,1)
- closeButton.setBackgroundColor(colors.red)
- closeButton.setTextColor(colors.black)
- closeButton.setCursorPos(1,1)
- closeButton.write("CLOSE")
- local scoreLabel = window.create(term.current(),infoX+1,infoY+3,9,1)
- local pattern = {}
- local score = 0
- local quit = false
- local buttons = {green,red,yellow,blue,infoPanel,startButton,closeButton}
- local function refreshScore ()
- scoreLabel.clear()
- scoreLabel.setVisible(true)
- scoreLabel.setCursorPos(1+math.floor((scoreLabel.getSize()/2)-(string.len(tostring(score))/2)),1)
- scoreLabel.write(tostring(score))
- end
- local function playSound (sound,freq)
- local note = peripheral.find("note_block")
- if note == nil then
- return
- end
- note.playSound(sound,freq,1)
- end
- local function playBeep (freq,dur)
- if dur ~= nil then
- local time = 0
- while time < dur do
- playSound("note.harp",freq)
- sleep(.1)
- time = time + .1
- end
- return
- end
- parallel.waitForAny(function () while true do playSound("note.harp",freq) sleep(.1) end end,
- function () os.pullEvent("mouse_up") end)
- end
- local function lightUp (c)
- for i=1,10 do
- c.setCursorPos(1,i)
- c.write("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
- end
- infoPanel.redraw()
- closeButton.redraw()
- scoreLabel.redraw()
- end
- local function getClick (timeout)
- local buttonClicked = nil
- parallel.waitForAny(
- function ()
- if not timeout then
- while true do
- sleep(1)
- end
- end
- sleep(5)
- return
- end,
- function ()
- while true do
- local event,b,x,y = os.pullEvent("mouse_click")
- for i=table.getn(buttons),1,-1 do
- local xMin,yMin = buttons[i].getPosition()
- local w,h = buttons[i].getSize()
- local xMax = xMin + w - 1
- local yMax = yMin + h - 1
- if x <= xMax and y <= yMax and x >= xMin and y >= yMin then
- if buttons[i] == infoPanel then
- break
- end
- buttonClicked = buttons[i]
- return
- end
- end
- end
- end
- )
- return buttonClicked
- end
- local function beep (c,dur)
- local freq
- lightUp(c)
- if c == green then
- freq = .9
- elseif c == red then
- freq = 1.32
- elseif c == blue then
- freq = 1.2
- elseif c == yellow then
- freq = 1.4
- end
- playBeep(freq,dur)
- c.clear()
- infoPanel.redraw()
- startButton.redraw()
- closeButton.redraw()
- scoreLabel.redraw()
- if dur ~= nil then
- sleep(.375)
- end
- end
- local function playPattern ()
- table.insert(pattern,table.getn(pattern)+1,1+math.floor(math.random()*4))
- for i=1,table.getn(pattern) do
- beep(buttons[pattern[i]],duration)
- end
- return
- end
- while true do
- startButton.setVisible(true)
- while true do
- local c = getClick(false)
- if c == startButton then
- pattern = {}
- score = 0
- refreshScore()
- break
- elseif c == closeButton then
- quit = true
- break
- end
- end
- startButton.setVisible(false)
- if quit then
- break
- end
- local failed = false
- while not failed do
- playPattern()
- --parallel.waitForAny(playPattern(),
- -- function ()
- -- while true do
- -- local c = getClick(false)
- -- if c == closeButton then
- -- quit = true
- -- return
- -- end
- -- end
- -- end)
- playSound("note.hat",1.32)
- if quit then
- break
- end
- for i=1,table.getn(pattern) do
- local m
- while true do
- m = getClick(true)
- if m == nil then
- failed = true
- break
- elseif m == closeButton then
- quit = true
- break
- elseif m == green or m == red or m == yellow or m == blue then
- if m == buttons[pattern[i]] then
- beep(m)
- break
- else
- failed = true
- break
- end
- end
- end
- if failed or quit then
- break
- end
- end
- if quit then
- break
- elseif failed then
- lightUp(green)
- lightUp(red)
- lightUp(blue)
- lightUp(yellow)
- for p=1,15 do
- playSound("note.bass",.9)
- sleep(.1)
- end
- green.clear()
- red.clear()
- yellow.clear()
- blue.clear()
- infoPanel.redraw()
- closeButton.redraw()
- scoreLabel.redraw()
- else
- playSound("note.hat",.9)
- score = score+1
- refreshScore()
- sleep(1.5)
- end
- end
- end
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,1)
- term.clear()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement