We put a self as an argument for the Rook class. As you noticed here and in the other statement: if piece = 'R': After we configure the letters, we'll overwrite the current square.occupying_piece with a value of their piece class with the color depending on the first value of the piece string. If 'R', then it's a Rook, 'B' for Bishop, and so on. If we got 'N', then it's a Knight if 'P', then it's a Pawn. Each piece from nfig will be identified according to the capital letter it has, except the Knight. If the current value of piece in nfig is an empty string or '', then the tile must be empty, and if not, it will access its respective tile position through the current value of x and y. The setup_board() creates each piece, and puts them in their respective place by mapping the nfig with the whole board. # as you notice above, we put `self` as argument, or means our class Board (x, y), 'white' if piece = 'w' else 'black', self # looking inside contents, what piece does it have Square = self.get_square_from_pos((x, y)) Now let's create the other parts of the Board, including the tup_board() we called above. Below it, we configure the self.squares with a value calling our self.generate_squares() for making chess tiles and putting them all in a list. The nfig represents the chessboard configuration with a 2D list having our pieces with their default position. In making the whole chess board, it's important to know first what is the width and height of the game window so we can divide it into 8 rows with 8 columns to identify our tiles' exact size. Return self.get_square_from_pos(pos).occupying_piece Square(x, y, self.tile_width, self.tile_height) # /* Board.pyįrom import Rookįrom import Bishopįrom import Knightįrom import Queenįrom import Kingįrom import Pawn Let's make another class for handling tiles and the whole board. The second if statement tells that if the square has a piece in this position, you should access its icon and place it inside the tile. The draw(), executes the configurations we did by drawing the tile in the canvas, in the color it was assigned. Like "a1", it is the bottom leftmost tile in a chess board. Letters symbolize rows, and the number symbolizes columns. The get_coord() returns the name of the tile depending on its x and y based on the real board. The self.rect configures the width, height, and location (using self.abs_x and self.abs_y) of a square or tile. We also have self.highlight_color which we use to highlight the tiles with the possible movement of a piece if it was selected. The lor tells the square tile should be light colored if it is divisible by 2 or instead dark if not, while the self.draw_color tells the color configuration for light and dark. ![]() self.abs_x and self.abs_y dictates where the chess tile is assigned to be drawn inside the window, and we compile them both in self.abs_pos. As you see above, we have self.x and self.y while we also have self.abs_x and self.abs_y. With this basic information, we can fulfill other variables using them. Let's start by adding the _init_() function to get the square's width, height, x for a row, and y for the column. The first thing we're gonna do is to make a class for making chess Square. (display, self.draw_color, self.rect)Ĭentering_rect = self.occupying_rect()ĭisplay.blit(self.occupying_piece.img, centering_rect.topleft) (display, self.highlight_color, self.rect) # configures if tile should be light or dark or highlighted tile lor = 'light' if (x + y) % 2 = 0 else 'dark' The Square class creates, colors, position, and draw each chess tile inside our game window: # /* Square.py The board will mainly focus on square positions and game rules, while the pieces focus on the piece they represent and the moves it has. Our chess game has two main code parts creating the board and creating the pieces. Coding the GameĪnd now we're done setting up we can start coding now. ![]() ![]() If you don't have chess icons, you can use mine here. Make sure your image files are named _.png just like this: Move the images of the chess icons you'll use in the python/data/imgs/ directory. Once we installed the pygame, let's move into setting up our environment by making the py files and folder we're using in this order: > python-chess Installation and Setting Upīefore we start coding, let's first install the pygame module in the terminal: $ pip install pygame Related: How to Make a Hangman Game in Python. Learn how to code a chess game with Python classes.It's good practice for making apps with GUIs while getting good at using classes. The chess game is a pretty cool project idea for intermediate Python programmers.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |