龙空技术网

用python写一个五子棋,跟自己下棋

积极的python程序猿 770

前言:

现时姐妹们对“用python做88的棋盘01程序”可能比较看重,同学们都需要知道一些“用python做88的棋盘01程序”的相关资讯。那么小编也在网络上收集了一些关于“用python做88的棋盘01程序””的相关资讯,希望我们能喜欢,同学们快快来了解一下吧!

程序使用 Python 的 Pygame 库。以下是一个简单的五子棋游戏可视化代码:

import pygameimport numpy as np# Initialize Pygamepygame.init()# Set up windowWINDOW_WIDTH = 600WINDOW_HEIGHT = 600window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))pygame.display.set_caption("五子棋")# Set up colorsBLACK = (0, 0, 0)WHITE = (255, 255, 255)GRAY = (128, 128, 128)# Set up fontfont = pygame.font.SysFont(None, 40)# Create boardboard = np.zeros((15,15))# Set up game variablescurrent_player = 1winner = 0game_over = False# Set up functionsdef draw_board():    window.fill(WHITE)    for row in range(15):        for col in range(15):             pygame.draw.rect(window, BLACK, (row*40+50, col*40+50, 40, 40), 2)            pygame.draw.rect(window, BLACK, (50, 50, 560, 560), 2)            pygame.draw.circle(window, BLACK, (320, 320), 5, 0)            pygame.draw.circle(window, BLACK, (160, 160), 5, 0)            pygame.draw.circle(window, BLACK, (480, 160), 5, 0)            pygame.draw.circle(window, BLACK, (160, 480), 5, 0)           pygame.draw.circle(window, BLACK, (480, 480), 5, 0)def draw_pieces():    for row in range(15):        for col in range(15):            if board[row][col] == 1:                pygame.draw.circle(window, BLACK, (row*40+50, col*40+50), 18, 0)           elif board[row][col] == 2:                pygame.draw.circle(window, GRAY, (row*40+50, col*40+50), 18, 0)def place_piece(row, col):     global current_player     if board[row][col] == 0:        board[row][col] = current_player    if current_player == 1:        current_player = 2    else:        current_player = 1def get_winner():# check rows    for row in range(15):for col in range(11):if board[row][col] == board[row][col+1] == board[row][col+2] == board[row][col+3] == board[row][col+4] and board[row][col] != 0:return board[row][col]# check columnsfor col in range(15):for row in range(11):if board[row][col] == board[row+1][col] == board[row+2][col] == board[row+3][col] == board[row+4][col] and board[row][col] != 0:return board[row][col]# check diagonalfor row in range(11):for col in range(11):if board[row][col] == board[row+1][col+1] == board[row+2][col+2] == board[row+3][col+3] == board[row+4][col+4] and board[row][col] != 0:return board[row][col]# check reverse diagonalfor row in range(11):for col in range(4, 15):if board[row][col] == board[row+1][col-1] == board[row+2][col-2] == board[row+3][col-3] == board[row+4][col-4] and board[row][col] != 0:return board[row][col]return 0# Main game loopwhile not game_over:for event in pygame.event.get():if event.type == pygame.QUIT:game_over = Trueelif event.type == pygame.MOUSEBUTTONDOWN:if winner == 0:x, y = event.posrow = int((y-50) / 40)col = int((x-50) / 40)place_piece(row, col)winner = get_winner()draw_board()draw_pieces()if winner == 0:text = font.render("Player " + str(current_player) + ", your turn", True, BLACK)else:text = font.render("Player " + str(winner) + " wins!", True, BLACK)text_rect = text.get_rect(center=(WINDOW_WIDTH/2, 20))window.blit(text, text_rect)pygame.display.update()# Quit Pygamepygame.quit()

这个代码创建了一个可视化的五子棋游戏,并允许两个玩家轮流落子。当有任意一方在横、竖、斜方向上连接了五个棋子时,游戏结束并宣布胜者。玩家可以通过鼠标点击棋盘上的空格来落子。游戏结束后,玩家可以通过关闭窗口来退出游戏。

图片来源于网络

标签: #用python做88的棋盘01程序