python开发五子棋8 #
- UI右侧信息显示
- 人机大战的主函数整体实现
收获 #
了解五子棋人机交战背后的python原理,通过PyGame实践学会独立建立和更新项目,完成五子棋游戏开发
UI右侧信息显示 #
本小节为设定界面UI右侧信息显示的方法,包括目前交战双方执手,以及战况.
########################################在main主函数前,添加下列函数################################################# # 设定界面UI右侧信息显示 def _draw_left_info(screen, font, cur_runner, black_win_count, white_win_count): _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + Stone_Radius2), BLACK_CHESSMAN.Color) _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, Start_X + Stone_Radius2 * 4), WHITE_CHESSMAN.Color) print_text(screen, font, RIGHT_INFO_POS_X, Start_X + 3, '玩家', BLUE_COLOR) print_text(screen, font, RIGHT_INFO_POS_X, Start_X + Stone_Radius2 * 3 + 3, '电脑', BLUE_COLOR) print_text(screen, font, SCREEN_HEIGHT, SCREEN_HEIGHT - Stone_Radius2 * 8, '战况:', BLUE_COLOR) _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, SCREEN_HEIGHT - int(Stone_Radius2 * 4.5)), BLACK_CHESSMAN.Color) _draw_chessman_pos(screen, (SCREEN_HEIGHT + Stone_Radius2, SCREEN_HEIGHT - Stone_Radius2 * 2), WHITE_CHESSMAN.Color) print_text(screen, font, RIGHT_INFO_POS_X, SCREEN_HEIGHT - int(Stone_Radius2 * 5.5) + 3, f'{black_win_count} 胜', BLUE_COLOR) print_text(screen, font, RIGHT_INFO_POS_X, SCREEN_HEIGHT - Stone_Radius2 * 3 + 3, f'{white_win_count} 胜', BLUE_COLOR) def _draw_chessman_pos(screen, pos, stone_color): pygame.gfxdraw.aacircle(screen, pos[0], pos[1], Stone_Radius2, stone_color) pygame.gfxdraw.filled_circle(screen, pos[0], pos[1], Stone_Radius2, stone_color)
人机大战的主函数整体实现 #
本小节为五子棋主函数的整体实现方法,可参考第四节中五子棋人人大战的主函数的实现方法,不同点在于使用AI类替换了原有对手的部分.
##################################将已有的五子棋人人大战的主函数替换为下列函数################################### ########################比较一下五子棋人人大战和人机大战的主函数有哪些相同点和不同点############################# def main(): pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('五子棋') font1 = pygame.font.SysFont('SimHei', 32) font2 = pygame.font.SysFont('SimHei', 72) fwidth, fheight = font2.size('黑方获胜') checkerboard = Checkerboard(Line_Points) cur_runner = BLACK_CHESSMAN winner = None computer = AI(Line_Points, WHITE_CHESSMAN) black_win_count = 0 white_win_count = 0 while True: for event in pygame.event.get(): if event.type == QUIT: sys.exit() elif event.type == KEYDOWN: if event.key == K_RETURN: if winner is not None: winner = None cur_runner = BLACK_CHESSMAN checkerboard = Checkerboard(Line_Points) computer = AI(Line_Points, WHITE_CHESSMAN) elif event.type == MOUSEBUTTONDOWN: if winner is None: pressed_array = pygame.mouse.get_pressed() if pressed_array[0]: mouse_pos = pygame.mouse.get_pos() click_point = _get_clickpoint(mouse_pos) if click_point is not None: if checkerboard.can_drop(click_point): winner = checkerboard.drop(cur_runner, click_point) if winner is None: cur_runner = _get_next(cur_runner) computer.get_opponent_drop(click_point) AI_point = computer.AI_drop() winner = checkerboard.drop(cur_runner, AI_point) if winner is not None: white_win_count += 1 cur_runner = _get_next(cur_runner) else: black_win_count += 1 else: print('超出棋盘区域') # 画棋盘 _draw_checkerboard(screen) # 画棋盘上已有的棋子 for i, row in enumerate(checkerboard.checkerboard): for j, cell in enumerate(row): if cell == BLACK_CHESSMAN.Value: _draw_chessman(screen, Point(j, i), BLACK_CHESSMAN.Color) elif cell == WHITE_CHESSMAN.Value: _draw_chessman(screen, Point(j, i), WHITE_CHESSMAN.Color) _draw_left_info(screen, font1, cur_runner, black_win_count, white_win_count) if winner: print_text(screen, font2, (SCREEN_WIDTH - fwidth)//2, (SCREEN_HEIGHT - fheight)//2, winner.Name + '获胜', RED_COLOR) pygame.display.flip() def _get_next(cur_runner): if cur_runner == BLACK_CHESSMAN: return WHITE_CHESSMAN else: return BLACK_CHESSMAN
小结 #
本节主要讲述了人机交战主函数的实现
需要掌握在游戏界面中进行信息显示的方法