Advertisement
Mackan90096

Slot Machine

May 3rd, 2013
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.38 KB | None | 0 0
  1. -- Variables --
  2. w, h = term.getSize()
  3. credits = 10
  4. bet = 0
  5. spin = 0
  6. version = "1.0"
  7. w2 = w-20
  8. selected = "1"
  9. numb2 = 10
  10. won = 0
  11. totalWon = 0
  12.  
  13. -- End Variables --
  14.  
  15. -- Functions --
  16.  
  17. function game()
  18.  
  19. statText = "Press spacebar to spin"
  20.  
  21.  
  22.  
  23. local function start()
  24. main()
  25. printMoney()
  26. end
  27.  
  28. function cashOut()
  29. term.clear()
  30. term.setBackgroundColor(32)
  31. term.setTextColor(32768)
  32. term.clear()
  33. term.setCursorPos(math.floor(w-string.len("Cashed Out."))/2, 2)
  34. print("Cashed Out.")
  35. term.setCursorPos(math.floor(w-string.len("You won: "..totalWon))/2, 4)
  36. print("You won: "..totalWon)
  37. term.setCursorPos(math.floor(w-string.len("You spinned: "..spin.." times."))/2, 6)
  38. print("You spinned: "..spin.." times.")
  39. sleep(5)
  40. term.setBackgroundColor(colors.black)
  41. term.clear()
  42. term.setCursorPos(1, 1)
  43. error()
  44. end
  45.  
  46.  
  47. function main()
  48. term.setBackgroundColor(8)
  49. term.clear()
  50. term.setBackgroundColor(8)
  51. term.setCursorPos(1,1)
  52. print(statText)
  53. end
  54.  
  55. function printMoney()
  56. term.setBackgroundColor(8)  
  57. term.clear()
  58. term.setBackgroundColor(8)
  59. term.setTextColor(colors.black)
  60. term.setCursorPos(1,1)
  61. print(statText)
  62. term.setCursorPos(1,3)
  63. print("You have: "..credits.." credits")
  64. term.setCursorPos(1,5)
  65. term.clearLine()
  66. term.setCursorPos(1, 5)
  67. print("           ")
  68. term.setCursorPos(1, 5)
  69. print("Won: "..won)
  70. end
  71.  
  72. -- User interaction --
  73.  
  74. while true do
  75.  
  76. local event, key = os.pullEvent("key")
  77.  if event == "key" then
  78.   if key == 57 then -- key == 57 means key = spacebar.
  79.     if credits < 1 then
  80.     term.clear()
  81.     term.setCursorPos(1, 1)
  82.     print("Not enough money. Ending game")
  83.     sleep(1)
  84.     error()
  85.     elseif credits >= 1 then
  86.         credits = credits-1
  87.     spin = spin + 1
  88.   end
  89.     if spin >= 5 then
  90.         numb2 = 50
  91.     else
  92.      number = math.random(numb2)
  93.        if number == 1 then
  94.         term.setCursorPos(1, 5)
  95.         term.clearLine()
  96.          print("Won: 10")
  97.          won = 10
  98.          credits = credits + won
  99.          printMoney()
  100.        elseif number == 2 then
  101.         term.setCursorPos(1, 5)
  102.         term.clearLine()
  103.          print("Won: 10")
  104.          won = 10
  105.          credits = credits + won
  106.          printMoney()
  107. elseif number == 3 then
  108.     term.setCursorPos(1, 5)
  109.         term.clearLine()
  110.          print("Won: 10")
  111.          won = 10
  112.          credits = credits + won
  113.          printMoney()
  114.          elseif number == 4 then
  115.             term.setCursorPos(1, 5)
  116.         term.clearLine()
  117.          print("Won: 10")
  118.          won = 10
  119.          credits = credits + won
  120.          printMoney()
  121.          elseif number == 5 then
  122.             term.setCursorPos(1, 5)
  123.         term.clearLine()
  124.          print("Won: 20")
  125.          won = 20
  126.          credits = credits + won
  127.          printMoney()
  128.         elseif number == 6 then
  129.             term.setCursorPos(1, 5)
  130.         term.clearLine()
  131.          print("Won: 50")
  132.          won = 50
  133.          credits = credits + won
  134.          printMoney()
  135.          elseif number == 7 then
  136.             term.setCursorPos(1, 5)
  137.         term.clearLine()
  138.             print("Won: 100")
  139.             won = 100
  140.          credits = credits + won
  141.             printMoney()
  142.          elseif number == 8 then
  143.             term.setCursorPos(1, 5)
  144.         term.clearLine()
  145.             print("Won: 500")
  146.             won = 500
  147.          credits = credits + won
  148.             printMoney()
  149.          elseif number == 36 then
  150.             term.setCursorPos(1, 5)
  151.         term.clearLine()
  152.             print("Won: 1000")
  153.             won = 1000
  154.          credits = credits + won
  155.          printMoney()
  156.         else
  157.             term.setCursorPos(1, 5)
  158.         term.clearLine()
  159.         print("Won: 0")
  160.         printMoney()
  161.         won = 0
  162.         end
  163.     end
  164. end
  165. end
  166. start()
  167. end
  168. end
  169.  
  170. function gameHelp()
  171. term.clear()
  172. term.setCursorPos(1, 1)
  173. print("Nothing here for now.")
  174. print(" Press arrow up to exit")
  175.  
  176. end
  177.  
  178. function menu1()
  179. if selected == "1" then
  180. term.clear()
  181. paintutils.drawLine(1, 1, w, 1, 128)
  182. term.setTextColor(colors.white)
  183. term.setCursorPos(math.floor(w-string.len("Slots Version: "..version))/2, 1)
  184. print("Slots Version: "..version)
  185. paintutils.drawLine(1, h, w, h, 128)
  186. term.setTextColor(colors.white)
  187. term.setCursorPos(w2, h)
  188. print("Made by: Mackan90096")
  189. term.setBackgroundColor(colors.white)
  190. term.setTextColor(colors.gray)
  191. term.setCursorPos(math.floor(w-string.len("[Play]"))/2, 4)
  192. print("[Play]")
  193. term.setCursorPos(math.floor(w-string.len("Help"))/2, 6)
  194. print("Help")
  195. term.setCursorPos(math.floor(w-string.len("About"))/2, 8)
  196. print("About")
  197. elseif selected == "2" then
  198. term.clear()
  199. paintutils.drawLine(1, 1, w, 1, 128)
  200. term.setTextColor(colors.white)
  201. term.setCursorPos(math.floor(w-string.len("Slots Version: "..version))/2, 1)
  202. print("Slots Version: "..version)
  203. paintutils.drawLine(1, h, w, h, 128)
  204. term.setTextColor(colors.white)
  205. term.setCursorPos(w2, h)
  206. print("Made by: Mackan90096")
  207. term.setBackgroundColor(colors.white)
  208. term.setTextColor(colors.gray)
  209. term.setCursorPos(math.floor(w-string.len("Play"))/2, 4)
  210. print("Play")
  211. term.setCursorPos(math.floor(w-string.len("[Help]"))/2, 6)
  212. print("[Help]")
  213. term.setCursorPos(math.floor(w-string.len("About"))/2, 8)
  214. print("About")
  215. elseif selected == "3" then
  216. term.clear()
  217. paintutils.drawLine(1, 1, w, 1, 128)
  218. term.setTextColor(colors.white)
  219. term.setCursorPos(math.floor(w-string.len("Slots Version: "..version))/2, 1)
  220. print("Slots Version: "..version)
  221. paintutils.drawLine(1, h, w, h, 128)
  222. term.setTextColor(colors.white)
  223. term.setCursorPos(w2, h)
  224. print("Made by: Mackan90096")
  225. term.setBackgroundColor(colors.white)
  226. term.setTextColor(colors.gray)
  227. term.setCursorPos(math.floor(w-string.len("Play"))/2, 4)
  228. print("Play")
  229. term.setCursorPos(math.floor(w-string.len("Help"))/2, 6)
  230. print("Help")
  231. term.setCursorPos(math.floor(w-string.len("[About]"))/2, 8)
  232. print("[About]")
  233. end
  234. end
  235.  
  236. menu1()
  237.  
  238. -- End Functions --
  239.  
  240. -- User Interaction --
  241. while true do
  242.     event, key = os.pullEvent("key")
  243.     if event == "key" then
  244.         if key == 208 then
  245.             if selected == "1" then selected = "2"
  246.                 menu1()
  247.             elseif selected == "2" then selected = "3"
  248.                 menu1()
  249.             end
  250.         end
  251.         if key == 200 then
  252.             if selected == "3" then selected = "2"
  253.                 menu1()
  254.             elseif selected == "2" then selected = "1"
  255.                 menu1()
  256.             end
  257.         end
  258.         end
  259.         if key == 28 then
  260.             if selected == "1" then
  261.                 game()
  262.             elseif selected == "2" then
  263.                 gameHelp()
  264.             end
  265.         end
  266.     end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement