In this article, we will continue with our chess engine project by creating a chessboard in python with the help of the pygame module. In our previous article, we are showing a pawn in the board but in this article, we will ignore that pawn and go ahead with creating a chessboard instead.
The chessboard will be 512 x 512 pixels with 8 rows x 8 columns filling with either light brown or deep brown square with the size of 64 x 64 pixels each.
We will use the light brown color as the base color as well as the color for the light square and the dark brown color as the color for the darker square.
Here is the entire revised program which you can easily follow using the comment in the 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 pygame.init() size = width, height = 512, 512 white = 255, 178, 102 black = 255, 128, 0 title = "IslandT Chess" 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 chessboard surface for chess_rect in rect_list: pygame.draw.rect(chess_board_surface, black, chess_rect) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() # displayed the chess surface screen.blit(chess_board_surface, (0, 0)) pygame.display.flip()
As you can see, I am using a loop to draw the dark rectangle on the surface and the light rectangle will automatically fill with the base color!
This is it! A simple step each time will help us move closer to our final target!