Utilize Python and Rich to Create a Wordle Clone
Creating a Wordle clone in Python using the “Rich” library is a fun project that combines word guessing and terminal-based graphics. Wordle is a word-guessing game where players try to guess a hidden word within a limited number of attempts.
Here’s a basic implementation of a Wordle clone using Python and the Rich library. Please note that this is a text-based version and won’t include a graphical interface:
import random
from rich import print
# List of words for the game
word_list = ["apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "honeydew"]
# Select a random word from the list
target_word = random.choice(word_list)
target_word = target_word.lower() # Convert the target word to lowercase
attempts = 6 # Number of attempts allowed
# Initialize the guessed word
guessed_word = ["_" for _ in target_word]
print("Welcome to Wordle!")
while attempts > 0:
print("\nAttempts left:", attempts)
print(" ".join(guessed_word))
guess = input("Guess a word: ").lower()
if guess == target_word:
print("\nCongratulations! You guessed the word:", target_word)
break
else:
print("Incorrect guess.")
attempts -= 1
if attempts == 0:
print("\nOut of attempts. The word was:", target_word)
In this code:
- We import the necessary modules, including
random
for selecting a random word, andrich
for printing messages with formatting. - We have a list of words from which the game randomly selects a target word.
- The player has a limited number of attempts (in this case, 6 attempts).
- We initialize a list called
guessed_word
to represent the current state of the guessed word. Initially, it contains underscores, one for each letter in the target word. - The game loop continues until the player either guesses the word correctly or runs out of attempts.
- Inside the loop, the current state of the guessed word is displayed, and the player is prompted to enter their guess.
- If the guess matches the target word, the player wins. Otherwise, attempts are decremented, and the loop continues.
- If the player runs out of attempts, the game ends, and the target word is revealed.
To run this code, make sure you have the “Rich” library installed. You can install it using pip
:
pip install rich
After installing the library, you can run the code, and it will create a simple terminal-based Wordle clone for you to play. You can expand upon this basic version to add more features and a larger word list for a richer gaming experience.