用python设计一个五子棋代码,要求运行界面可视化
时间: 2024-02-16 10:04:53 浏览: 106
python课程作业设计五子棋游戏源代码.zip
好的,这是一个比较复杂的项目,需要使用Python的图形库来实现可视化界面。其中,我会使用Pygame这个库来实现五子棋游戏的可视化界面。以下是五子棋的代码实现:
```python
import pygame
import sys
import numpy as np
# 初始化界面
pygame.init()
WIDTH = 600
HEIGHT = 600
LINE_WIDTH = 15
BOARD_ROWS = 15
BOARD_COLS = 15
CIRCLE_RADIUS = int(WIDTH / (BOARD_COLS * 2))
CIRCLE_WIDTH = 15
BG_COLOR = (236, 217, 199)
LINE_COLOR = (0, 0, 0)
PLAYER1_COLOR = (255, 0, 0)
PLAYER2_COLOR = (0, 0, 255)
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('五子棋')
screen.fill(BG_COLOR)
# 初始化棋盘
def create_board():
board = np.zeros((BOARD_ROWS, BOARD_COLS))
return board
board = create_board()
# 画棋盘
def draw_lines():
for i in range(BOARD_ROWS):
pygame.draw.line(screen, LINE_COLOR, (LINE_WIDTH, i * int(HEIGHT / BOARD_ROWS) + LINE_WIDTH), (WIDTH - LINE_WIDTH, i * int(HEIGHT / BOARD_ROWS) + LINE_WIDTH), LINE_WIDTH)
pygame.draw.line(screen, LINE_COLOR, (i * int(WIDTH / BOARD_COLS) + LINE_WIDTH, LINE_WIDTH), (i * int(WIDTH / BOARD_COLS) + LINE_WIDTH, HEIGHT - LINE_WIDTH), LINE_WIDTH)
# 画棋子
def draw_piece(row, col, player):
if player == 1:
color = PLAYER1_COLOR
else:
color = PLAYER2_COLOR
pygame.draw.circle(screen, color, (int(col * int(WIDTH / BOARD_COLS) + int(WIDTH / BOARD_COLS / 2)), int(row * int(HEIGHT / BOARD_ROWS) + int(HEIGHT / BOARD_ROWS / 2))), CIRCLE_RADIUS, CIRCLE_WIDTH)
# 检查胜利条件
def is_win(player):
# 横向检查
for r in range(BOARD_ROWS):
for c in range(BOARD_COLS - 4):
if board[r][c] == player and board[r][c+1] == player and board[r][c+2] == player and board[r][c+3] == player and board[r][c+4] == player:
return True
# 竖向检查
for r in range(BOARD_ROWS - 4):
for c in range(BOARD_COLS):
if board[r][c] == player and board[r+1][c] == player and board[r+2][c] == player and board[r+3][c] == player and board[r+4][c] == player:
return True
# 左上到右下检查
for r in range(BOARD_ROWS - 4):
for c in range(BOARD_COLS - 4):
if board[r][c] == player and board[r+1][c+1] == player and board[r+2][c+2] == player and board[r+3][c+3] == player and board[r+4][c+4] == player:
return True
# 右上到左下检查
for r in range(BOARD_ROWS - 4):
for c in range(4, BOARD_COLS):
if board[r][c] == player and board[r+1][c-1] == player and board[r+2][c-2] == player and board[r+3][c-3] == player and board[r+4][c-4] == player:
return True
return False
# 主函数
def main():
player = 1
game_over = False
draw_lines()
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouseX = event.pos[0]
mouseY = event.pos[1]
clicked_row = int(mouseY // (HEIGHT / BOARD_ROWS))
clicked_col = int(mouseX // (WIDTH / BOARD_COLS))
if board[clicked_row][clicked_col] == 0:
draw_piece(clicked_row, clicked_col, player)
board[clicked_row][clicked_col] = player
if is_win(player):
game_over = True
print('Player ' + str(player) + ' wins!')
player = player % 2 + 1
if game_over:
pygame.time.wait(3000)
pygame.display.update()
main()
pygame.quit()
```
运行以上代码,即可实现五子棋的游戏界面。
阅读全文