Python Text-based Game: Longgong
Submitted by nurhodelta_17 on Saturday, October 14, 2017 - 16:20.
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.
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.
Later, you'll find out why we pass our money variable to our checkmoney() function.
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.
Imports:
First we need to declare our imports.- import random
- import time
Initial Money
- money = 10;
Game Sequence
Next, we set up the order of events of our game.- intro()
- winning()
- start()
- checkmoney(money)
intro() and winning() functions
- def intro():
- print('Introduction:')
- print('')
- print('LONGGONG is a dice gamble game in the Philippines.')
- print('It is mostly played in carnivals/festivals but due to the use of illegal dice,')
- print('these gamble game is rarely seen in carnivals/festivals nowadays.')
- print('')
- print('The player will bet on a number from 1-6 then the dealer will roll the 3 dice.')
- print('If the number comes out in either of the three dice, the player wins.')
- print('Your goal is to make your money greater than or equal to 100.')
- print('')
- def winning():
- print('Winnings:')
- print('If your number comes out in one of the three dice, you win the amount you bet')
- print('If your number comes out in two dice, you win double')
- print('If the number in 3 of the dice is your number, you win triple.')
- print('If your number is neither of the 3 dice, you lose your bet.')
- print('')
start() function
This is where the player will input his/her name to start the game.- def start():
- name = input('Please input name: ')
- print('')
- print('Welcome to Longgong,',name,'!')
- print('Your starting money is',money)
- 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.- def checkmoney(money):
- if money == 0:
- print('You run out of money.')
- print('Game will exit in 3 seconds...')
- time.sleep(3)
- exit()
- elif money >= 100:
- print('Congatulations! You have achieve the goal')
- print('Game will exit in 3 seconds...')
- time.sleep(3)
- exit()
- else:
- 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.- def betting(money):
- bstate = True
- while bstate:
- bet = input('Place bet: ')
- if bet.isdigit():
- if int(bet) < 1 or int(bet) > money:
- print('Invalid bet. Please bet an amount and not greater than',money)
- bstate = True
- else:
- fbet = int(bet)
- bstate = False
- elif bet.isalpha():
- print('Please input integer only')
- bstate = True
- nstate = True
- while nstate:
- number = input('Your number: ')
- if number.isdigit():
- if int(number) < 1 or int(number) > 6:
- print('Number should between 1-6.')
- nstate = True
- else:
- fnumber = int(number)
- nstate = False
- elif number.isalpha():
- print('Please input integer only')
- nstate = True
- die1=random.randint(1,6)
- die2=random.randint(1,6)
- die3=random.randint(1,6)
- print('')
- print('You bet',fbet,'money on number',fnumber)
- print('')
- print('The dealer is rolling the 3 dice...')
- time.sleep(3)
- print('The results:')
- time.sleep(1)
- print('First Die: ',die1)
- time.sleep(1)
- print('Second Die: ',die2)
- time.sleep(1)
- print('Third Die: ',die3)
- time.sleep(1)
- print('')
- win = 0
- if die1==fnumber:
- win += fbet
- if die2==fnumber:
- win += fbet
- if die3==fnumber:
- win += fbet
- if win == 0:
- money -= fbet
- print('Your number didnt come out in either of the three dice')
- print('You lose your bet of',fbet)
- print('You now have',money)
- print('')
- else:
- money += fbet
- print('Congrats! You won',win)
- print('You now have',money)
- print('')
- checkmoney(money)
Whole Code
- import random
- import time
- money = 10;
- def intro():
- print('Introduction:')
- print('')
- print('LONGGONG is a dice gamble game in the Philippines.')
- print('It is mostly played in carnivals/festivals but due to the use of illegal dice,')
- print('these gamble game is rarely seen in carnivals/festivals nowadays.')
- print('')
- print('The player will bet on a number from 1-6 then the dealer will roll the 3 dice.')
- print('If the number comes out in either of the three dice, the player wins.')
- print('Your goal is to make your money greater than or equal to 100.')
- print('')
- def winning():
- print('Winnings:')
- print('If your number comes out in one of the three dice, you win the amount you bet')
- print('If your number comes out in two dice, you win double')
- print('If the number in 3 of the dice is your number, you win triple.')
- print('If your number is neither of the 3 dice, you lose your bet.')
- print('')
- def start():
- name = input('Please input name: ')
- print('')
- print('Welcome to Longgong,',name,'!')
- print('Your starting money is',money)
- print('')
- def checkmoney(money):
- if money == 0:
- print('You run out of money.')
- print('Game will exit in 3 seconds...')
- time.sleep(3)
- exit()
- elif money >= 100:
- print('Congatulations! You have achieve the goal')
- print('Game will exit in 3 seconds...')
- time.sleep(3)
- exit()
- else:
- betting(money)
- def betting(money):
- bstate = True
- while bstate:
- bet = input('Place bet: ')
- if bet.isdigit():
- if int(bet) < 1 or int(bet) > money:
- print('Invalid bet. Please bet an amount and not greater than',money)
- bstate = True
- else:
- fbet = int(bet)
- bstate = False
- elif bet.isalpha():
- print('Please input integer only')
- bstate = True
- nstate = True
- while nstate:
- number = input('Your number: ')
- if number.isdigit():
- if int(number) < 1 or int(number) > 6:
- print('Number should between 1-6.')
- nstate = True
- else:
- fnumber = int(number)
- nstate = False
- elif number.isalpha():
- print('Please input integer only')
- nstate = True
- die1=random.randint(1,6)
- die2=random.randint(1,6)
- die3=random.randint(1,6)
- print('')
- print('You bet',fbet,'money on number',fnumber)
- print('')
- print('The dealer is rolling the 3 dice...')
- time.sleep(3)
- print('The results:')
- time.sleep(1)
- print('First Die: ',die1)
- time.sleep(1)
- print('Second Die: ',die2)
- time.sleep(1)
- print('Third Die: ',die3)
- time.sleep(1)
- print('')
- win = 0
- if die1==fnumber:
- win += fbet
- if die2==fnumber:
- win += fbet
- if die3==fnumber:
- win += fbet
- if win == 0:
- money -= fbet
- print('Your number didnt come out in either of the three dice')
- print('You lose your bet of',fbet)
- print('You now have',money)
- print('')
- else:
- money += fbet
- print('Congrats! You won',win)
- print('You now have',money)
- print('')
- checkmoney(money)
- intro()
- winning()
- start()
- checkmoney(money)
Add new comment
- 419 views