Advertisement
j3d247

Untitled

Feb 16th, 2014
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. os.loadAPI("displayAPI")
  2. --Peripherals
  3. local b = peripheral.wrap("bottom") -- Bridge Pos
  4. local d = peripheral.wrap("left") -- Monitor Pos
  5. --End Peripherals
  6.  
  7. --Winner Config
  8. local dispwin = 2 -- How Long To Display Winner
  9. local wintxt = "And The Winner Is:" -- Winner Text
  10. local wintxtsz = 3 -- Winner Font Size
  11. --End Winner Config
  12.  
  13. --Scoreboard Config
  14. local glasses = true -- Display on glasses? true/false
  15. local sbtxtsz = 3 -- Scoreboard Text Size
  16. --End Scoreboard Config
  17. term.clear()
  18. term.setCursorPos(1,1)
  19. d.clear()
  20. b.clear()
  21. function spairs(t, order)
  22. -- collect the keys
  23. local keys = {}
  24. for k in pairs(t) do keys[#keys+1] = k end
  25.  
  26. -- if order function given, sort by it by passing the table and keys a, b,
  27. -- otherwise just sort the keys
  28. if order then
  29. table.sort(keys, function(a,b) return order(t, a, b) end)
  30. else
  31. table.sort(keys)
  32. end
  33.  
  34. -- return the iterator function
  35. local i = 0
  36. return function()
  37. i = i + 1
  38. if keys[i] then
  39. return keys[i], t[keys[i]]
  40. end
  41. end
  42. end
  43. function cwrite(text)
  44. local w, h = d.getSize()
  45. local x, y = d.getCursorPos()
  46. x = math.max(math.floor((w/2) - (#text / 2)), 0)
  47. d.setCursorPos(x,y)
  48. text = string.upper(text)
  49. d.write(text)
  50. d.setCursorPos(x,y+1)
  51. end
  52. function pwrite(text)
  53. local w, h = d.getSize()
  54. local x,y = d.getCursorPos()
  55. x = math.max(math.floor((15/2) - (#text / 2)), 0)
  56. d.setCursorPos(x,y)
  57. text = string.upper(text)
  58. d.write(text)
  59. d.setCursorPos(x,y+1)
  60. end
  61. function twrite(text)
  62. local x2,y2 = term.getCursorPos()
  63. local x,y = term.getSize()
  64. term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
  65. print(text)
  66. end
  67.  
  68. function winner(name)
  69. name = string.upper(name)
  70. d.clear()
  71. d.setCursorPos(1,6)
  72. d.setTextScale(wintxtsz)
  73. cwrite(wintxt)
  74. sleep(1)
  75. cwrite(name)
  76. for n,s in pairs(players) do
  77. if n == name then
  78. s = tostring(s+1)
  79. players[n] = s
  80. end
  81. end
  82. sleep(dispwin)
  83. scoreboard()
  84. end
  85.  
  86. function refreshScreen()
  87. d.clear()
  88. scoreboard()
  89. end
  90.  
  91. function restartGame()
  92. os.reboot()
  93. end
  94.  
  95. function scoreboard()
  96. d.clear()
  97. term.clear()
  98. term.setCursorPos(1,8)
  99. twrite("Loneztar's Sheep Race")
  100. if players == nil then
  101. d.setCursorPos(1,4)
  102. d.setTextScale(3)
  103. cwrite("No Players Added")
  104. else
  105. d.setCursorPos(3,3)
  106. d.setTextScale(sbtxtsz)
  107. d.write("Players")
  108. d.setCursorPos(19,3)
  109. d.write("Score")
  110. d.setCursorPos(1,4)
  111. for name,score in spairs(players, function(t,a,b) return t[b] < t[a] end) do
  112. local h, v = d.getCursorPos()
  113. pwrite(name)
  114. d.setCursorPos(21,v)
  115. d.write(score)
  116. d.setCursorPos(h,v+1)
  117. end
  118. end
  119. end
  120.  
  121. function addPlayer(name)
  122. name = string.upper(name)
  123. if players == nil then
  124. players = {}
  125. players[name] = "0"
  126. else
  127. players[name] = "0"
  128. end
  129. end
  130.  
  131. function startScreen()
  132. term.setCursorPos(1,8)
  133. twrite("Loneztar's Sheep Race")
  134. term.setCursorPos(1,11)
  135. twrite("To Get Started")
  136. twrite("Type '$$Start' into your chat")
  137. d.clear()
  138. d.setCursorPos(1,6)
  139. d.setTextScale(3)
  140. cwrite("Loneztars'")
  141. cwrite("Sheep Race")
  142. d.setCursorPos(1,9)
  143. cwrite("$$start to begin!")
  144. end
  145.  
  146. function help()
  147. if commandBox == nil then
  148. commandBox = b.addBox(10, 40, 95, 90, 0xffffff, 0.5)
  149. cmdx = commandBox.getX()
  150. cmdy = commandBox.getY()
  151. cmdop = commandBox.getOpacity()
  152. titleline = b.addBox(cmdx, cmdy+10, 95, 1, 0x000000, cmdop)
  153. cTitle = b.addText((cmdx+1), (cmdy+2), "Commands List:", 0x000000)
  154. local commands = {"addplayer <name>","winner <name>", "refresh", "RESTART"}
  155. vpos = cmdy+15
  156. for _,v in pairs(commands) do
  157. b.addText(cmdx+1,vpos, v,0x000000)
  158. vpos = vpos+10
  159. end
  160. else
  161. b.clear()
  162. end
  163. end
  164.  
  165. while true do
  166. scoreboard()
  167. local e, msg = os.pullEvent("chat_command")
  168. local tWords = {}
  169. for match in string.gmatch(msg, "[^ \t]+" ) do
  170. table.insert( tWords, match )
  171. end
  172. --Commands List
  173. if tWords[1] == "winner" then
  174. winner(tWords[2])
  175. elseif tWords[1] == "refresh" then
  176. refreshScreen()
  177. elseif tWords[1] == "addplayer" then
  178. addPlayer(tWords[2])
  179. elseif tWords[1] == "RESTART" then
  180. restartGame()
  181. elseif tWords[1] == "help" then
  182. help()
  183. elseif tWords[1] == "start" then
  184. scoreboard()
  185. elseif tWords[1] == "begin" then
  186. startScreen()
  187. end
  188. -- End Commands
  189. end --whiletruedo
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement