写一篇基于python的五子棋小游戏可视化代码
时间: 2023-08-01 15:06:12 浏览: 71
好的,以下是基于Python的五子棋小游戏可视化代码:
```
import pygame
# 定义棋盘大小和格子大小
BOARD_SIZE = 15
CELL_SIZE = 40
# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (200, 200, 200)
# 初始化pygame
pygame.init()
# 创建游戏窗口
screen = pygame.display.set_mode((BOARD_SIZE * CELL_SIZE, BOARD_SIZE * CELL_SIZE))
pygame.display.set_caption("五子棋小游戏")
# 加载棋子图片
black_stone = pygame.image.load("black.png")
white_stone = pygame.image.load("white.png")
# 初始化棋盘数组
board = [[0 for i in range(BOARD_SIZE)] for j in range(BOARD_SIZE)]
# 判断是否下棋成功
def check_win(x, y, player):
# 横向判断
count = 0
for i in range(BOARD_SIZE):
if board[x][i] == player:
count += 1
if count == 5:
return True
else:
count = 0
# 竖向判断
count = 0
for i in range(BOARD_SIZE):
if board[i][y] == player:
count += 1
if count == 5:
return True
else:
count = 0
# 斜向判断
count = 0
for i in range(-4, 5):
if x + i < 0 or x + i >= BOARD_SIZE or y + i < 0 or y + i >= BOARD_SIZE:
continue
if board[x + i][y + i] == player:
count += 1
if count == 5:
return True
else:
count = 0
# 反斜向判断
count = 0
for i in range(-4, 5):
if x + i < 0 or x + i >= BOARD_SIZE or y - i < 0 or y - i >= BOARD_SIZE:
continue
if board[x + i][y - i] == player:
count += 1
if count == 5:
return True
else:
count = 0
return False
# 绘制棋盘和棋子
def draw_board():
# 绘制棋盘背景
screen.fill(GRAY)
# 绘制棋盘网格
for i in range(BOARD_SIZE):
pygame.draw.line(screen, BLACK, (0, i * CELL_SIZE), (BOARD_SIZE * CELL_SIZE, i * CELL_SIZE))
pygame.draw.line(screen, BLACK, (i * CELL_SIZE, 0), (i * CELL_SIZE, BOARD_SIZE * CELL_SIZE))
# 绘制棋子
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i][j] == 1:
screen.blit(black_stone, (i * CELL_SIZE, j * CELL_SIZE))
elif board[i][j] == 2:
screen.blit(white_stone, (i * CELL_SIZE, j * CELL_SIZE))
# 游戏主循环
def main():
# 初始化棋盘
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
board[i][j] = 0
# 初始化玩家
player = 1
# 游戏开始
while True:
# 处理游戏事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
# 获取鼠标点击位置
x, y = event.pos[0] // CELL_SIZE, event.pos[1] // CELL_SIZE
# 判断下棋位置是否合法
if x < 0 or x >= BOARD_SIZE or y < 0 or y >= BOARD_SIZE:
continue
if board[x][y] != 0:
continue
# 下棋
board[x][y] = player
# 绘制棋盘和棋子
draw_board()
# 判断是否胜利
if check_win(x, y, player):
pygame.time.delay(200)
if player == 1:
print("黑方获胜!")
else:
print("白方获胜!")
# 重新开始游戏
pygame.time.delay(2000)
main()
# 切换玩家
player = 1 if player == 2 else 2
# 绘制棋盘和棋子
draw_board()
# 刷新屏幕
pygame.display.flip()
# 启动游戏
main()
```
以上代码实现了一个基本的五子棋游戏可视化,支持玩家与玩家对战,在窗口中可以进行操作和查看游戏状态。使用者可以根据需要进行修改和扩展。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)