How I Built A Chess Game With Python: Case Study

Samuel Ogunleke (SamuelHSSP)
8 min readSep 7, 2024

--

A guy holding up a chess piece, maybe the king?

Python is a general-purpose programming language and can be used for almost anything, but it has its own strengths and weaknesses. Game development is one of Python’s weaknesses, but I used the same programming language to build a chess game. In this article, you will learn how a YouTube video inspired me to build a Python chess game.

It all started when I was watching chess videos on YouTube, and I came across a particular channel, showing ‘scripted’ chess plays, with funny sound effects, as if it were a chess anime. It was very interesting, with so many plot twists, then I got triggered to build an actual chess game.

Okay, perhaps I need to mention here that I’m not a professional chess player, not even close to a master. I was watching chess videos because I knew the basics, at least, and they seemed interesting. You’d agree with me that you can only build a game if you fully understand what it’s all about. You need to know the rules, how people win and lose, and what story your game tells. But how did I go about building a whole chess game without being a professional chess player? Well, ride with me.

Research

If there is one thing I always do before solving any problem, it’s called research. I went to Google, ChatGPT and asked many questions about chess and all the rules involved. I looked for all the different ways people could win or lose or draw (checkmate, stalemate, resignation etc). Chess.com was very helpful for this, because they had a particular blog post about the different ways chess games can end. I had to know about all the interesting things about chess, such as the concept of “grass to grace” when a pawn turns to a queen :D and so many other interesting concepts.

Planning

A guy writing a to-do list on paper

Before doing anything, I took my time to plan how I would go about building the whole thing. I already knew what libraries I would use to build the game, and how I would go about implementing the ‘special’ features in the development. Okay, my special features were the funny background sounds played whenever a queen is killed or when a king is in check. Let me describe the other one. Whenever a king is in check, the game plays the sound of a gun being cocked, as if you pointed a gun at the king’s head:

“Give up or sacrifice any of your subjects!”.

Another thing happens whenever there’s a checkmate, a sound of someone saying “CHECKMATE!” majestically is played.

I did the planning well and wrote whatever I had to write down. I had a list of the three core libraries I was going to use in Python — Kivy, KivyMD, and Chess. Funnily enough, I already knew about the chess library in Python, but never took a step to build an actual game with it. Well, it was high time!

Gathering Resources

A guy on a MacBook on a white table with coffee and other gadgets

It was quite easy to get all the 12 chess pieces downloaded online, so that didn’t take too much time. Okay, it was time to gather sound effects online. At least, if not perfect, they should be close to the ones I heard from the YouTube videos I watched. I searched Envato Elements, Epidemic Sound, and other websites for all sound effects I wanted. I got the majestic “CHECKMATE!” sound, but not for stalemate. Hmm, how to get the sound for stalemate? I knew I couldn’t possibly get all the sound effects I needed, but how do I create a majestic voice saying “STALEMATE!”. Well, one thing I always do is making use of what I have, to get what I need or don’t have.

Variables and paths for sound effects

So, I set up my microphone, with the confidence in my deep voice, fired up Audacity on my laptop, and set off to create the majestic sound effect by myself. Believe me, it took many takes to finally get what I wanted. I said “STALEMATE!” many times as if it was my lucky word (if that exists). I had to reduce the background noise, set the right volume (compression, amplification), the right pitch, and the filters for bass and treble. It was a lot! Nobody told me that game development would make a programmer like me create sound effects by myself. But at the end, it was worth it. The more you learn a skill you need but don’t necessarily have to learn, the better you become than the average person. At the end, I had all the sound effects I needed at the time, and began with development.

Not only that, I also downloaded all the special fonts I needed for the game. If you know, games don’t usually use normal fonts for game text. I downloaded quite a number of fonts like Astron Boy, Debug, Metal Lord, Minecraft, Pricedown, and Quarterback. It didn’t really matter which one to download, because I was going to test all of them anyway.

I created folders for the fonts, avatars (of the players), background images, chess pieces, and the sound effects. File organization is very important in development, as it helps you to keep track of your files easily.

Development

Some Python code not related to this article

It was time to code! I had all the necessary skills required to build the game (or so I thought). So, I felt unstoppable and set up Kivy and KivyMD on my laptop, then installed the chess library to build the game. Okay, the chess library works this way: when you run it on your terminal or REPL, it displays a 8x8 chess board in the form of text, using dots (.) to represent empty spaces and letters to represent the chess pieces, like uppercase Q for white queen (lowercase q for black, no racism intended, hahaha). Now, I needed to find a way to bring the text to life, in a GUI (Graphical User Interface), created with Kivy and KivyMD.

The Chess Board

Chess board from Chess.com

The chess board is a 8x8 grid alternating between light and dark colours (for example, black and white). I had to be mindful of the order of the light and dark colours. It was important to note that the top left and the bottom right corners had a light colour, while the other corners had a dark colour. Okay, check! But how to draw the grid?

My first thought to use an image for the chess board, and place the chess pieces at their respective positions, accordingly. Maybe a good solution, but not quite sustainable. What if I wanted to move the chess pieces, then I would have to do some complex maths to calculate the centre of the box I was moving to. So, I gave up on that, and the only option left was to draw the chess board by myself.

I used a grid layout in Kivy and drew the chess boxes with a for-loop, alternating between light and dark colours. I set the grid to have 8 rows and 8 columns, so I had up to 64 boxes on the loop, then the layout arranged them accordingly. Believe me, I didn’t get it the first time, but let me not waste your time telling you about how long it took.

The Chess Pieces

Chess pieces

Placing the chess pieces was quite straightforward. I used the chess library and “married” it to the chess board I had created. Whatever the chess engine from the library showed, would be reflected in the GUI. I assigned the chess pieces accordingly and placed them on their respective positions. I made them clickable (toggle button), and used the chess engine to find out legal moves for any chess piece that was toggled on, then found the boxes around the chess piece, and changed their background colours. Whenever you move a chess piece, it calls the same move as a function, in the chess engine. So, they were working together. The chess engine was the backbone of everything, and the GUI was just meant to bring the engine to life.

A game loop exists in every game, to make sure the game keeps running until someone wins or loses, or there is a draw. However, I didn’t want to use a game loop, so I used a different approach, to check the status of the chess game after each chess move. So, whenever you move a piece, it uses the chess engine to check the status of the board, if either someone loses or wins, or there is a draw, before the end is ended.

Moreover, I made sure I kept track of the number of kills and the particular chess pieces who ‘died’.

The Gameplay

Playing the chess game with some background music, and special sound effects felt so cool! There are usually two players in a chess game (unless there’s a rigged version), and either of them plays the white or black pieces. Playing the game involves the following steps:

  1. Launch the game
  2. Host a game or join a game
  3. Start playing
  4. Kill a piece with a high rank (knight, queen, etc) and hear the funny sound effect.
  5. Put the king on check, and hear a gun cock.
  6. Checkmate the other player, and hear a gun shot and the majestic “CHECKMATE!” voice.
Chess Game between Ronaldo and SamuelHSSP

I added miscellaneous features, such as a chat system, since it’s a desktop game. I made it so that you could chat with your opponent. Furthermore, I added a feature to join a chess game as either a player or a watcher. So you could decide to join a chess match, between your friends. You could also change the theme of the game, in case you want a chess board in blue, brown, black, or other cool colours.

I created a YouTube demo for you to see how the whole game looks like. Okay, I obviously didn’t play the game with Ronaldo, it was actually a birthday project I announced on my birthday (Feb 5), and Ronaldo was my opponent, simply because he also celebrates his birthday on the same day (no intention to brag here).

Conclusion

Let me know what you think about this article and the game, and if you want more case study articles like this. If you’d like to collaborate with me on this project, then feel free to reach out to me via LinkedIn. The next steps for this project would be creating an AI that plays with you, so that you can’t practise playing with a computer, whenever no one is available to play. Remember the procedures for this game development, and I went from bringing an idea from my head to life. To learn more how to solve problems using tech, you can visit my websiteTech Plus Plus.

--

--

Samuel Ogunleke (SamuelHSSP)

Founder at Tech Plus Plus • Creator of CMDTube and PyPkgGen (Python library with 10.1k+ downloads) • Software Engineer • Technical Writer