I believe that everyone has played, so you have not tried to write their own renju? Today to take you to achieve the following five-piece.
Def initChessSquare(x,y): # initChessSquare for I in range(15): # rowlist = [] for j in range(15): PointX = x+ j*40 pointY = y+ I *40 sp = StornPoint(pointX,pointY,0) initChessList.append(rowlist)Copy the code
Create init checkerboard method initChessSquare(x,y) : iterate over all the coordinates of the checkerboard picture based on the number of intersects.
Def eventHander(): # for event in pygame.event.get(): Global initRole if event.type == QUIT:# The event type is pyGame.quit () sys.exit() if event.type == MOUSEBUTTONDOWN: I =0 j=0 for temp in initChessList: for point in temp: x,y = pygame.mouse. Get_pos () If x>=point.x +10 and x<=point.x+10 and y>=point.y +10 and y<=point.y+10: if point.value == 0 and initRole == 1: Value = 1 # judgeResult(I,j,1) initRole =2 # Switch roles elif point.value == 0 and initRole ==2: # when the board position is empty; Value = 2 # When the mouse is clicked, the pawn is black judgeResult(I,j,2) initRole =1 # Switch role break j+=1 I +=1 j=0Copy the code
Here is the check event. There are QUIT exit events and MOUSEBUTTONDOWN mouse click events. In fact, after clicking the mouse, you need to traverse all the positions and calculate whether the position you click is empty. Do not respond if it is not empty. If it’s empty, you need to determine whether you’re playing black or white.
Def judgeResult(I,j,value): # resultFlag flag = False for x in range(j,j + 5): If x >= 0 and x + 4 < 15: if initChessList[i][x].value == value and \ initChessList[i][x + 1].value == value and \ initChessList[i][x + 2].value == value and \ initChessList[i][x + 3].value == value and \ initChessList[i][x + 4].value == value : Flag = True break pass for x in range(i-4, I + 5): if initChessList[x][j].value == value and \ initChessList[x + 1][j].value == value and \ initChessList[x + 2][j].value == value and \ initChessList[x + 3][j].value == value and \ initChessList[x + 4][j].value == value: # First judge the winning/losing x column axis in the northeast diagonal direction, y is the row axis, I is the row and J is the column (right oblique) (go through the edge one by one, For x, y in zip(range(j + 4, j-5, -1), range(i-4, I + 5)): if x >= 0 and x + 4 < 15 and y + 4 >= 0 and y < 15: if initChessList[y][x].value == value and \ initChessList[y - 1][x + 1].value == value and \ initChessList[y - 2][x + 2].value == value and \ initChessList[y - 3][x + 3].value == value and \ initChessList[y - 4][x + 4].value == value: Flag = True # 2. Judge the winning x column axis under the northwest diagonal direction, y is the row axis, I is the row and J is the column (left oblique) (traverse the edge one by one, For x, y in zip(range(j-4, j + 5), range(i-4, I + 5)): if x >= 0 and x + 4 < 15 and y >= 0 and y + 4 < 15: if initChessList[y][x].value == value and \ initChessList[y + 1][x + 1].value == value and \ initChessList[y + 2][x + 2].value == value and \ initChessList[y + 3][x + 3].value == value and \ initChessList[y + 4][x + 4].value == value: Print (" white win "if value ==1 else" black win ")Copy the code
The main idea of the above code is to judge whether to achieve gobang from the horizontal, vertical and oblique aspects, and judge whether to win or lose.
Overall, it’s not too difficult, and the overall code count is a bit long. If you have any questions, please feel free to discuss the procedure with me and answer any questions you may have. Follow the public number: poetic code, make a friend.