写一个带画面的五子棋程序,用Python写
时间: 2023-09-27 12:04:39 浏览: 34
好的,以下是一个简单的五子棋程序,使用 Python 语言编写。
```python
import pygame
import numpy as np
# 初始化 pygame
pygame.init()
# 游戏界面大小
SCREEN_SIZE = (640, 640)
# 棋盘大小
BOARD_SIZE = (480, 480)
# 棋盘起始坐标
BOARD_POS = ((SCREEN_SIZE[0]-BOARD_SIZE[0])//2, (SCREEN_SIZE[1]-BOARD_SIZE[1])//2)
# 棋盘网格大小
GRID_SIZE = 30
# 棋子半径
STONE_RADIUS = GRID_SIZE//2 - 2
# 定义黑白棋子颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# 创建游戏界面
screen = pygame.display.set_mode(SCREEN_SIZE)
pygame.display.set_caption('五子棋')
# 创建棋盘数组
board = np.zeros((15, 15))
# 判断游戏是否结束
game_over = False
# 判断当前下棋方
current_player = 1
# 绘制棋盘
def draw_board():
for i in range(15):
pygame.draw.line(screen, BLACK, (BOARD_POS[0], BOARD_POS[1] + i*GRID_SIZE), (BOARD_POS[0]+BOARD_SIZE[0], BOARD_POS[1] + i*GRID_SIZE))
pygame.draw.line(screen, BLACK, (BOARD_POS[0] + i*GRID_SIZE, BOARD_POS[1]), (BOARD_POS[0] + i*GRID_SIZE, BOARD_POS[1]+BOARD_SIZE[1]))
# 绘制棋子
def draw_stone(row, col, player):
pos = (BOARD_POS[0] + col*GRID_SIZE, BOARD_POS[1] + row*GRID_SIZE)
if player == 1:
pygame.draw.circle(screen, BLACK, pos, STONE_RADIUS)
else:
pygame.draw.circle(screen, WHITE, pos, STONE_RADIUS)
# 判断胜负
def check_win(row, col, player):
# 判断横向是否连成五子
count = 0
for i in range(5):
if col-i >= 0 and board[row][col-i] == player:
count += 1
else:
break
for i in range(1, 5):
if col+i < 15 and board[row][col+i] == player:
count += 1
else:
break
if count >= 5:
return True
# 判断纵向是否连成五子
count = 0
for i in range(5):
if row-i >= 0 and board[row-i][col] == player:
count += 1
else:
break
for i in range(1, 5):
if row+i < 15 and board[row+i][col] == player:
count += 1
else:
break
if count >= 5:
return True
# 判断左上到右下是否连成五子
count = 0
for i in range(5):
if row-i >= 0 and col-i >= 0 and board[row-i][col-i] == player:
count += 1
else:
break
for i in range(1, 5):
if row+i < 15 and col+i < 15 and board[row+i][col+i] == player:
count += 1
else:
break
if count >= 5:
return True
# 判断左下到右上是否连成五子
count = 0
for i in range(5):
if row+i < 15 and col-i >= 0 and board[row+i][col-i] == player:
count += 1
else:
break
for i in range(1, 5):
if row-i >= 0 and col+i < 15 and board[row-i][col+i] == player:
count += 1
else:
break
if count >= 5:
return True
return False
# 游戏循环
while True:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN and not game_over:
if event.button == 1:
x, y = event.pos
col = (x - BOARD_POS[0]) // GRID_SIZE
row = (y - BOARD_POS[1]) // GRID_SIZE
if col >= 0 and col < 15 and row >= 0 and row < 15 and board[row][col] == 0:
board[row][col] = current_player
draw_stone(row, col, current_player)
if check_win(row, col, current_player):
game_over = True
print('Player %d win!' % current_player)
current_player = 3 - current_player
# 绘制界面
screen.fill((200, 200, 200))
draw_board()
for i in range(15):
for j in range(15):
if board[i][j] != 0:
draw_stone(i, j, board[i][j])
pygame.display.update()
```
运行程序后,会打开一个游戏界面,黑色棋子先手。点击左键可以在棋盘上下棋,直到有一方连成五子或棋盘下满为止,游戏结束。