Color letters with colorama!

Published May 6, 2022, 7:04 p.m.

Last time we made an exhaustive list of 5 letter words.  Now we will pivot over to another import feature of the Wordle game.  Namely using color as an indicator of whether a letter is correctly placed or not.

To do this we will be using the python package colorama.   Start a new text file for our program.  I named mine "Wordle.py".

At the top of our code we will start with our imports:

import sys, os
import colorama
from colorama import Fore, Back, Style
from random import choice

colorama.init()

I import both the full colorama and specific packages from it.  I need the full package to "initialize" it for use.  In the name of convenience I import the specific packages so that I don't have to type colorama when I want to use them.

After getting all the imports that I think I will need, I go ahead and initialize colorama as shown.

Next, I want to create a general definition that will print a statement (text) and colorize the background the color that I want.  I can do it via the following:

def printback(statement, color):
    print(color + Style.BRIGHT + f"{statement}" + Style.RESET_ALL, end="")
    
green = Back.GREEN
yellow = Back.YELLOW
grey = Back.LIGHTBLACK_EX

I define "printback" and make it have 2 arguments: the statement that I want to print and the color to print the background.  Notice that in the print statement I feed it the color (which is yet to be defined) a Style, the statement, a Style reset and a keyword to remove the default "newline" that happens after a normal print statement.

After the printback definition, I can define the colors that I will use by employing the "Back" attribute from the colorama module.

Let's test it! 

printback("Hello World!", green)
printback("Hello World!", yellow)
printback("Hello World!", grey)

This statement should print the "Hello World!" text with the accompanying background colors implied.

To make a space inbewteen we can employ an empty print statement:

printback("Hello World!", green)
print("")
printback("Hello World!", yellow)
print("")
printback("Hello World!", grey)
print("")

Finally, to show the actual behavior that we want we can use an series of "printback" statements to spell the word "Hello" with different colors:

printback("H", green)
printback("e", yellow)
printback("l", grey)
printback("l", grey)
printback("o", green)

Now we have a robust method for printing letters with the colors that we want.   In the next tutorial we will start cooking up the logic to make the actual Wordle game.

See you there!

 

skip_nextGet words from our word list!
  • Introduction to PyWordle

  • Get the latest version of Python (3.10.4)

  • Figure out all the 5 letter words (part 1)

  • Figure out all the 5 letter words (part 2)

  • Color letters with colorama!
    (currently viewing)
  • Get words from our word list!

  • Select a Random Wordle

  • Use a 'WHILE' loop for guessing

  • Start Comparing Letters in Words

  • Plan out the Wordle Logic

  • Put Wordle Logic Plan into Action!

  • Declaring Victory