Monday, February 22, 2021

IslandT: Find out which chess board square has been clicked using Python Pygame

Welcome back to the ongoing python pygame chess engine project and in this article, I will show you the code which will display the name of the square (if you know chess then you should know the name of those squares a1, b5 and etc) on the chessboard which has been clicked by a user! If we know where the user has clicked on the chessboard we can then assign the position to the piece on the chessboard later on.

Here are the entire names of those squares on the chess board that you might want to take a look before we start!

&

The purpose I am creating this chess engine project is not to cheat but instead, I want to play and learn with stockfish so I can become a better chess player when I play a grandmaster. I will not use the chess engine in any game on official chess websites because I just want to play a real game with a real player who also will not use any chess engine or receives help from a grandmaster during the game!

The key point to the positioning solution is to create a list of names of those squares on the chess board and display it when a user has clicked on that square on the chess board!

#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 = ''

#empty chess dictionary

chess_dict = {}

#chess square list
chess_square_list = [
                    "a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
                    "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
                    "a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
                    "a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
                    "a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
                    "a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
                    "a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
                    "a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"
                    ]

# chess square position
chess_square_position = []

# create a list to map name of column and row
for i in range(0, 8) : # control row
    for j in range(0, 8): # control column
        chess_square_position.append((j * width, i * width))

# create a dictionary to map name of column and row

for n in range(0, len(chess_square_position)):
    chess_dict[chess_square_list[n]] = chess_square_position[n]

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 * width, i * width, width, width))
        else:
            if j % 2 == 0: # which means it is an even column
                rect_list.append(pygame.Rect(j * width, i * width, width, width))


# 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)

            # print the square name which you have clicked on
            for key, value in chess_dict.items():
                if (x * width, y * width) == (value[0],value[1]):
                    print(key)

            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()

When we clicked on any area on the chessboard the name of the square will be displayed on the output area of the PyCharm IDE.

altThe name of the square has been displayed when the user clicks on the chessboard area

With this, we can then start to create an object which consists of all the positions of the pieces on the chessboard and move them around! Stay tuned for the next article!



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...