def ensure_data_directory():
"""Ensure the data directory exists."""
if not os.path.exists('data'):
os.makedirs('data')
def load_user_data():
"""Load user data from a file."""
if not os.path.exists(USER_DATA_FILE):
return {}
code
with open(USER_DATA_FILE, 'r') as file:
user_data = {}
for line in file:
username = line.strip()
user_data[username] = True # Using a dictionary to track registered users
return user_data
def register_user(username):
"""Register a new user."""
with open(USER_DATA_FILE, 'a') as file:
file.write(username + '\n')
def user_login():
"""Handle user login or registration."""
user_data = load_user_data()
code
Assistant
User
Assistant
User
Assistant
while True:
try:
print("Login (l) or Register (r): ", end='', flush=True)
choice = sys.stdin.readline().strip().lower() # Using sys.stdin.readline()
print("Enter your username: ", end='', flush=True)
username = sys.stdin.readline().strip() # Using sys.stdin.readline()
if choice == 'l':
if username in user_data:
print(f"Welcome back, {username}!")
return username
else:
print("Username does not exist. Please register.")
elif choice == 'r':
if username not in user_data:
register_user(username)
print(f"User {username} registered successfully!")
return username
else:
print("Username already exists. Please choose a different one.")
else:
print("Invalid choice. Please enter 'l' for Login or 'r' for Register.")
except EOFError:
print("\nInput error: Please ensure you're running this in an interactive console.")
break
except KeyboardInterrupt:
print("\nInput interrupted. Exiting user login.")
break
except Exception as e:
print(f"An error occurred: {e}")
break
def load_high_scores():
"""Load high scores from a file."""
if not os.path.exists(HIGH_SCORES_FILE):
return {}
code
with open(HIGH_SCORES_FILE, 'r') as file:
high_scores = {}
for line in file:
difficulty, name, score = line.strip().split(',')
high_scores[difficulty] = (name, int(score))
return high_scores
def save_high_score(name, score, difficulty):
"""Save player's name and score to a file."""
ensure_data_directory() # Ensure data directory exists
with open(HIGH_SCORES_FILE, 'a') as file:
file.write(f"{difficulty},{name},{score}\n")
def get_word(difficulty):
"""Select a random word from a given difficulty level."""
return random.choice(DIFFICULTY_WORDS[difficulty])
def display_progress(selected_word, guessed_letters):
"""Display the current state of the guessed word."""
display_word = ''
for letter in selected_word:
if letter in guessed_letters:
display_word += letter + ' '
else:
display_word += '_ '
print("\nCurrent word:", display_word.strip())
def guess_the_word(username):
"""Guess the word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Guess the Word'!")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
code
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
if remaining_tries <= 0:
print(f"Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Zero score if they fail to guess
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def display_shark(shark):
"""Display the shark's position."""
print("Shark's position:", shark)
def beat_the_shark(username):
"""A simple race game where player guesses letters and compares to a shark opponent."""
while True: # Loop for playing the game until user decides to exit
print("\nWelcome to 'Beat the Shark'!")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
code
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
# Initialize shark string based on difficulty
if difficulty == 'easy':
shark = "﹏﹏﹏𓂁" # 3 underscores for easy
elif difficulty == 'medium':
shark = "﹏﹏﹏﹏﹏𓂁" # 5 underscores for medium
else:
shark = "﹏﹏﹏﹏﹏﹏﹏﹏𓂁" # 7 underscores for hard
# Display initial shark position
display_shark(shark)
while remaining_tries > 0: # The loop continues as long as there are remaining tries
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess! The shark is still behind!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points. The shark moves forward!")
# Remove one '﹏' from the shark to show it moving closer
shark = shark[:-1] # Slice off the last character
if len(shark) <= 1: # If we only have the shark emoji left
shark = '𓂁' # Replace with only the shark emoji
# Display updated shark position
display_shark(shark)
# If the player successfully guesses all letters in the word, they win
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! The shark couldn't catch you!") # Show word on win
break
# If player runs out of tries, the shark catches them
if remaining_tries <= 0:
print(f"The shark caught you! Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Score is zero if caught
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def timed_guess_the_word(username):
"""Timed Guess the Word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Timed Guess the Word'!")
print("You have infinite guesses but must guess the word within the time limit.")
code
# Select time limit
while True:
print("Choose a time limit:")
print("1. 30 seconds")
print("2. 1 minute (60 seconds)")
print("3. 1 minute and 30 seconds (90 seconds)")
time_choice = input("Select a time limit (1, 2, 3): ").strip()
if time_choice == '1':
timer_duration = 30
break
elif time_choice == '2':
timer_duration = 60
break
elif time_choice == '3':
timer_duration = 90
break
else:
print("Invalid choice. Please select 1, 2, or 3.")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty]
start_time = time.time() # Start the timer
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
elapsed_time = time.time() - start_time # Calculate elapsed time
remaining_time = max(0, timer_duration - elapsed_time) # Calculate remaining time (cannot be negative)
print(f"Time remaining: {int(remaining_time)} seconds")
if remaining_time <= 0:
print(f"Time's up! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0
break
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
save_high_score(username, score, 'timed') # Save high score for timed game
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def display_high_scores(high_scores):
"""Display the high scores."""
print("\nHigh Scores:")
for difficulty in DIFFICULTY_WORDS.keys():
if difficulty in high_scores:
name, score = high_scores[difficulty]
print(f"High Score [Difficulty: {difficulty}]: {name} with {score} points")
else:
print(f"High Score [Difficulty: {difficulty}]: No scores yet")
def start_menu():
"""Display the start menu and handle user choices."""
while True: # Loop for displaying the main menu
print("Pick a game mode!")
print("1. Guess the Word: Earn points by guessing the correct letters.")
print("2. Beat the Shark: Race against a shark by guessing the word correctly.")
print("3. Timed Guess the Word: Infinite guesses but beat the timer!")
try:
choice = input("Select a game (1, 2, or 3), or 'q' to quit: ")
code
if choice == '1':
guess_the_word(username)
elif choice == '2':
beat_the_shark(username)
elif choice == '3':
timed_guess_the_word(username)
elif choice.lower() == 'q':
print("Thank you for playing! Goodbye!")
break
else:
print("Invalid choice. Please try again.")
except KeyboardInterrupt:
print("\nInput interrupted. Exiting the game.")
break
Load high scores at the start
high_scores = load_high_scores()
Display high scores if available
display_high_scores(high_scores)
Load users and handle login/registration
username = user_login()
Start the game
if name == "main":
start_menu() how would i make the easy medium and hard diffrent colours make easy green medium yellow hard red
To implement color coding for the different difficulty levels (easy, medium, and hard) in your Python game, you'll want to use ANSI escape sequences. These escape sequences allow you to control text colors in terminal outputs.
Here's how you can modify your code to include color formatting for the different difficulty levels:
Step 1: Define Colors
You can define color codes at the beginning of your code. For example, you can define green for easy, yellow for medium, and red for hard:
python
# Define color codes
COLOR_GREEN = "\033[92m" # Bright green
COLOR_YELLOW = "\033[93m" # Bright yellow
COLOR_RED = "\033[91m" # Bright red
COLOR_RESET = "\033[0m" # Reset color
Step 2: Apply Colors to Difficulty Levels
Whenever you ask for the difficulty level or display it, you can use the defined color codes. Here’s an example of how you might integrate this into your existing input request and display functions.
Modify your guess_the_word, beat_the_shark, and timed_guess_the_word functions to include the color coding when displaying or processing the difficulty:
python
def guess_the_word(username):
"""Guess the word game."""
while True:
print("Welcome to 'Guess the Word'!")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
# Rest of your code...
Similarly, update the other functions beat_the_shark and timed_guess_the_word to include the colored difficulty prompts.
Step 3: Displaying High Scores with Colors
When displaying high scores, you may want to add colors as well:
python
def display_high_scores(high_scores):
"""Display the high scores."""
print("\nHigh Scores:")
for difficulty in DIFFICULTY_WORDS.keys():
if difficulty in high_scores:
name, score = high_scores[difficulty]
color = COLOR_GREEN if difficulty == 'easy' else COLOR_YELLOW if difficulty == 'medium' else COLOR_RED
print(f"High Score [Difficulty: {color}{difficulty}{COLOR_RESET}]: {name} with {score} points")
else:
color = COLOR_GREEN if difficulty == 'easy' else COLOR_YELLOW if difficulty == 'medium' else COLOR_RED
print(f"High Score [Difficulty: {color}{difficulty}{COLOR_RESET}]: No scores yet")
Complete Simple Example
Here's a simple consolidated example just for demonstration purposes:
python
# Define color codes
COLOR_GREEN = "\033[92m" # Bright green
COLOR_YELLOW = "\033[93m" # Bright yellow
COLOR_RED = "\033[91m" # Bright red
COLOR_RESET = "\033[0m" # Reset color
def show_difficulty_prompt():
"""Show difficulty prompt with colors."""
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
return difficulty
# Example usage
difficulty = show_difficulty_prompt()
# Continue with your game logic using the validated difficulty...
Important Note
Make sure your terminal supports ANSI escape codes (which most modern terminals do). If you're using Windows, you might need to enable ANSI support or use a library like colorama to ensure proper color handling.
Conclusion
This approach will provide a visually appealing interface for your game, clearly distinguishing each difficulty level by color, making the game more engaging for players.
import random
import os
import sys
import time # Import time for the timer functionality
def ensure_data_directory():
"""Ensure the data directory exists."""
if not os.path.exists('data'):
os.makedirs('data')
def load_user_data():
"""Load user data from a file."""
if not os.path.exists(USER_DATA_FILE):
return {}
code
with open(USER_DATA_FILE, 'r') as file:
user_data = {}
for line in file:
username = line.strip()
user_data[username] = True # Using a dictionary to track registered users
return user_data
def register_user(username):
"""Register a new user."""
with open(USER_DATA_FILE, 'a') as file:
file.write(username + '\n')
def user_login():
"""Handle user login or registration."""
user_data = load_user_data()
code
while True:
try:
print("Login (l) or Register (r): ", end='', flush=True)
choice = sys.stdin.readline().strip().lower() # Using sys.stdin.readline()
print("Enter your username: ", end='', flush=True)
username = sys.stdin.readline().strip() # Using sys.stdin.readline()
if choice == 'l':
if username in user_data:
print(f"Welcome back, {username}!")
return username
else:
print("Username does not exist. Please register.")
elif choice == 'r':
if username not in user_data:
register_user(username)
print(f"User {username} registered successfully!")
return username
else:
print("Username already exists. Please choose a different one.")
else:
print("Invalid choice. Please enter 'l' for Login or 'r' for Register.")
except EOFError:
print("\nInput error: Please ensure you're running this in an interactive console.")
break
except KeyboardInterrupt:
print("\nInput interrupted. Exiting user login.")
break
except Exception as e:
print(f"An error occurred: {e}")
break
def load_high_scores():
"""Load high scores from a file."""
if not os.path.exists(HIGH_SCORES_FILE):
return {}
code
with open(HIGH_SCORES_FILE, 'r') as file:
high_scores = {}
for line in file:
difficulty, name, score = line.strip().split(',')
high_scores[difficulty] = (name, int(score))
return high_scores
def save_high_score(name, score, difficulty):
"""Save player's name and score to a file."""
ensure_data_directory() # Ensure data directory exists
with open(HIGH_SCORES_FILE, 'a') as file:
file.write(f"{difficulty},{name},{score}\n")
def get_word(difficulty):
"""Select a random word from a given difficulty level."""
return random.choice(DIFFICULTY_WORDS[difficulty])
def display_progress(selected_word, guessed_letters):
"""Display the current state of the guessed word."""
display_word = ''
for letter in selected_word:
if letter in guessed_letters:
display_word += letter + ' '
else:
display_word += '_ '
print("\nCurrent word:", display_word.strip())
def guess_the_word(username):
"""Guess the word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Guess the Word'!")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
code
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
if remaining_tries <= 0:
print(f"Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Zero score if they fail to guess
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def display_shark(shark):
"""Display the shark's position."""
print("Shark's position:", shark)
def beat_the_shark(username):
"""A simple race game where player guesses letters and compares to a shark opponent."""
while True: # Loop for playing the game until user decides to exit
print("\nWelcome to 'Beat the Shark'!")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
code
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
# Initialize shark string based on difficulty
if difficulty == 'easy':
shark = "﹏﹏﹏𓂁" # 3 underscores for easy
elif difficulty == 'medium':
shark = "﹏﹏﹏﹏﹏𓂁" # 5 underscores for medium
else:
shark = "﹏﹏﹏﹏﹏﹏﹏﹏𓂁" # 7 underscores for hard
# Display initial shark position
display_shark(shark)
while remaining_tries > 0: # The loop continues as long as there are remaining tries
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess! The shark is still behind!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points. The shark moves forward!")
# Remove one '﹏' from the shark to show it moving closer
shark = shark[:-1] # Slice off the last character
if len(shark) <= 1: # If we only have the shark emoji left
shark = '𓂁' # Replace with only the shark emoji
# Display updated shark position
display_shark(shark)
# If the player successfully guesses all letters in the word, they win
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! The shark couldn't catch you!") # Show word on win
break
# If player runs out of tries, the shark catches them
if remaining_tries <= 0:
print(f"The shark caught you! Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Score is zero if caught
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def timed_guess_the_word(username):
"""Timed Guess the Word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Timed Guess the Word'!")
print("You have infinite guesses but must guess the word within the time limit.")
code
# Select time limit
while True:
print("Choose a time limit:")
print("1. 30 seconds")
print("2. 1 minute (60 seconds)")
print("3. 1 minute and 30 seconds (90 seconds)")
time_choice = input("Select a time limit (1, 2, 3): ").strip()
if time_choice == '1':
timer_duration = 30
break
elif time_choice == '2':
timer_duration = 60
break
elif time_choice == '3':
timer_duration = 90
break
else:
print("Invalid choice. Please select 1, 2, or 3.")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty]
start_time = time.time() # Start the timer
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
elapsed_time = time.time() - start_time # Calculate elapsed time
remaining_time = max(0, timer_duration - elapsed_time) # Calculate remaining time (cannot be negative)
print(f"Time remaining: {int(remaining_time)} seconds")
if remaining_time <= 0:
print(f"Time's up! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0
break
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
save_high_score(username, score, 'timed') # Save high score for timed game
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def display_high_scores(high_scores):
"""Display the high scores."""
print("\nHigh Scores:")
for difficulty in DIFFICULTY_WORDS.keys():
if difficulty in high_scores:
name, score = high_scores[difficulty]
print(f"High Score [Difficulty: {difficulty}]: {name} with {score} points")
else:
print(f"High Score [Difficulty: {difficulty}]: No scores yet")
def start_menu():
"""Display the start menu and handle user choices."""
while True: # Loop for displaying the main menu
print("Pick a game mode!")
print("1. Guess the Word: Earn points by guessing the correct letters.")
print("2. Beat the Shark: Race against a shark by guessing the word correctly.")
print("3. Timed Guess the Word: Infinite guesses but beat the timer!")
try:
choice = input("Select a game (1, 2, or 3), or 'q' to quit: ")
code
if choice == '1':
guess_the_word(username)
elif choice == '2':
beat_the_shark(username)
elif choice == '3':
timed_guess_the_word(username)
elif choice.lower() == 'q':
print("Thank you for playing! Goodbye!")
break
else:
print("Invalid choice. Please try again.")
except KeyboardInterrupt:
print("\nInput interrupted. Exiting the game.")
break
Load high scores at the start
high_scores = load_high_scores()
Display high scores if available
display_high_scores(high_scores)
Load users and handle login/registration
username = user_login()
Start the game
if name == "main":
start_menu() make the easy green medium yellow hard red
To implement color coding for the difficulty levels (easy, medium, hard) in your Python script with ANSI escape codes, follow the steps below. This will allow your game to display the difficulty levels in green, yellow, and red, respectively.
Step 1: Define Color Codes
At the beginning of your script, define the color codes for the green, yellow, and red texts, as well as the reset code to revert to the default color.
python
# Define color codes
COLOR_GREEN = "\033[92m" # Bright green
COLOR_YELLOW = "\033[93m" # Bright yellow
COLOR_RED = "\033[91m" # Bright red
COLOR_RESET = "\033[0m" # Reset color
Step 2: Update Difficulty Input Prompts
Update the input prompts in your guess_the_word, beat_the_shark, and timed_guess_the_word functions to include the color codes. Here is how you can do it:
Update Example for guess_the_word Function:
You can easily apply this same change to other functions as well.
python
def guess_the_word(username):
"""Guess the word game."""
while True:
print("Welcome to 'Guess the Word'!")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
if remaining_tries <= 0:
print(f"Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Zero score if they fail to guess
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
Step 3: Update the Same for Other Functions
You need to make similar changes in the beat_the_shark and timed_guess_the_word functions, so that the difficulty prompt will display in the defined colors:
Example for beat_the_shark Function:
python
def beat_the_shark(username):
"""A simple race game where player guesses letters and compares to a shark opponent."""
while True:
print("\nWelcome to 'Beat the Shark'!")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
# Initialize shark string based on difficulty
if difficulty == 'easy':
shark = "﹏﹏﹏𓂁" # 3 underscores for easy
elif difficulty == 'medium':
shark = "﹏﹏﹏﹏﹏𓂁" # 5 underscores for medium
else:
shark = "﹏﹏﹏﹏﹏﹏﹏﹏𓂁" # 7 underscores for hard
# Display initial shark position
display_shark(shark)
while remaining_tries > 0: # The loop continues as long as there are remaining tries
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess! The shark is still behind!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points. The shark moves forward!")
# Remove one '﹏' from the shark to show it moving closer
shark = shark[:-1] # Slice off the last character
if len(shark) <= 1: # If we only have the shark emoji left
shark = '𓂁' # Replace with only the shark emoji
# Display updated shark position
display_shark(shark)
# If the player successfully guesses all letters in the word, they win
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! The shark couldn't catch you!") # Show word on win
break
# If player runs out of tries, the shark catches them
if remaining_tries <= 0:
print(f"The shark caught you! Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Score is zero if caught
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
Example for timed_guess_the_word Function:
python
def timed_guess_the_word(username):
"""Timed Guess the Word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Timed Guess the Word'!")
print("You have infinite guesses but must guess the word within the time limit.")
# Select time limit
while True:
print("Choose a time limit:")
print("1. 30 seconds")
print("2. 1 minute (60 seconds)")
print("3. 1 minute and 30 seconds (90 seconds)")
time_choice = input("Select a time limit (1, 2, 3): ").strip()
if time_choice == '1':
timer_duration = 30
break
elif time_choice == '2':
timer_duration = 60
break
elif time_choice == '3':
timer_duration = 90
break
else:
print("Invalid choice. Please select 1, 2, or 3.")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty]
start_time = time.time() # Start the timer
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
elapsed_time = time.time() - start_time # Calculate elapsed time
remaining_time = max(0, timer_duration - elapsed_time) # Calculate remaining time (cannot be negative)
print(f"Time remaining: {int(remaining_time)} seconds")
if remaining_time <= 0:
print(f"Time's up! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0
break
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
save_high_score(username, score, 'timed') # Save high score for timed game
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
Summary
With these changes, when players are prompted to select a difficulty level, they will see "easy" in green, "medium" in yellow, and "hard" in red. This gives a clearer visual representation, making the game more engaging.
The same approach can be employed in any part of the game where you want to emphasize text through color. Just wrap the desired text with the corresponding color codes.
no use this code import random
import os
import sys
import time # Import time for the timer functionality
def ensure_data_directory():
"""Ensure the data directory exists."""
if not os.path.exists('data'):
os.makedirs('data')
def load_user_data():
"""Load user data from a file."""
if not os.path.exists(USER_DATA_FILE):
return {}
code
with open(USER_DATA_FILE, 'r') as file:
user_data = {}
for line in file:
username = line.strip()
user_data[username] = True # Using a dictionary to track registered users
return user_data
def register_user(username):
"""Register a new user."""
with open(USER_DATA_FILE, 'a') as file:
file.write(username + '\n')
def user_login():
"""Handle user login or registration."""
user_data = load_user_data()
code
while True:
try:
print("Login (l) or Register (r): ", end='', flush=True)
choice = sys.stdin.readline().strip().lower() # Using sys.stdin.readline()
print("Enter your username: ", end='', flush=True)
username = sys.stdin.readline().strip() # Using sys.stdin.readline()
if choice == 'l':
if username in user_data:
print(f"Welcome back, {username}!")
return username
else:
print("Username does not exist. Please register.")
elif choice == 'r':
if username not in user_data:
register_user(username)
print(f"User {username} registered successfully!")
return username
else:
print("Username already exists. Please choose a different one.")
else:
print("Invalid choice. Please enter 'l' for Login or 'r' for Register.")
except EOFError:
print("\nInput error: Please ensure you're running this in an interactive console.")
break
except KeyboardInterrupt:
print("\nInput interrupted. Exiting user login.")
break
except Exception as e:
print(f"An error occurred: {e}")
break
def load_high_scores():
"""Load high scores from a file."""
if not os.path.exists(HIGH_SCORES_FILE):
return {}
code
with open(HIGH_SCORES_FILE, 'r') as file:
high_scores = {}
for line in file:
difficulty, name, score = line.strip().split(',')
high_scores[difficulty] = (name, int(score))
return high_scores
def save_high_score(name, score, difficulty):
"""Save player's name and score to a file."""
ensure_data_directory() # Ensure data directory exists
with open(HIGH_SCORES_FILE, 'a') as file:
file.write(f"{difficulty},{name},{score}\n")
def get_word(difficulty):
"""Select a random word from a given difficulty level."""
return random.choice(DIFFICULTY_WORDS[difficulty])
def display_progress(selected_word, guessed_letters):
"""Display the current state of the guessed word."""
display_word = ''
for letter in selected_word:
if letter in guessed_letters:
display_word += letter + ' '
else:
display_word += '_ '
print("\nCurrent word:", display_word.strip())
def guess_the_word(username):
"""Guess the word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Guess the Word'!")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
code
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
if remaining_tries <= 0:
print(f"Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Zero score if they fail to guess
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def display_shark(shark):
"""Display the shark's position."""
print("Shark's position:", shark)
def beat_the_shark(username):
"""A simple race game where player guesses letters and compares to a shark opponent."""
while True: # Loop for playing the game until user decides to exit
print("\nWelcome to 'Beat the Shark'!")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
code
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
# Initialize shark string based on difficulty
if difficulty == 'easy':
shark = "﹏﹏﹏𓂁" # 3 underscores for easy
elif difficulty == 'medium':
shark = "﹏﹏﹏﹏﹏𓂁" # 5 underscores for medium
else:
shark = "﹏﹏﹏﹏﹏﹏﹏﹏𓂁" # 7 underscores for hard
# Display initial shark position
display_shark(shark)
while remaining_tries > 0: # The loop continues as long as there are remaining tries
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess! The shark is still behind!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points. The shark moves forward!")
# Remove one '﹏' from the shark to show it moving closer
shark = shark[:-1] # Slice off the last character
if len(shark) <= 1: # If we only have the shark emoji left
shark = '𓂁' # Replace with only the shark emoji
# Display updated shark position
display_shark(shark)
# If the player successfully guesses all letters in the word, they win
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! The shark couldn't catch you!") # Show word on win
break
# If player runs out of tries, the shark catches them
if remaining_tries <= 0:
print(f"The shark caught you! Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Score is zero if caught
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def timed_guess_the_word(username):
"""Timed Guess the Word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Timed Guess the Word'!")
print("You have infinite guesses but must guess the word within the time limit.")
code
# Select time limit
while True:
print("Choose a time limit:")
print("1. 30 seconds")
print("2. 1 minute (60 seconds)")
print("3. 1 minute and 30 seconds (90 seconds)")
time_choice = input("Select a time limit (1, 2, 3): ").strip()
if time_choice == '1':
timer_duration = 30
break
elif time_choice == '2':
timer_duration = 60
break
elif time_choice == '3':
timer_duration = 90
break
else:
print("Invalid choice. Please select 1, 2, or 3.")
difficulty = input("Choose difficulty (easy, medium, hard): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty]
start_time = time.time() # Start the timer
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
elapsed_time = time.time() - start_time # Calculate elapsed time
remaining_time = max(0, timer_duration - elapsed_time) # Calculate remaining time (cannot be negative)
print(f"Time remaining: {int(remaining_time)} seconds")
if remaining_time <= 0:
print(f"Time's up! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0
break
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
save_high_score(username, score, 'timed') # Save high score for timed game
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def display_high_scores(high_scores):
"""Display the high scores."""
print("\nHigh Scores:")
for difficulty in DIFFICULTY_WORDS.keys():
if difficulty in high_scores:
name, score = high_scores[difficulty]
print(f"High Score [Difficulty: {difficulty}]: {name} with {score} points")
else:
print(f"High Score [Difficulty: {difficulty}]: No scores yet")
def start_menu():
"""Display the start menu and handle user choices."""
while True: # Loop for displaying the main menu
print("Pick a game mode!")
print("1. Guess the Word: Earn points by guessing the correct letters.")
print("2. Beat the Shark: Race against a shark by guessing the word correctly.")
print("3. Timed Guess the Word: Infinite guesses but beat the timer!")
try:
choice = input("Select a game (1, 2, or 3), or 'q' to quit: ")
code
if choice == '1':
guess_the_word(username)
elif choice == '2':
beat_the_shark(username)
elif choice == '3':
timed_guess_the_word(username)
elif choice.lower() == 'q':
print("Thank you for playing! Goodbye!")
break
else:
print("Invalid choice. Please try again.")
except KeyboardInterrupt:
print("\nInput interrupted. Exiting the game.")
break
Load high scores at the start
high_scores = load_high_scores()
Display high scores if available
display_high_scores(high_scores)
Load users and handle login/registration
username = user_login()
Start the game
if name == "main":
start_menu() just add it to this
To add color coding for the difficulty levels (easy, medium, hard) in your provided code, we can utilize ANSI escape sequences. This will allow you to display the difficulty labels in green, yellow, and red as specified.
Here's how to integrate the color-coding seamlessly into the provided code:
Step 1: Define Color Codes
At the beginning of your script, just after the import statements, define the color codes:
python
# Define color codes
COLOR_GREEN = "\033[92m" # Bright green
COLOR_YELLOW = "\033[93m" # Bright yellow
COLOR_RED = "\033[91m" # Bright red
COLOR_RESET = "\033[0m" # Reset to default color
Step 2: Update Input Prompts for Difficulty Selection
Modify the prompts in the functions that ask for difficulty to include the color codes. Here's how the specific functions will look:
guess_the_word Function:
python
def guess_the_word(username):
"""Guess the word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Guess the Word'!")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
if remaining_tries <= 0:
print(f"Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Zero score if they fail to guess
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
beat_the_shark Function:
python
def beat_the_shark(username):
"""A simple race game where player guesses letters and compares to a shark opponent."""
while True: # Loop for playing the game until user decides to exit
print("\nWelcome to 'Beat the Shark'!")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
# Initialize shark string based on difficulty
if difficulty == 'easy':
shark = "﹏﹏﹏𓂁" # 3 underscores for easy
elif difficulty == 'medium':
shark = "﹏﹏﹏﹏﹏𓂁" # 5 underscores for medium
else:
shark = "﹏﹏﹏﹏﹏﹏﹏﹏𓂁" # 7 underscores for hard
# Display initial shark position
display_shark(shark)
while remaining_tries > 0: # The loop continues as long as there are remaining tries
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess! The shark is still behind!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points. The shark moves forward!")
# Remove one '﹏' from the shark to show it moving closer
shark = shark[:-1] # Slice off the last character
if len(shark) <= 1: # If we only have the shark emoji left
shark = '𓂁' # Replace with only the shark emoji
# Display updated shark position
display_shark(shark)
# If the player successfully guesses all letters in the word, they win
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! The shark couldn't catch you!") # Show word on win
break
# If player runs out of tries, the shark catches them
if remaining_tries <= 0:
print(f"The shark caught you! Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Score is zero if caught
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
timed_guess_the_word Function:
In this function, make similar changes to the difficulty selection prompt:
python
def timed_guess_the_word(username):
"""Timed Guess the Word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Timed Guess the Word'!")
print("You have infinite guesses but must guess the word within the time limit.")
# Select time limit
while True:
print("Choose a time limit:")
print("1. 30 seconds")
print("2. 1 minute (60 seconds)")
print("3. 1 minute and 30 seconds (90 seconds)")
time_choice = input("Select a time limit (1, 2, 3): ").strip()
if time_choice == '1':
timer_duration = 30
break
elif time_choice == '2':
timer_duration = 60
break
elif time_choice == '3':
timer_duration = 90
break
else:
print("Invalid choice. Please select 1, 2, or 3.")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty]
start_time = time.time() # Start the timer
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
elapsed_time = time.time() - start_time # Calculate elapsed time
remaining_time = max(0, timer_duration - elapsed_time) # Calculate remaining time (cannot be negative)
print(f"Time remaining: {int(remaining_time)} seconds")
if remaining_time <= 0:
print(f"Time's up! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0
break
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
save_high_score(username, score, 'timed') # Save high score for timed game
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
Summary
By applying the ANSI color codes as shown, your game will now display the difficulty levels in colored text, enhancing the user experience. Remember that these colors will only display correctly in terminals that support ANSI escape codes. Here's your complete updated code with color-coded difficulty input prompts:
python
This updated version of your code reflects the color-coded difficulty prompts as required!
import random
import os
import sys
import time # Import time for the timer functionality
# Print ASCII Art Banner
ascii_art = """
██╗ ██╗ ██████╗ ██████╗ ██████╗ ██████╗ █████╗ ███╗ ███╗███████╗
██║ ██║██╔═══██╗██╔══██╗██╔══██╗ ██╔════╝ ██╔══██╗████╗ ████║██╔════╝
██║ █╗ ██║██║ ██║██████╔╝██║ ██║ ██║ ███╗███████║██╔████╔██║█████╗
██║███╗██║██║ ██║██╔══██╗██║ ██║ ██║ ██║██╔══██║██║╚██╔╝██║██╔══╝
╚███╔███╔╝╚██████╔╝██║ ██║██████╔╝ ╚██████╔╝██║ ██║██║ ╚═╝ ██║███████╗
╚══╝╚══╝ ╚═════╝ ╚═╝ ╚═╝╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝
"""
print(ascii_art)
# Define color codes
COLOR_GREEN = "\033[92m" # Bright green
COLOR_YELLOW = "\033[93m" # Bright yellow
COLOR_RED = "\033[91m" # Bright red
COLOR_RESET = "\033[0m" # Reset to default color
# Constants
HIGH_SCORES_FILE = 'data/high_scores.txt'
USER_DATA_FILE = 'data/user_data.txt'
# Expanded word lists
DIFFICULTY_WORDS = {
'easy': ['cat', 'fish', 'dog', 'bird', 'frog', 'apple', 'table', 'chair', 'book', 'lamp'],
'medium': ['giraffe', 'monkey', 'penguin', 'squirrel', 'elephant', 'dolphin', 'computer', 'pencil', 'candle', 'window'],
'hard': ['chimpanzee', 'extraordinary', 'microorganism', 'unbelievable', 'determination', 'superintendent', 'phenomenon', 'incomprehensible', 'electromagnetic', 'characteristic']
}
# Set number of tries based on difficulty
TRIES = {
'easy': 5,
'medium': 7,
'hard': 10
}
def ensure_data_directory():
"""Ensure the data directory exists."""
if not os.path.exists('data'):
os.makedirs('data')
def load_user_data():
"""Load user data from a file."""
if not os.path.exists(USER_DATA_FILE):
return {}
with open(USER_DATA_FILE, 'r') as file:
user_data = {}
for line in file:
username = line.strip()
user_data[username] = True # Using a dictionary to track registered users
return user_data
def register_user(username):
"""Register a new user."""
with open(USER_DATA_FILE, 'a') as file:
file.write(username + '\n')
def user_login():
"""Handle user login or registration."""
user_data = load_user_data()
while True:
try:
print("Login (l) or Register (r): ", end='', flush=True)
choice = sys.stdin.readline().strip().lower() # Using sys.stdin.readline()
print("Enter your username: ", end='', flush=True)
username = sys.stdin.readline().strip() # Using sys.stdin.readline()
if choice == 'l':
if username in user_data:
print(f"Welcome back, {username}!")
return username
else:
print("Username does not exist. Please register.")
elif choice == 'r':
if username not in user_data:
register_user(username)
print(f"User {username} registered successfully!")
return username
else:
print("Username already exists. Please choose a different one.")
else:
print("Invalid choice. Please enter 'l' for Login or 'r' for Register.")
except EOFError:
print("\nInput error: Please ensure you're running this in an interactive console.")
break
except KeyboardInterrupt:
print("\nInput interrupted. Exiting user login.")
break
except Exception as e:
print(f"An error occurred: {e}")
break
def load_high_scores():
"""Load high scores from a file."""
if not os.path.exists(HIGH_SCORES_FILE):
return {}
with open(HIGH_SCORES_FILE, 'r') as file:
high_scores = {}
for line in file:
difficulty, name, score = line.strip().split(',')
high_scores[difficulty] = (name, int(score))
return high_scores
def save_high_score(name, score, difficulty):
"""Save player's name and score to a file."""
ensure_data_directory() # Ensure data directory exists
with open(HIGH_SCORES_FILE, 'a') as file:
file.write(f"{difficulty},{name},{score}\n")
def get_word(difficulty):
"""Select a random word from a given difficulty level."""
return random.choice(DIFFICULTY_WORDS[difficulty])
def display_progress(selected_word, guessed_letters):
"""Display the current state of the guessed word."""
display_word = ''
for letter in selected_word:
if letter in guessed_letters:
display_word += letter + ' '
else:
display_word += '_ '
print("\nCurrent word:", display_word.strip())
def guess_the_word(username):
"""Guess the word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Guess the Word'!")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
if remaining_tries <= 0:
print(f"Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Zero score if they fail to guess
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def display_shark(shark):
"""Display the shark's position."""
print("Shark's position:", shark)
def beat_the_shark(username):
"""A simple race game where player guesses letters and compares to a shark opponent."""
while True: # Loop for playing the game until user decides to exit
print("\nWelcome to 'Beat the Shark'!")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty] # Set tries based on selected difficulty
# Initialize shark string based on difficulty
if difficulty == 'easy':
shark = "﹏﹏﹏𓂁" # 3 underscores for easy
elif difficulty == 'medium':
shark = "﹏﹏﹏﹏﹏𓂁" # 5 underscores for medium
else:
shark = "﹏﹏﹏﹏﹏﹏﹏﹏𓂁" # 7 underscores for hard
# Display initial shark position
display_shark(shark)
while remaining_tries > 0: # The loop continues as long as there are remaining tries
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess! The shark is still behind!")
else:
remaining_tries -= 1 # Deduct one attempt for incorrect guess
score -= 10
print("Wrong guess! -10 points. The shark moves forward!")
# Remove one '﹏' from the shark to show it moving closer
shark = shark[:-1] # Slice off the last character
if len(shark) <= 1: # If we only have the shark emoji left
shark = '𓂁' # Replace with only the shark emoji
# Display updated shark position
display_shark(shark)
# If the player successfully guesses all letters in the word, they win
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! The shark couldn't catch you!") # Show word on win
break
# If player runs out of tries, the shark catches them
if remaining_tries <= 0:
print(f"The shark caught you! Game over! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0 # Score is zero if caught
save_high_score(username, score, difficulty)
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def timed_guess_the_word(username):
"""Timed Guess the Word game."""
while True: # Loop for playing the game until user decides to exit
print("Welcome to 'Timed Guess the Word'!")
print("You have infinite guesses but must guess the word within the time limit.")
# Select time limit
while True:
print("Choose a time limit:")
print("1. 30 seconds")
print("2. 1 minute (60 seconds)")
print("3. 1 minute and 30 seconds (90 seconds)")
time_choice = input("Select a time limit (1, 2, 3): ").strip()
if time_choice == '1':
timer_duration = 30
break
elif time_choice == '2':
timer_duration = 60
break
elif time_choice == '3':
timer_duration = 90
break
else:
print("Invalid choice. Please select 1, 2, or 3.")
difficulty = input(f"Choose difficulty ({COLOR_GREEN}easy{COLOR_RESET}, "
f"{COLOR_YELLOW}medium{COLOR_RESET}, "
f"{COLOR_RED}hard{COLOR_RESET}): ").lower()
if difficulty not in DIFFICULTY_WORDS:
print("Invalid difficulty. Please try again.")
continue
selected_word = get_word(difficulty)
guessed_letters = []
score = 100
remaining_tries = TRIES[difficulty]
start_time = time.time() # Start the timer
while remaining_tries > 0:
display_progress(selected_word, guessed_letters)
print(f"Remaining tries: {remaining_tries}")
elapsed_time = time.time() - start_time # Calculate elapsed time
remaining_time = max(0, timer_duration - elapsed_time) # Calculate remaining time (cannot be negative)
print(f"Time remaining: {int(remaining_time)} seconds")
if remaining_time <= 0:
print(f"Time's up! The correct word was '{selected_word}'.") # Show correct word on loss
score = 0
break
guess = input("Guess a letter: ").lower()
if guess in guessed_letters:
print("You've already guessed that letter!")
continue
guessed_letters.append(guess)
if guess in selected_word:
print("Good guess!")
else:
remaining_tries -= 1
score -= 10
print("Wrong guess! -10 points")
if all(letter in guessed_letters for letter in selected_word):
print(f"You guessed the word '{selected_word}'! Congratulations!") # Show word on win
break
save_high_score(username, score, 'timed') # Save high score for timed game
# Asking the player if they want to play again
play_again = input("Do you wish to play again? (yes/no): ").lower()
if play_again != 'yes':
break # Exit the loop to stop the game
def display_high_scores(high_scores):
"""Display the high scores."""
print("\nHigh Scores:")
for difficulty in DIFFICULTY_WORDS.keys():
if difficulty in high_scores:
name, score = high_scores[difficulty]
print(f"High Score [Difficulty: {difficulty}]: {name} with {score} points")
else:
print(f"High Score [Difficulty: {difficulty}]: No scores yet")
def start_menu():
"""Display the start menu and handle user choices."""
while True: # Loop for displaying the main menu
print("Pick a game mode!")
print("1. Guess the Word: Earn points by guessing the correct letters.")
print("2. Beat the Shark: Race against a shark by guessing the word correctly.")
print("3. Timed Guess the Word: Infinite guesses but beat the timer!")
try:
choice = input("Select a game (1, 2, or 3), or 'q' to quit: ")
if choice == '1':
guess_the_word(username)
elif choice == '2':
beat_the_shark(username)
elif choice == '3':
timed_guess_the_word(username)
elif choice.lower() == 'q':
print("Thank you for playing! Goodbye!")
break
else:
print("Invalid choice. Please try again.")
except KeyboardInterrupt:
print("\nInput interrupted. Exiting the game.")
break
# Load high scores at the start
high_scores = load_high_scores()
# Display high scores if available
display_high_scores(high_scores)
# Load users and handle login/registration
username = user_login()
# Start the game
if __name__ == "__main__":
start_menu()