Python Text-based Game: Longgong

This tutorial tackles creating a simple Text-based Game using Python 3. The game is entitled Longgong. This game is popular in carnivals/festival in the Philippines that uses 3 dice. The dealer will roll the dice and player will bet on the outcome of the dice.

Imports:

First we need to declare our imports.
  1. import random
  2. import time
We use random for the computer to generate the result of the dice for us and time for us to set a little bit of pause.

Initial Money

  1. money = 10;

Game Sequence

Next, we set up the order of events of our game.
  1. intro()
  2. winning()
  3. start()
  4. checkmoney(money)
Later, you'll find out why we pass our money variable to our checkmoney() function.

intro() and winning() functions

  1. def intro():
  2.     print('Introduction:')
  3.     print('')
  4.     print('LONGGONG is a dice gamble game in the Philippines.')
  5.     print('It is mostly played in carnivals/festivals but due to the use of illegal dice,')
  6.     print('these gamble game is rarely seen in carnivals/festivals nowadays.')
  7.     print('')
  8.     print('The player will bet on a number from 1-6 then the dealer will roll the 3 dice.')
  9.     print('If the number comes out in either of the three dice, the player wins.')
  10.     print('Your goal is to make your money greater than or equal to 100.')
  11.     print('')
  12.    
  13. def winning():
  14.     print('Winnings:')
  15.     print('If your number comes out in one of the three dice, you win the amount you bet')
  16.     print('If your number comes out in two dice, you win double')
  17.     print('If the number in 3 of the dice is your number, you win triple.')
  18.     print('If your number is neither of the 3 dice, you lose your bet.')
  19.     print('')

start() function

This is where the player will input his/her name to start the game.
  1. def start():
  2.     name = input('Please input name: ')
  3.     print('')
  4.     print('Welcome to Longgong,',name,'!')
  5.     print('Your starting money is',money)
  6.     print('')

checkmoney(money)

This function checks the money of the player every after bet. We pass our money variable so that it can be processed within the function.
  1. def checkmoney(money):
  2.     if money == 0:
  3.         print('You run out of money.')
  4.         print('Game will exit in 3 seconds...')
  5.         time.sleep(3)
  6.         exit()
  7.     elif money >= 100:
  8.         print('Congatulations! You have achieve the goal')
  9.         print('Game will exit in 3 seconds...')
  10.         time.sleep(3)
  11.         exit()
  12.     else:
  13.         betting(money)

betting(money)

This is called whenever the player's money doesn't reach zero or doesn't achieve the goal of 100. At the end of the function, it will call our checkmoney() to check the money of the player.
  1. def betting(money):
  2.     bstate = True
  3.     while bstate:
  4.         bet = input('Place bet: ')
  5.         if bet.isdigit():
  6.             if int(bet) < 1 or int(bet) > money:
  7.                 print('Invalid bet. Please bet an amount and not greater than',money)
  8.                 bstate = True
  9.             else:
  10.                 fbet = int(bet)
  11.                 bstate = False
  12.         elif bet.isalpha():
  13.             print('Please input integer only')
  14.             bstate = True
  15.            
  16.     nstate = True
  17.     while nstate:
  18.         number = input('Your number: ')
  19.         if number.isdigit():
  20.             if int(number) < 1 or int(number) > 6:
  21.                 print('Number should between 1-6.')
  22.                 nstate = True
  23.             else:
  24.                 fnumber = int(number)
  25.                 nstate = False
  26.         elif number.isalpha():
  27.             print('Please input integer only')
  28.             nstate = True
  29.        
  30.     die1=random.randint(1,6)
  31.     die2=random.randint(1,6)
  32.     die3=random.randint(1,6)
  33.  
  34.     print('')
  35.     print('You bet',fbet,'money on number',fnumber)
  36.     print('')
  37.     print('The dealer is rolling the 3 dice...')
  38.     time.sleep(3)
  39.     print('The results:')
  40.     time.sleep(1)
  41.     print('First Die: ',die1)
  42.     time.sleep(1)
  43.     print('Second Die: ',die2)
  44.     time.sleep(1)
  45.     print('Third Die: ',die3)
  46.     time.sleep(1)
  47.     print('')
  48.  
  49.     win = 0
  50.  
  51.     if die1==fnumber:
  52.         win += fbet
  53.     if die2==fnumber:
  54.         win += fbet
  55.     if die3==fnumber:
  56.         win += fbet
  57.  
  58.     if win == 0:
  59.         money -= fbet
  60.         print('Your number didnt come out in either of the three dice')
  61.         print('You lose your bet of',fbet)
  62.         print('You now have',money)
  63.         print('')
  64.     else:
  65.         money += fbet
  66.         print('Congrats! You won',win)
  67.         print('You now have',money)
  68.         print('')
  69.        
  70.     checkmoney(money)

Whole Code

  1. import random
  2. import time
  3.  
  4. money = 10;
  5.  
  6. def intro():
  7.     print('Introduction:')
  8.     print('')
  9.     print('LONGGONG is a dice gamble game in the Philippines.')
  10.     print('It is mostly played in carnivals/festivals but due to the use of illegal dice,')
  11.     print('these gamble game is rarely seen in carnivals/festivals nowadays.')
  12.     print('')
  13.     print('The player will bet on a number from 1-6 then the dealer will roll the 3 dice.')
  14.     print('If the number comes out in either of the three dice, the player wins.')
  15.     print('Your goal is to make your money greater than or equal to 100.')
  16.     print('')
  17.    
  18. def winning():
  19.     print('Winnings:')
  20.     print('If your number comes out in one of the three dice, you win the amount you bet')
  21.     print('If your number comes out in two dice, you win double')
  22.     print('If the number in 3 of the dice is your number, you win triple.')
  23.     print('If your number is neither of the 3 dice, you lose your bet.')
  24.     print('')
  25.  
  26. def start():
  27.     name = input('Please input name: ')
  28.     print('')
  29.     print('Welcome to Longgong,',name,'!')
  30.     print('Your starting money is',money)
  31.     print('')
  32.  
  33. def checkmoney(money):
  34.     if money == 0:
  35.         print('You run out of money.')
  36.         print('Game will exit in 3 seconds...')
  37.         time.sleep(3)
  38.         exit()
  39.     elif money >= 100:
  40.         print('Congatulations! You have achieve the goal')
  41.         print('Game will exit in 3 seconds...')
  42.         time.sleep(3)
  43.         exit()
  44.     else:
  45.         betting(money)  
  46.  
  47. def betting(money):
  48.     bstate = True
  49.     while bstate:
  50.         bet = input('Place bet: ')
  51.         if bet.isdigit():
  52.             if int(bet) < 1 or int(bet) > money:
  53.                 print('Invalid bet. Please bet an amount and not greater than',money)
  54.                 bstate = True
  55.             else:
  56.                 fbet = int(bet)
  57.                 bstate = False
  58.         elif bet.isalpha():
  59.             print('Please input integer only')
  60.             bstate = True
  61.            
  62.     nstate = True
  63.     while nstate:
  64.         number = input('Your number: ')
  65.         if number.isdigit():
  66.             if int(number) < 1 or int(number) > 6:
  67.                 print('Number should between 1-6.')
  68.                 nstate = True
  69.             else:
  70.                 fnumber = int(number)
  71.                 nstate = False
  72.         elif number.isalpha():
  73.             print('Please input integer only')
  74.             nstate = True
  75.        
  76.     die1=random.randint(1,6)
  77.     die2=random.randint(1,6)
  78.     die3=random.randint(1,6)
  79.  
  80.     print('')
  81.     print('You bet',fbet,'money on number',fnumber)
  82.     print('')
  83.     print('The dealer is rolling the 3 dice...')
  84.     time.sleep(3)
  85.     print('The results:')
  86.     time.sleep(1)
  87.     print('First Die: ',die1)
  88.     time.sleep(1)
  89.     print('Second Die: ',die2)
  90.     time.sleep(1)
  91.     print('Third Die: ',die3)
  92.     time.sleep(1)
  93.     print('')
  94.  
  95.     win = 0
  96.  
  97.     if die1==fnumber:
  98.         win += fbet
  99.     if die2==fnumber:
  100.         win += fbet
  101.     if die3==fnumber:
  102.         win += fbet
  103.  
  104.     if win == 0:
  105.         money -= fbet
  106.         print('Your number didnt come out in either of the three dice')
  107.         print('You lose your bet of',fbet)
  108.         print('You now have',money)
  109.         print('')
  110.     else:
  111.         money += fbet
  112.         print('Congrats! You won',win)
  113.         print('You now have',money)
  114.         print('')
  115.        
  116.     checkmoney(money)
  117.  
  118. intro()
  119. winning()
  120. start()
  121. checkmoney(money)
That's the end of this tutorial. If you have any question or comments, feel free to write it below or message me. Happy Coding. P.S. If you happen to clear the game by making your money to 100 or higher, screenshot your whole game to me and I'll give you a reward.

Add new comment