Hello everyone, this is a new chess game project which I am going to start to create and update the code on this website weekly. In this first chapter of the project report, I am going to 1) render out the chessboard 2) write a code to recognize the square which I am touching on 3) Put a pawn on the board.
Without further due, let me start by explaining to you first what this project is all about and what is the final outcome of the project.
The reason I have started this project is that I want to create a chess game application that I can play with as a training exercise for myself to sharpen my chess skill. Stockfish is the chess engine that runs this application and the python code will use one of the Stockfish wrappers written in python to communicate with the Stockfish chess engine. Pygame will be used to create the chess game interface as well as to move pieces along the chessboard. The finished project will contain a common interface together with the chessboard and possibly also includes the AI and analysis part later on but not in this project.
The below python program will perform the above three goals, first, render the chessboard, then prints out the square on the command line and highlight the square area (I am using pycharm to write this program but you can certainly use another editor if you have one) each time the user pressed on one of the squares, and finally, put a pawn on one of the squares on the chessboard.
import sys, pygame import math pygame.init() size = width, height = 512, 512 white = 255, 178, 102 black = 126, 126, 126 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 = [] #pawn image pawn0 = pygame.image.load("pawn.png") # 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 the 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 chessboard 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)) screen.blit(pawn0, (0, 64)) # just testing... pygame.display.update()
The result is as follows…
pygame chess projectAt the moment only one piece of pawn is on the chessboard and our next goal is to move that piece along the board which is going to happen in the next coming chapter!
from Planet Python
via read more
No comments:
Post a Comment