This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mackan90096's profile picture

[Game] Slots

Started by Mackan90096, 03 May 2013 - 03:27 AM
Mackan90096 #1
Posted 03 May 2013 - 05:27 AM
Hi! I just finished my "slot machine" game.
It requires a advanced computer.
Use arrows up and down for menu movement and enter to select.

Code:
Spoiler


-- Variables --
w, h = term.getSize()
credits = 10
bet = 0
spin = 0
version = "1.0"
w2 = w-20
selected = "1"
numb2 = 10
won = 0
totalWon = 0

-- End Variables --

-- Functions --

function game()

statText = "Press spacebar to spin"



local function start()
main()
printMoney()
end

function cashOut()
term.clear()
term.setBackgroundColor(32)
term.setTextColor(32768)
term.clear()
term.setCursorPos(math.floor(w-string.len("Cashed Out."))/2, 2)
print("Cashed Out.")
term.setCursorPos(math.floor(w-string.len("You won: "..totalWon))/2, 4)
print("You won: "..totalWon)
term.setCursorPos(math.floor(w-string.len("You spinned: "..spin.." times."))/2, 6)
print("You spinned: "..spin.." times.")
sleep(5)
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1, 1)
error()
end


function main()
term.setBackgroundColor(8)
term.clear()
term.setBackgroundColor(8)
term.setCursorPos(1,1)
print(statText)
end

function printMoney()
term.setBackgroundColor(8)  
term.clear()
term.setBackgroundColor(8)
term.setTextColor(colors.black)
term.setCursorPos(1,1)
print(statText)
term.setCursorPos(1,3)
print("You have: "..credits.." credits")
term.setCursorPos(1,5)
term.clearLine()
term.setCursorPos(1, 5)
print("		   ")
term.setCursorPos(1, 5)
print("Won: "..won)
end

-- User interaction --

while true do

local event, key = os.pullEvent("key")
if event == "key" then
  if key == 57 then -- key == 57 means key = spacebar.
   if credits < 1 then
   term.clear()
   term.setCursorPos(1, 1)
   print("Not enough money. Ending game")
   sleep(1)
   error()
   elseif credits >= 1 then
   credits = credits-1
   spin = spin + 1
  end
   if spin >= 5 then
   numb2 = 50
   else
	 number = math.random(numb2)
	   if number == 1 then
		term.setCursorPos(1, 5)
		term.clearLine()
		 print("Won: 10")
		 won = 10
		 credits = credits + won
		 printMoney()
	   elseif number == 2 then
		term.setCursorPos(1, 5)
		term.clearLine()
		 print("Won: 10")
		 won = 10
		 credits = credits + won
		 printMoney()
elseif number == 3 then
term.setCursorPos(1, 5)
		term.clearLine()
		 print("Won: 10")
		 won = 10
		 credits = credits + won
		 printMoney()
		 elseif number == 4 then
		  term.setCursorPos(1, 5)
		term.clearLine()
		 print("Won: 10")
		 won = 10
		 credits = credits + won
		 printMoney()
		 elseif number == 5 then
		  term.setCursorPos(1, 5)
		term.clearLine()
		 print("Won: 20")
		 won = 20
		 credits = credits + won
		 printMoney()
		elseif number == 6 then
		term.setCursorPos(1, 5)
		term.clearLine()
		 print("Won: 50")
		 won = 50
		 credits = credits + won
		 printMoney()
		 elseif number == 7 then
		  term.setCursorPos(1, 5)
		term.clearLine()
		  print("Won: 100")
		  won = 100
		 credits = credits + won
		  printMoney()
		 elseif number == 8 then
		  term.setCursorPos(1, 5)
		term.clearLine()
		  print("Won: 500")
		  won = 500
		 credits = credits + won
		  printMoney()
		 elseif number == 36 then
		  term.setCursorPos(1, 5)
		term.clearLine()
		  print("Won: 1000")
		  won = 1000
		 credits = credits + won
		 printMoney()
		else
		term.setCursorPos(1, 5)
		term.clearLine()
		print("Won: 0")
		printMoney()
		won = 0
		end
	end
end
end
start()
end
end

function gameHelp()
term.clear()
term.setCursorPos(1, 1)
print("Nothing here for now.")
print(" Press arrow up to exit")

end

function menu1()
if selected == "1" then
term.clear()
paintutils.drawLine(1, 1, w, 1, 128)
term.setTextColor(colors.white)
term.setCursorPos(math.floor(w-string.len("Slots Version: "..version))/2, 1)
print("Slots Version: "..version)
paintutils.drawLine(1, h, w, h, 128)
term.setTextColor(colors.white)
term.setCursorPos(w2, h)
print("Made by: Mackan90096")
term.setBackgroundColor(colors.white)
term.setTextColor(colors.gray)
term.setCursorPos(math.floor(w-string.len("[Play]"))/2, 4)
print("[Play]")
term.setCursorPos(math.floor(w-string.len("Help"))/2, 6)
print("Help")
term.setCursorPos(math.floor(w-string.len("About"))/2, 8)
print("About")
elseif selected == "2" then
term.clear()
paintutils.drawLine(1, 1, w, 1, 128)
term.setTextColor(colors.white)
term.setCursorPos(math.floor(w-string.len("Slots Version: "..version))/2, 1)
print("Slots Version: "..version)
paintutils.drawLine(1, h, w, h, 128)
term.setTextColor(colors.white)
term.setCursorPos(w2, h)
print("Made by: Mackan90096")
term.setBackgroundColor(colors.white)
term.setTextColor(colors.gray)
term.setCursorPos(math.floor(w-string.len("Play"))/2, 4)
print("Play")
term.setCursorPos(math.floor(w-string.len("[Help]"))/2, 6)
print("[Help]")
term.setCursorPos(math.floor(w-string.len("About"))/2, 8)
print("About")
elseif selected == "3" then
term.clear()
paintutils.drawLine(1, 1, w, 1, 128)
term.setTextColor(colors.white)
term.setCursorPos(math.floor(w-string.len("Slots Version: "..version))/2, 1)
print("Slots Version: "..version)
paintutils.drawLine(1, h, w, h, 128)
term.setTextColor(colors.white)
term.setCursorPos(w2, h)
print("Made by: Mackan90096")
term.setBackgroundColor(colors.white)
term.setTextColor(colors.gray)
term.setCursorPos(math.floor(w-string.len("Play"))/2, 4)
print("Play")
term.setCursorPos(math.floor(w-string.len("Help"))/2, 6)
print("Help")
term.setCursorPos(math.floor(w-string.len("[About]"))/2, 8)
print("[About]")
end
end

menu1()

-- End Functions --

-- User Interaction --
while true do
event, key = os.pullEvent("key")
if event == "key" then
if key == 208 then
if selected == "1" then selected = "2"
menu1()
   elseif selected == "2" then selected = "3"
	menu1()
   end
end
if key == 200 then
   if selected == "3" then selected = "2"
	menu1()
   elseif selected == "2" then selected = "1"
	menu1()
end
   end
		end
		if key == 28 then
		 if selected == "1" then
		 game()
		 elseif selected == "2" then
		 gameHelp()
		 end
		end
	end

To do:

SpoilerAdd cash out
Add save/load

Or pastebin: yAzu0mqX

Screenshots: http://imgur.com/a/kVa90
svdragster #2
Posted 03 May 2013 - 08:13 AM
Cool!
Mackan90096 #3
Posted 05 May 2013 - 06:01 AM
Thanks