Advertisement
Mackan90096

Slot machine

Apr 10th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.96 KB | None | 0 0
  1. --[[
  2. Copyright 2015 Max Thor <thormax5@gmail.com>
  3.  
  4.    Licensed under the Apache License, Version 2.0 (the "License");
  5.    you may not use this file except in compliance with the License.
  6.    You may obtain a copy of the License at
  7.  
  8.        http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10.    Unless required by applicable law or agreed to in writing, software
  11.    distributed under the License is distributed on an "AS IS" BASIS,
  12.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.    See the License for the specific language governing permissions and
  14.    limitations under the License.
  15.    ]]--
  16.    
  17. -- Variables
  18.  
  19. local color = false
  20. local creds = 0
  21. local plusX = 0
  22. local w, h = term.getSize()
  23.  
  24. function cWrite(txt,y)
  25.     term.setCursorPos(math.floor(w-string.len(txt))/2+plusX, h/2)
  26.     term.write(txt)
  27.     plusX = plusX+1
  28. end
  29.  
  30. function cWrite2(txt, y)
  31.     term.setCursorPos(math.floor(w-string.len(txt))/2, y)
  32.     term.write(txt)
  33. end
  34.  
  35. function win(amt)
  36.     creds = creds+amt
  37.     if(color) then
  38.         term.setTextColor(16)
  39.         cWrite2("Won! "..amt, 2)
  40.     else
  41.         cWrite2("Won! "..amt, 2)
  42.     end
  43. end
  44.  
  45. function save()
  46.     local h = fs.open(".slots/save", "w")
  47.     h.write(creds)
  48.     h.close()
  49. end
  50.  
  51. function ui()
  52.     if(color) then
  53.         term.setCursorPos(1,1)
  54.         term.setBackgroundColor(256)
  55.         term.clearLine()
  56.         term.setTextColor(1)
  57.         term.write("Credits: "..creds)
  58.         term.setBackgroundColor(32768)
  59.     else
  60.         term.setCursorPos(1,1)
  61.         term.clearLine()
  62.         term.setTextColor(1)
  63.         term.write("Credits: "..creds)
  64.     end
  65. end
  66.  
  67. function spin()
  68.     term.clear()
  69.     local a = math.random(0, 3)
  70.     local b = math.random(0, 3)
  71.     local c = math.random(0, 3)
  72.    
  73.     if(a == 1) then
  74.         if(color) then
  75.             term.setTextColor(32)
  76.             cWrite("X", 0)
  77.         else
  78.             cWrite("X", 0)
  79.         end
  80.     elseif(a == 2) then
  81.         if(color) then
  82.             term.setTextColor(2)
  83.             cWrite("$", 0)
  84.         else
  85.             cWrite("$", 0)
  86.         end
  87.     elseif(a == 3) then
  88.         if(color) then
  89.             term.setTextColor(8)
  90.             cWrite("O", 0)
  91.         else
  92.             cWrite("O", 0)
  93.         end
  94.     else
  95.         if(color) then
  96.             term.setTextColor(16384)
  97.             cWrite("-",0)
  98.         else
  99.             cWrite("-", 0)
  100.         end
  101.     end
  102.    
  103.     if(b == 1) then
  104.         if(color) then
  105.             term.setTextColor(32)
  106.             cWrite("X", 0)
  107.         else
  108.             cWrite("X", 0)
  109.         end
  110.     elseif(b == 2) then
  111.         if(color) then
  112.             term.setTextColor(2)
  113.             cWrite("$", 0)
  114.         else
  115.             cWrite("$", 0)
  116.         end
  117.     elseif(b == 3) then
  118.         if(color) then
  119.             term.setTextColor(8)
  120.             cWrite("O", 0)
  121.         else
  122.             cWrite("O", 0)
  123.         end
  124.     else
  125.         if(color) then
  126.             term.setTextColor(16384)
  127.             cWrite("-",0)
  128.         else
  129.             cWrite("-", 0)
  130.         end
  131.     end
  132.  
  133.     if(c == 1) then
  134.         if(color) then
  135.             term.setTextColor(32)
  136.             cWrite("X", 0)
  137.         else
  138.             cWrite("X", 0)
  139.         end
  140.     elseif(c == 2) then
  141.         if(color) then
  142.             term.setTextColor(2)
  143.             cWrite("$", 0)
  144.         else
  145.             cWrite("$", 0)
  146.         end
  147.     elseif(c == 3) then
  148.         if(color) then
  149.             term.setTextColor(8)
  150.             cWrite("O", 0)
  151.         else
  152.             cWrite("O", 0)
  153.         end
  154.     else
  155.         if(color) then
  156.             term.setTextColor(16384)
  157.             cWrite("-",0)
  158.         else
  159.             cWrite("-", 0)
  160.         end
  161.     end
  162.  
  163.     if(a == 1 and b == 1 and c == 1) then
  164.         win(5)
  165.     elseif(a == 2 and b == 2 and c == 2) then
  166.         win(10)
  167.     elseif(a == 3 and b == 3 and c == 3) then
  168.         win(15)
  169.     end
  170.    
  171.     plusX = 0
  172.     ui()   
  173. end
  174.  
  175. function main()
  176.     term.clear()
  177.     ui()
  178.     while true do
  179.         e, p1 = os.pullEventRaw()
  180.         if(e == "key") then
  181.             if(p1 == 57) then
  182.                 if(creds > 0) then
  183.                     creds = creds-1
  184.                     spin()
  185.                 end
  186.             elseif(p1 == 1) then
  187.                 save()
  188.                 os.queueEvent("terminate")
  189.             end
  190.         elseif(e == "terminate") then
  191.             term.setTextColor(1)
  192.             term.setBackgroundColor(32768)
  193.             term.clear()
  194.             plusX = 0
  195.             cWrite("Thanks for playing!")
  196.             break
  197.         end
  198.     end
  199. end
  200.  
  201.    
  202. function setup()
  203.     if(term.isColor()) then
  204.         color = true
  205.     end
  206.     if(fs.exists(".slots/save")) then
  207.         local h = fs.open(".slots/save", "r")
  208.         creds = tonumber(h.readAll())
  209.         h.close()
  210.         main()
  211.     else
  212.         fs.makeDir(".slots")
  213.         local h = fs.open(".slots/save", "w")
  214.         h.write("10")
  215.         h.close()
  216.     end
  217. end
  218.  
  219. setup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement