Python development of Chinese chess practice (with source code)

Keywords: Python Web Development Django

 

Pygame's Chinese chess has always been fond of playing chess. When we write python, we will try it. The level of computer walking is limited and the level of computer walking is low. We need to update the computer walking in the next version. We hope that the source code can help you learn Python better. There are four files in total: chinachess.py as the main file, constants.py as the data constant, pieces.py as the chess sub class, walking method, and computer.py as the computer walking method.

PS: in the process of learning python, many people often give up because they can't solve problems or don't have good tutorials. Therefore, I sorted out the basic Python script, web development, crawler, django, data mining and other [PDF] needs, which can be used in the full stack of Python development and communication. Skirt: after a long time of martial arts, you can find it under the conversion of thinking (Digital homophony) Here we are. There are the latest Python tutorial projects to take. The problems that we don't understand can be solved by the old driver. We can supervise each other and make progress together

 

 

chinachess.py is the main file

import pygame
import time
import constants
import pieces
import computer

class MainGame():
    window = None
    Start_X = constants.Start_X
    Start_Y = constants.Start_Y
    Line_Span = constants.Line_Span
    Max_X = Start_X + 8 * Line_Span
    Max_Y = Start_Y + 9 * Line_Span

    player1Color = constants.player1Color
    player2Color = constants.player2Color
    Putdownflag = player1Color
    piecesSelected = None

    button_go = None
    piecesList = []

    def start_game(self):
        MainGame.window = pygame.display.set_mode([constants.SCREEN_WIDTH, constants.SCREEN_HEIGHT])
        pygame.display.set_caption("Azure-Chinese chess")
        MainGame.button_go = Button(MainGame.window, "Restart", constants.SCREEN_WIDTH - 100, 300)  # Create start button
        self.piecesInit()

        while True:
            time.sleep(0.1)
            # Get events
            MainGame.window.fill(constants.BG_COLOR)
            self.drawChessboard()
            #MainGame.button_go.draw_button()
            self.piecesDisplay()
            self.VictoryOrDefeat()
            self.Computerplay()
            self.getEvent()
            pygame.display.update()
            pygame.display.flip()

    def drawChessboard(self):
        mid_end_y = MainGame.Start_Y + 4 * MainGame.Line_Span
        min_start_y = MainGame.Start_Y + 5 * MainGame.Line_Span
        for i in range(0, 9):
            x = MainGame.Start_X + i * MainGame.Line_Span
            if i==0 or i ==8:
                y = MainGame.Start_Y + i * MainGame.Line_Span
                pygame.draw.line(MainGame.window, constants.BLACK, [x, MainGame.Start_Y], [x, MainGame.Max_Y], 1)
            else:
                pygame.draw.line(MainGame.window, constants.BLACK, [x, MainGame.Start_Y], [x, mid_end_y], 1)
                pygame.draw.line(MainGame.window, constants.BLACK, [x, min_start_y], [x, MainGame.Max_Y], 1)

        for i in range(0, 10):
            x = MainGame.Start_X + i * MainGame.Line_Span
            y = MainGame.Start_Y + i * MainGame.Line_Span
            pygame.draw.line(MainGame.window, constants.BLACK, [MainGame.Start_X, y]

Posted by xatter on Fri, 14 Feb 2020 08:21:45 -0800