For those of you who have read my previous article about my desire to create a chess game based on the stockfish chess engine, this one is the continued story from the previous article. In the previous article, I have shown you how to install stockfish module for python, and in this article, we will start to create the graphic part of the chess application using Python.

When it comes to creating a game with python, PyGame is the only game engine that many have considered to be the best in the market right now. There are other game engines such as Panda3d which also allows us to create a game with Python program but I will stick to PyGame because it is simple and easy to use as compared with Panda3d and the other game engines.

What I need from PyGame is to help me to create the game GUI and then I will use stockfish to calculate the next move for the opponent and display all the pieces on the game scene.

In this article, we will first begin to install PyGame on the PyCharm IDE and then create a simple program to display a pawn on the scene.

Here are the steps to install the PyGame module on our project:

  1. Go to File->Settings.
  2. On the left pane of the Settings panel, under Project select Python Interpreter then click on the ‘+’ sign on the bottom of the right pane to open the Available Packages panel, type in PyGame to the search box and then select the PyGame package which appears on the left pane of the Available Packages panel, finally click the Install Package button to install the PyGame package to our project.

Next I will create a 300 x 300 pixels pawn graphic and imported it to our project.

Finally, write the python program with the help of the PyGame module to display the pawn on the game scene as follow.

Display a chess pawn on the scene with PyGame game engine.
import sys, pygame
pygame.init()

size = width, height = 300, 300
white = 255, 255, 255
title = "Chess"

screen = pygame.display.set_mode(size)
pygame.display.set_caption(title)

pawn = pygame.image.load("pawn.png")
pawnrect = pawn.get_rect()

while 1:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    screen.fill(white)
    screen.blit(pawn, pawnrect)
    pygame.display.flip()

In the coming articles, we will create the chessboard and start to arrange the chess pieces on the chess squares, this project will take a year to complete so do enjoy your stay on this site! I have assumed you to be very good in a python programming language as well as know the PyGame game engine very well, if you are just a beginner then you need to go to the related website to learn more about those two or you also can consider buying books through this website to brush up your python skills 🙂

Please follow and like us:

Leave a Reply

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