micro:bit Wordle game

screenshot of micro:bit Wordle game

I made a Wordle-like game in Python for the BBC micro:bit.

The code is pretty compact and easy to understand, so I think getting students to create their own version of this popular game or pick apart this one would make a nice activity. There are plenty of opportunities to improve it.

You could use it to:

  • teach some Python
  • learn about functions
  • learn about using global variables in functions
  • learn about slicing strings
  • use imports – I hide the word list in an import
  • experiment with the serial console in the new alpha micro:bit Python editor

You’ll need a micro:bit and a Chrome or Edge browser as it uses webUSB – you interact with the micro:bit using your computer’s keyboard and screen.

You get 5 guesses. It shows your progress on the micro:bit’s LED display: a bright LED means the right letter in the right place, a dim LED means the right letter in the wrong place.

It also prints out your progress in the serial console. Capital letters are in the right place, lower case in the wrong place: So

--a-T

means the word ends in T and has an ‘a’ in 1st, 2nd or 4th place.

You can download a hex file to flash direct onto a micro:bit or drag and drop onto the Python editor, plus the raw Python files over on GitHub: https://github.com/blogmywiki/wordobit.

You’ll probably want to add more words to words.py. You could also improve the program in lots of ways, for example add some simple encryption like ROT13 to the word list or add more word checking.

I also made a video to explain how it works and how to play it:

main.py code

from microbit import *
import random
import music
from words import wordlist

def newWord():
    print('New game!')
    display.clear()
    global word
    global turn
    word = random.choice(wordlist)
    turn = 0

newWord()

while True:
    print('Turn',turn+1)
    if turn > 4:
        print('You lose. Your score is X/5. The word was ' + word)
        music.play(music.POWER_DOWN)
        sleep(5000)
        newWord()
    guess = input('What is your guess? ')
    if len(guess) != 5:
        print('Not a 5 letter word!')
    else:
        progress = ''
        for a in range(5):
            if guess[a] in word:
                if guess[a] == word[a]:
                    display.set_pixel(a,turn,9)
                    progress = progress + guess[a].upper()
                else:
                    display.set_pixel(a,turn,4)
                    progress = progress + guess[a].lower()
            else:
                progress = progress + '-'
        print(progress)
        turn += 1
        if guess == word:
            print('Congratulations! Your score is ' + str(turn) + '/5')
            music.play(music.POWER_UP)
            sleep(5000)
            newWord()

words.py code

# List of 5 letter words hidden from main Python program view
wordlist = ['heart', 'aloft', 'float', 'banjo', 'scoop']
This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>