STM8 - Word Game
Contributed by jimloko
Overview
A simple word game using the STM8L-DISCOVERY board. Words of a category flash on the display, and you have to push teh button when you see a word that doesn't belong to that category. Your response time is measured, and well as a simple scoring system!!
Details
Finally finished the game! I have uploaded the project files. it includes an info doc with a long description of the application. i will summarise below:
The Game
I’ve put together a simple word game using the STM8L. It uses the LCD to display words from a category of words, and randomly throws in a word from another category. When you see the odd word you hit the button. It then counts as a hit, increases your score and shows you your button push response time. If you miss a word it removes a ‘life’. It also displays your response time!
Your lives are also displayed on the LCD using the ‘bar’ section. When all lives are lost its game over after which it shows your score (number of correct hits), and your average response time for the game.
As each round progresses the words show for a shorter and shorter amount of time, until it gets ridiculously fast!!
Game Operation
Looking at the code the game works in a standard game loop. There are 3 game states; Into, Play and Game End.
Intro: just displays a welcome message and waits to start play. When it detects the button press it shifts into game play state and initialises game variables.
Play: is where the action happens. It starts by randomly selecting a word category and begins looping through the words. For each word it selects another random number, and based on this number decides whether to display the word, or a word from another category. If the word is from this category it just displays it and waits a timeout period, then moves onto the next word.
Game End: display the game over message, and also shows you how many hits you got, as well as your average response time across all rounds. Monitors for a button press, and moves into the Intro state to start again.
Game Word Array
I needed a way to keep a list of the words within the categories and within an array so they were all kept neat, and I could reference the group using a common variable name.
C doesn’t use a standard character ‘string’, instead it uses an array of char characters, terminated with a nul (\0). So when you define a ‘string’, it’s actually an array of chars, or a char array.
I wanted to select the word category based on a random integer between 1 and the number of word categories. This is not going to work in C because c stores a multi-dimensional array as a contiguous block of chars.
Random Numbers
I used a little trick I picked up which uses the ADC to sample a floating pin. Because the pin is floating, and not grounded, the ADC should pick up rogue charges on that pin, giving us a pseudo random number!!
After doing some maths on this number to clamp it between a min and max number, hey-presto, we have a random number generator!!
Button Press Response Time
Timer 4 can easily be configured to run for a millisecond, and fire a trigger after that millisecond. Initialise the timer for 1ms, and start a while loop. In the loop I then start the timer and sleep until the timer flag is triggered (by the timer), then I increment a counter and reset the timer to trigger after another ms, thus counting the number of elapsed milliseconds.
Conclusion
After extracting the project, open with the IAR IDE, goto Project/Rebuild All, then plug in the board and go Project/Download and Debug.
The debug settings should already be set to ST-LINK, so it will automatically pick up the dev board, download the app and wait for you to hit run!
Supporting Materials
| Title | Information type |
|---|---|
| Word Game Project Files | Software Code |
Comments
Related blog posts
Associated products and knowledge
Questions and answers
Contribute
Like to share knowledge with the community or ask a question relating to this knowledge?

GaryJackson
It's really good and the documentation really helps, so good effort there.
LStacey
This is great!
Thanks, Jimloko!
GaryJackson
Hi Jim
How's it coming along?
Gary
LStacey
Looking forward to it!