Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --------------------------------------------------
- --Please install this using the installer (pastebin run RkRVwZTW)
- --------------------------------------------------
- -- Constants
- local limit = 10 -- Minimum credits required to play
- local diamondW = 1 -- % chance to land diamond
- local dollarW = 2 -- % chance to land dollar
- local sevenW = 3 -- % chance to land seven
- local bellW = 4 -- % chance to land bell
- local orangeW = 5 -- % chance to land orange
- -- Chances for symbols
- local orangech = 30
- local diamondch = 5
- local bellch = 20
- local sevench = 15
- local dollarch = 10
- -- Credit and bet variables
- local credits = 0
- local bet = limit
- local multi = 0
- local price = 0
- local playerName = ""
- -- Load images
- local diamond = paintutils.loadImage("images/diamond.nfp")
- local bell = paintutils.loadImage("images/bell.nfp")
- local seven = paintutils.loadImage("images/7.nfp")
- local dollar = paintutils.loadImage("images/dollar.nfp")
- local orange = paintutils.loadImage("images/orange.nfp")
- local none = paintutils.loadImage("images/none.nfp")
- -- Center text function
- function centerText(y, text)
- local width = term.getSize()
- term.setCursorPos(math.floor((width - #text) / 2), y)
- term.write(text)
- end
- -- Display insert card screen
- function insertCard()
- while not fs.exists("disk/") do
- paintutils.drawFilledBox(1, 1, 51, 19, colors.black)
- centerText(8, "[Please Insert Casino Card]")
- sleep(1)
- end
- paintutils.drawFilledBox(1, 1, 51, 19, colors.black)
- end
- -- Load player's name from name.txt on the disk
- function loadPlayerName()
- if fs.exists("disk/name.txt") then
- local nameFile = fs.open("disk/name.txt", "r")
- playerName = nameFile.readAll():gsub("\n", "")
- nameFile.close()
- else
- centerText(8, "[Player name file not found!]")
- sleep(2)
- return false
- end
- return true
- end
- -- Read credits from card
- function readCard()
- if not fs.exists("disk/creds.lua") then
- centerText(8, "[Casino Card not found!]")
- sleep(2)
- return false
- end
- local Card = fs.open("disk/creds.lua", "r")
- local data = Card.readAll()
- Card.close()
- -- Extract credits
- local credsStart = string.find(data, "11066011") + 8
- local credsEnd = string.find(data, "11077011", credsStart)
- credits = tonumber(string.sub(data, credsStart, credsEnd - 1))
- if credits < limit then
- paintutils.drawFilledBox(1, 1, 51, 19, colors.red)
- centerText(10, "[Insufficient Funds.]")
- while fs.exists("disk/") do
- sleep(0.5)
- end
- return false
- end
- return true
- end
- -- Write credits back to card
- function writeCard()
- if not fs.exists("disk/creds.lua") then
- centerText(8, "[Card not found!]")
- sleep(2)
- return false
- end
- local card = fs.open("disk/creds.lua", "w")
- local data = tostring(math.random(1, 163456)) .. "11066011" .. tostring(credits) .. "11077011" .. tostring(math.random(1, 163456))
- card.write(data)
- card.close()
- disk.setLabel("bottom", playerName .. "'s Casino Card - " .. tostring(credits) .. " Credits")
- end
- -- Insert bet function
- function insertBet()
- local validBet = false
- while not validBet do
- paintutils.drawFilledBox(1, 1, 51, 19, colors.black)
- centerText(8, "[Enter Bet Amount]")
- centerText(10, "[Current Credits: " .. credits .. "]")
- term.setCursorPos(15, 12)
- term.write("Bet: ")
- local input = read()
- local betAmount = tonumber(input)
- if betAmount and betAmount > 0 and betAmount <= credits then
- bet = betAmount
- validBet = true
- else
- centerText(14, "[Invalid bet. Try again.]")
- sleep(2)
- end
- end
- credits = credits - bet
- writeCard()
- end
- -- Constants for symbols
- local resultMultiplier = {0, 2, 1.75, 1.5, 1.25, 1}
- local resultPrice = {0, 1, 2, 3, 4, 5}
- -- Slot machine visual setup
- function slotMachine()
- paintutils.drawFilledBox(1, 1, 51, 19, colors.green)
- paintutils.drawFilledBox(5, 1, 46, 19, colors.lightGray)
- paintutils.drawLine(5, 1, 5, 19, colors.white)
- paintutils.drawLine(19, 1, 19, 19, colors.white)
- paintutils.drawLine(33, 1, 33, 19, colors.white)
- paintutils.drawLine(47, 1, 47, 19, colors.white)
- paintutils.drawLine(2, 10, 5, 10, colors.gray)
- paintutils.drawLine(47, 10, 50, 10, colors.gray)
- end
- -- Draw random symbols on the slot machine
- function drawSymbols(column, row)
- local images = {none, diamond, dollar, seven, bell, orange}
- local symbolIndex = math.random(1, #images)
- paintutils.drawImage(images[symbolIndex], 8 + (column - 1) * 14, row)
- return symbolIndex
- end
- -- Roll the slot machine
- function roll()
- local results = {}
- for i = 1, 3 do
- local symbolIndex = drawSymbols(i, 8)
- table.insert(results, symbolIndex)
- sleep(0.8)
- end
- return results
- end
- -- Display the final results
- function displayResults(results)
- local totalWin = 0
- for _, symbolIndex in ipairs(results) do
- totalWin = totalWin + (bet * resultMultiplier[symbolIndex])
- end
- if totalWin > 0 then
- credits = credits + totalWin
- centerText(15, "[You Win: " .. totalWin .. " Credits!]")
- else
- centerText(15, "[You Lose!]")
- end
- writeCard()
- sleep(2)
- end
- -- Lever pull screen
- function lever()
- paintutils.drawFilledBox(1, 1, 51, 19, colors.black)
- centerText(8, "[Pull lever to roll]")
- while not redstone.getInput("right") do
- sleep(0.1)
- end
- while redstone.getInput("right") do
- sleep(0.1)
- end
- end
- -- Main game loop
- while true do
- insertCard()
- if readCard() then
- if loadPlayerName() then
- insertBet()
- lever()
- slotMachine()
- local results = roll()
- displayResults(results)
- end
- end
- end
Add Comment
Please, Sign In to add comment