The3vilM0nk3y

sphinx.lua

Aug 25th, 2022 (edited)
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.66 KB | None | 0 0
  1. function printSphinx(y)
  2.   f = fs.open("sphinxImg.txt", "r")
  3.   c = f.readAll()
  4.   f.close()
  5.   m.clear()
  6.   if y == nil then y = 1 end
  7.   m.setCursorPos(1,y)
  8.   t = term.redirect(m)
  9.   print(c)
  10.   term.redirect(t)
  11. end
  12. --Y = sphinx start y ie. Same as what is passed to printSphinx
  13. function talkAnimation(y)
  14.   if y == nil then y = 1 end
  15.   y = y + 6
  16.   x = 28
  17.   if mouth == "__" then mouth = "<>" else mouth = "__" end
  18.   m.setCursorPos(x,y)
  19.   m.write(mouth)
  20. end
  21. function showQuestion(y, qNum)
  22.   buttons = {}
  23.   if y == nil then y = 1 end
  24.   y = y + 23
  25.   if qNum == nil or qNum == 0 then
  26.     if #questions > 1 then
  27.       qNum = math.random(1,#questions)
  28.     else
  29.       qNum = 1
  30.     end
  31.   end
  32.  
  33.   m.setCursorPos(1,y)
  34.   t = term.redirect(m)
  35.   print(questions[qNum].question)
  36.   term.redirect(t)
  37.   --print responce buttons
  38.   qType = questions[qNum].type
  39.   if qType == "torf" then
  40.     buttons =
  41.     {
  42.       {y = h, x = midX - 5, text = "true"},
  43.       {y = h, x = midX + 1, text = "false"}
  44.     }
  45.   elseif qType == "multi" or qType == "all" then
  46.     yOff = 0
  47.     if qType == "all" then
  48.       yOff = 2
  49.       table.insert(buttons, {y = h, x = w - 7, text = "Submit"})
  50.     end
  51.     for i=1, #questions[qNum].choices do
  52.       -- print 2 buttons per line
  53.       doShift = false
  54.       if i % 2 == 0 then
  55.         xOff = 1
  56.         doShift = true
  57.       else
  58.         xOff = -1 - #questions[qNum].choices[i]
  59.       end
  60.       table.insert(buttons, {y = h - yOff, x = midX + xOff, text = questions[qNum].choices[i]})
  61.       if doShift then yOff = yOff + 2 end
  62.     end
  63.   --no buttons for this
  64.   --elseif qType == "text" then
  65.   end
  66.   --print the buttons
  67.   m.setBackgroundColor(colors.green)
  68.   for i=1, #buttons do
  69.     m.setCursorPos(buttons[i].x, buttons[i].y)
  70.     m.write(buttons[i].text)
  71.   end
  72.   m.setBackgroundColor(colors.black)
  73.   return qNum
  74. end
  75. function getButtonClicked(x,y)
  76.   for i=1, #buttons do
  77.     if x >= buttons[i].x and x <= buttons[i].x + #buttons[i].text and y == buttons[i].y then
  78.       return buttons[i].text
  79.     end
  80.   end
  81. end
  82. function main()
  83.   printSphinx()
  84.   selectedQuestion = showQuestion()
  85.   print("Question #" .. selectedQuestion)
  86.   gotAnswer = false
  87.   clickedButton = nil
  88.   --wait for an answer
  89.   while gotAnswer == false do
  90.     -- start a timer for animation breaks
  91.     timer = os.startTimer(1)
  92.     while true do
  93.       --wait for an event
  94.       event, p1, p2, p3 = os.pullEvent()
  95.       if event == "monitor_touch" then
  96.         --check if a button was clicked
  97.         clickedButton = getButtonClicked(p2,p3)
  98.         if clickedButton ~= nil then
  99.           gotAnswer = true
  100.           break
  101.         end
  102.       elseif event == "timer" and p1 == timer then
  103.         break
  104.       end
  105.     end
  106.     talkAnimation()
  107.   end
  108.   if clickedButton == questions[selectedQuestion].answer then
  109.     --correct answer
  110.     print("Correct Answer - " .. clickedButton)
  111.   else
  112.     --incorrect answer
  113.     print("Wrong. Given - " .. clickedButton .. "\nProper Answer - " .. questions[selectedQuestion].answer)
  114.   end
  115. end
  116. questions = {
  117.   {
  118.     type = "multi",
  119.     question = "What is the air speed velocity of an unladen swallow? (In meters per second)",
  120.     answer = "11",
  121.     choices = {"1","6","11","15"}
  122.   },
  123.   {
  124.     type = "torf",
  125.     question = "T or F: The planet Earth is tidally locked to the moon. ",
  126.     answer = "true",
  127.   },
  128.   {
  129.     type = "all",
  130.     question = "What timezone(s) does Kansas reside.",
  131.     answer = {"Mountain", "Central"},
  132.     choices = {"Pacific","Mountain","Central", "Eastern"}
  133.   },
  134. }
  135. buttons = {}
  136. m = peripheral.wrap("back")
  137. m.setTextScale(.5)
  138. w,h = m.getSize()
  139. midX = math.floor(w/2)
  140. while true do main() end
  141.  
  142.  
Advertisement
Add Comment
Please, Sign In to add comment