Created
April 3, 2023 08:34
-
-
Save codepedia-top/f096f5c57c5b66abd226fc6a010cfb47 to your computer and use it in GitHub Desktop.
truth or dare game with python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| from urllib.request import urlopen | |
| import random | |
| def print_logo(): printline("""Truth or Dare""") | |
| def printline(msg): print(f"\n{msg}\n") | |
| class Color: | |
| def colors_256(color_, msg=""): | |
| return(f"\033[38;5;{str(color_)}m {msg} \033[0;0m") | |
| def red(msg): | |
| return Color.colors_256(9, msg) | |
| def blue(msg): | |
| return Color.colors_256(6, msg) | |
| def yellow(msg): | |
| return Color.colors_256(11, msg) | |
| def green(msg): | |
| return Color.colors_256(10, msg) | |
| messages = { | |
| 'TRUTH': Color.yellow("""truth | |
| """), | |
| 'DARE': Color.yellow("""DARE | |
| """), | |
| 'NO_OF_PLAYER': Color.blue("Enter number of players: "), | |
| 'INPUT_COMMAND': Color.blue("""\nPlease enter your choice\n1) Truth \n2) Dare \n9) Restart Game\n0) End Game\n>"""), | |
| } | |
| truth_dare_url = 'https://gist.githubusercontent.com/deepakshrma/9498a19a3ed460fc662c536d138c29b1/raw/f29d323b9b3f0a82f66ed58c7117fb9b599fb8d5/truth-n-dare.json' | |
| def num(i): | |
| try: | |
| i = int(i) | |
| except: | |
| i = 0 | |
| return i | |
| def requestJson(url, default=[]): | |
| data = default | |
| try: | |
| data = json.load(urlopen(truth_dare_url)) | |
| except: | |
| print("Error while fetching data...") | |
| return data | |
| def main(): | |
| print_logo() | |
| # Request truth_dare json data | |
| truth_dare_list = requestJson(truth_dare_url) | |
| # Parse and create list of truth and dare | |
| truth_list = [ | |
| item | |
| for item in truth_dare_list | |
| if item["type"] == "Truth" | |
| ] | |
| dare_list = [ | |
| item | |
| for item in truth_dare_list | |
| if item["type"] == "Dare" | |
| ] | |
| # Take number of players | |
| number_of_player = int(input(Color.blue("Enter number of players: "))) | |
| print(Color.green( | |
| f"{number_of_player} players are playing this game")) | |
| # Current Player | |
| current_player = 1 | |
| while True: | |
| print(Color.blue(f"PLAYER NO:: {current_player}")) | |
| choice_list = truth_list | |
| # Take User input(choice) | |
| command = num( | |
| input(messages["INPUT_COMMAND"])) | |
| # If exit, end the game | |
| if command == 0: | |
| break | |
| # On user select TRUTH | |
| if command == 1: | |
| print(messages["TRUTH"]) | |
| # On user select Dare | |
| elif command == 2: | |
| print(messages["DARE"]) | |
| choice_list = dare_list | |
| # Guess question from list | |
| ran = random.randint(0, len(choice_list)-1) | |
| question = choice_list[ran]["summary"] | |
| print(Color.green(f"\n{question}\n\n")) | |
| # Change player | |
| current_player = 1 if current_player >= number_of_player else current_player + 1 | |
| print(Color.red("Thanks for playing Game TRUTH & DARE")) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment