Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function printSphinx(y)
- f = fs.open("sphinxImg.txt", "r")
- c = f.readAll()
- f.close()
- m.clear()
- if y == nil then y = 1 end
- m.setCursorPos(1,y)
- t = term.redirect(m)
- print(c)
- term.redirect(t)
- end
- --Y = sphinx start y ie. Same as what is passed to printSphinx
- function talkAnimation(y)
- if y == nil then y = 1 end
- y = y + 6
- x = 28
- if mouth == "__" then mouth = "<>" else mouth = "__" end
- m.setCursorPos(x,y)
- m.write(mouth)
- end
- function showQuestion(y, qNum)
- buttons = {}
- if y == nil then y = 1 end
- y = y + 23
- if qNum == nil or qNum == 0 then
- if #questions > 1 then
- qNum = math.random(1,#questions)
- else
- qNum = 1
- end
- end
- m.setCursorPos(1,y)
- t = term.redirect(m)
- print(questions[qNum].question)
- term.redirect(t)
- --print responce buttons
- qType = questions[qNum].type
- if qType == "torf" then
- buttons =
- {
- {y = h, x = midX - 5, text = "true"},
- {y = h, x = midX + 1, text = "false"}
- }
- elseif qType == "multi" or qType == "all" then
- yOff = 0
- if qType == "all" then
- yOff = 2
- table.insert(buttons, {y = h, x = w - 7, text = "Submit"})
- end
- for i=1, #questions[qNum].choices do
- -- print 2 buttons per line
- doShift = false
- if i % 2 == 0 then
- xOff = 1
- doShift = true
- else
- xOff = -1 - #questions[qNum].choices[i]
- end
- table.insert(buttons, {y = h - yOff, x = midX + xOff, text = questions[qNum].choices[i]})
- if doShift then yOff = yOff + 2 end
- end
- --no buttons for this
- --elseif qType == "text" then
- end
- --print the buttons
- m.setBackgroundColor(colors.green)
- for i=1, #buttons do
- m.setCursorPos(buttons[i].x, buttons[i].y)
- m.write(buttons[i].text)
- end
- m.setBackgroundColor(colors.black)
- return qNum
- end
- function getButtonClicked(x,y)
- for i=1, #buttons do
- if x >= buttons[i].x and x <= buttons[i].x + #buttons[i].text and y == buttons[i].y then
- return buttons[i].text
- end
- end
- end
- function main()
- printSphinx()
- selectedQuestion = showQuestion()
- print("Question #" .. selectedQuestion)
- gotAnswer = false
- clickedButton = nil
- --wait for an answer
- while gotAnswer == false do
- -- start a timer for animation breaks
- timer = os.startTimer(1)
- while true do
- --wait for an event
- event, p1, p2, p3 = os.pullEvent()
- if event == "monitor_touch" then
- --check if a button was clicked
- clickedButton = getButtonClicked(p2,p3)
- if clickedButton ~= nil then
- gotAnswer = true
- break
- end
- elseif event == "timer" and p1 == timer then
- break
- end
- end
- talkAnimation()
- end
- if clickedButton == questions[selectedQuestion].answer then
- --correct answer
- print("Correct Answer - " .. clickedButton)
- else
- --incorrect answer
- print("Wrong. Given - " .. clickedButton .. "\nProper Answer - " .. questions[selectedQuestion].answer)
- end
- end
- questions = {
- {
- type = "multi",
- question = "What is the air speed velocity of an unladen swallow? (In meters per second)",
- answer = "11",
- choices = {"1","6","11","15"}
- },
- {
- type = "torf",
- question = "T or F: The planet Earth is tidally locked to the moon. ",
- answer = "true",
- },
- {
- type = "all",
- question = "What timezone(s) does Kansas reside.",
- answer = {"Mountain", "Central"},
- choices = {"Pacific","Mountain","Central", "Eastern"}
- },
- }
- buttons = {}
- m = peripheral.wrap("back")
- m.setTextScale(.5)
- w,h = m.getSize()
- midX = math.floor(w/2)
- while true do main() end
Advertisement
Add Comment
Please, Sign In to add comment