After reading lesson 38, you’ll be able to
- Write a simple game by using the tkinter library
- Use classes and object-oriented programming to organize code for a GUI
- Write code that interacts with the user using the keyboard
- Use a canvas to draw shapes in your program
When you think of a program that uses a GUI, one of the most common kinds of programs that comes to mind are games. Games that are short and interactive offer quick distractions. They’re even more fun to play when you write them yourself!
The problem
Write a GUI game using the tkinter library. The game simulates a game of tag. You should create two players inside a window. The players’ position and size can be randomized at the start. Both players will use the same keyboard: one will use the W, A, S, D keys, and the other will use the I, J, K, L keys to move their piece. Users decide which one will try to catch the other. Then, they’ll move around the window using their respective keys to try to touch the other player. When they touch the other player, the word Tag should appear somewhere on the screen.
This is a simple game, and the code to write it won’t be long. When writing GUIs or visual applications such as games, it’s important to not be too ambitious at the beginning. Start with a simpler problem and build upon it as you get things working.