In this article I will continue to work on the previous chess engine project by creating a clickable chessboard with Python and Pygame. What I will do here is to write a program which will highlight any square within the chessboard every time we left click on it with our mouse and that square will return to it’s original color after we have released the mouse!

The most important part of the program is to find out which square on the chessboard has the user clicked on with below code.

 if event.type == pygame.QUIT: sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pos = event.pos
            x = math.floor(pos[0] / width)
            y = math.floor(pos[1] / width)
            original_color = chess_board_surface.get_at((x * width, y * width ))
            pygame.draw.rect(chess_board_surface, hightlight, pygame.Rect((x) * width, (y) * width, 64, 64))
        elif event.type == pygame.MOUSEBUTTONUP:
            pos = event.pos
            x = math.floor(pos[0] / width)
            y = math.floor(pos[1] / width)
            pygame.draw.rect(chess_board_surface, original_color, pygame.Rect((x) * width, (y) * width, 64, 64))

The rest of the code is the same as before, here is the entire revise version of the previous program!

#from stockfish import Stockfish

#stockfish = Stockfish("E:\StockFish\stockfish_20090216_x64")
#stockfish.set_position(["e2e4", "e7e6"])
#print(stockfish.get_board_visual())

import sys, pygame
import math

import pygame as pygame

pygame.init()

size = width, height = 512, 512
white = 255, 178, 102
black = 255, 128, 0
hightlight = 192, 192, 192
title = "IslandT Chess"

width = 64 # width of the square
original_color = ''

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

rect_list = list() # this is the list of brown rectangle

# used this loop to create a list of brown rectangles
for i in range(0, 8): # control the row
    for j in range(0, 8): # control the column
        if i % 2 == 0: # which means it is an even row
            if j % 2 != 0: # which means it is an odd column
                rect_list.append(pygame.Rect(j * 64, i * 64, 64, 64))
        else:
            if j % 2 == 0: # which means it is an even column
                rect_list.append(pygame.Rect(j * 64, i * 64, 64, 64))


# create main surface and fill the base color with light brown color
chess_board_surface = pygame.Surface(size)
chess_board_surface.fill(white)

# next draws the dark brown rectangles on the chess board surface
for chess_rect in rect_list:
    pygame.draw.rect(chess_board_surface, black, chess_rect)

while True:
    for event in pygame.event.get():

        if event.type == pygame.QUIT: sys.exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pos = event.pos
            x = math.floor(pos[0] / width)
            y = math.floor(pos[1] / width)
            original_color = chess_board_surface.get_at((x * width, y * width ))
            pygame.draw.rect(chess_board_surface, hightlight, pygame.Rect((x) * width, (y) * width, 64, 64))
        elif event.type == pygame.MOUSEBUTTONUP:
            pos = event.pos
            x = math.floor(pos[0] / width)
            y = math.floor(pos[1] / width)
            pygame.draw.rect(chess_board_surface, original_color, pygame.Rect((x) * width, (y) * width, 64, 64))

    # displayed the chess surface
    screen.blit(chess_board_surface, (0, 0))
    pygame.display.update()

The above step is very important because if we know where the user has clicked on the chessboard then we will be able to move the piece on the chessboard to any location within the chessboard in the later stage of the project.

This project has been through the on and off stage and I have decided to finished up the entire project as fast as I can which will result in a full version of the chess engine project with wonderful graphic and powerful stockfish chess engine as it’s main brain! You really cannot find any great online chess engine where you can use to help you to improve your chess game, therefore this program will help you out a lot after it has completed!

I have started a fan page on KO-FI and if you want to subscribe to it please do so through the below button. This fan page will update constantly with the latest game and software which I have created where you can download and enjoy it! Also if you want to help me out then you can donate $3 dollar to me through this ko-fi page. I will work harder to write more free code so you can use it to further upgrade to create your own project. I have written python code and try them out for you so you do not need to waste time writing the program skeleton by yourself. You can leave a comment to suggest to me what type of python program which you need on my KO-FI page and I will write an article about it on this website if I can, but first, you will need to become my fan before you can make any request! So go ahead and subscribe to the below page, fan page subscription is totally free, the donation is one-off 3 USD, but if you think the code is helping you out then you can donate to me more later, thank you!

We will continue the chess engine project in the next article!

Please follow and like us:

Leave a Reply

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