Advertisement
jesusthekiller

Dice roll

Jul 25th, 2013
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. local dice = {}
  2.  
  3. dice[1] = {
  4.     "+---------+  |",
  5.     "|         |  |",
  6.     "|         |  |",
  7.     "|    O    |  |",
  8.     "|         | /",
  9.     "|         |/",
  10.     "+---------+",
  11. }
  12.  
  13. dice[2] = {
  14.     "+---------+  |",
  15.     "|         |  |",
  16.     "|         |  |",
  17.     "| O     O |  |",
  18.     "|         | /",
  19.     "|         |/",
  20.     "+---------+",
  21. }
  22.  
  23. dice[3] = {
  24.     "+---------+  |",
  25.     "| O       |  |",
  26.     "|         |  |",
  27.     "|    O    |  |",
  28.     "|         | /",
  29.     "|       O |/",
  30.     "+---------+",
  31. }
  32.  
  33. dice[4] = {
  34.     "+---------+  |",
  35.     "|         |  |",
  36.     "| O     O |  |",
  37.     "|         |  |",
  38.     "| O     O | /",
  39.     "|         |/",
  40.     "+---------+",
  41. }
  42.  
  43. dice[5] = {
  44.     "+---------+  |",
  45.     "| O     O |  |",
  46.     "|         |  |",
  47.     "|    O    |  |",
  48.     "|         | /",
  49.     "| O     O |/",
  50.     "+---------+",
  51. }
  52.  
  53. dice[6] = {
  54.     "+---------+  |",
  55.     "| O     O |  |",
  56.     "|         |  |",
  57.     "| O     O |  |",
  58.     "|         | /",
  59.     "| O     O |/",
  60.     "+---------+",
  61. }
  62.  
  63. local function cwrite(t)
  64.     term.setCursorPos((51-#t)/2, ({term.getCursorPos()})[2])
  65.     write(t)
  66. end
  67.  
  68. local function cprint(t)
  69.     cwrite(t)
  70.     write("\n")
  71. end
  72.  
  73. local function clear(c)
  74.     term.setBackgroundColor(c)
  75.     term.setTextColor(colors.white)
  76.     term.clear()
  77.     term.setCursorPos(1, 1)
  78. end
  79.  
  80. local function makeDice(val)
  81.     term.setTextColor(colors.white)
  82.     term.setBackgroundColor(colors.black)
  83.    
  84.     term.setCursorPos(24, 6)
  85.     write("+---------+")
  86.    
  87.     term.setCursorPos(23, 7)
  88.     write("/         /|")
  89.    
  90.     term.setCursorPos(22, 8)
  91.     write("/         / |")
  92.    
  93.     for i = 1, 7 do
  94.         term.setCursorPos(21, 8+i)
  95.         write(dice[val][i])
  96.     end
  97. end
  98.  
  99. local function last5(last)
  100.     -- Last 5 rolls
  101.     term.setCursorPos(1, 1)
  102.     term.setBackgroundColor(colors.cyan)
  103.     write("Last 9|")
  104.    
  105.     term.setBackgroundColor(colors.blue)
  106.    
  107.     for i = 1, 18 do
  108.         term.setCursorPos(1, i+1)
  109.        
  110.        
  111.         if i % 2 == 1 then
  112.             last[(i+1)/2] = last[(i+1)/2] and last[(i+1)/2] or 0
  113.             for j = 1, last[(i+1)/2] do
  114.                 write("o")
  115.             end
  116.            
  117.             for j = 1, 6 - last[(i+1)/2] do
  118.                 write(" ")
  119.             end
  120.            
  121.             write("|")
  122.         else
  123.             write("+-----<")
  124.         end
  125.     end
  126. end
  127.  
  128. local function roll(last)
  129.     clear(colors.black)
  130.    
  131.     -- Close button
  132.     term.setCursorPos(50, 1)
  133.     term.setTextColor(colors.white)
  134.     term.setBackgroundColor(colors.red)
  135.     write("\\/")
  136.     term.setCursorPos(50, 2)
  137.     write("/\\")
  138.    
  139.     last5(last)
  140.    
  141.     -- Roll
  142.     local val
  143.     for i = 0.1, 0.7, 0.03 do
  144.         val = math.random(1,6)
  145.         makeDice(val)
  146.         sleep(i*i)
  147.     end
  148.    
  149.     -- Move items in last
  150.     for i = 9, 2, -1 do
  151.         last[i] = last[i-1]
  152.     end
  153.    
  154.     last[1] = val
  155.    
  156.     last5(last)
  157.    
  158.     return last
  159. end
  160.  
  161. ---------------
  162.  
  163. -- Splash
  164. clear(colors.black)
  165. cprint("Dice roll")
  166. cprint("Click on screen to roll dice!")
  167. cprint("(or click on X to exit)")
  168.  
  169. term.setCursorPos(1, 15)
  170. term.setTextColor(colors.yellow)
  171. cprint("Click on screen to continue")
  172. term.setCursorPos(1, 19)
  173. term.setTextColor(colors.gray)
  174. cwrite("by Jesusthekiller")
  175. os.pullEvent("mouse_click")
  176.  
  177. -- Roll
  178. local last = {}
  179. while true do
  180.     last = roll(last)
  181.     local e, b, x, y = os.pullEvent("mouse_click")
  182.     if x >= 50 and y <= 2 then
  183.         break
  184.     end
  185. end
  186.  
  187. clear(colors.black)
  188. cprint("By Jesusthekiller")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement