用python基于Py.game实现四子棋游戏让玩家和电脑对战
时间: 2024-02-06 19:10:55 浏览: 45
好的,以下是用Python基于Pygame实现四子棋游戏让玩家和电脑对战的代码。
首先,我们需要安装Pygame模块,可以使用以下命令进行安装:
`pip install pygame`
然后,我们来编写代码:
```python
import pygame
import random
# 初始化Pygame
pygame.init()
# 游戏窗口大小
WINDOW_SIZE = (640, 640)
# 棋盘大小
BOARD_SIZE = (600, 600)
# 棋格大小
CELL_SIZE = 75
# 棋盘起始位置
BOARD_START = (20, 20)
# 玩家棋子颜色
PLAYER_COLOR = (255, 0, 0)
# 电脑棋子颜色
COMPUTER_COLOR = (0, 0, 255)
# 背景颜色
BACKGROUND_COLOR = (255, 255, 255)
# 游戏结束标志
game_over = False
# 初始化棋盘
board = [[0 for i in range(8)] for j in range(8)]
# 初始化Pygame窗口
screen = pygame.display.set_mode(WINDOW_SIZE)
# 设置窗口标题
pygame.display.set_caption('四子棋游戏')
# 绘制棋盘
def draw_board():
for i in range(8):
for j in range(8):
x = BOARD_START[0] + i * CELL_SIZE
y = BOARD_START[1] + j * CELL_SIZE
pygame.draw.rect(screen, (0, 0, 0), (x, y, CELL_SIZE, CELL_SIZE), 1)
# 绘制棋子
def draw_piece(row, col, color):
x = BOARD_START[0] + col * CELL_SIZE + CELL_SIZE // 2
y = BOARD_START[1] + row * CELL_SIZE + CELL_SIZE // 2
pygame.draw.circle(screen, color, (x, y), CELL_SIZE // 2 - 5)
# 检查游戏是否结束
def check_game_over():
for i in range(8):
for j in range(8):
if board[i][j] == 0:
continue
if i < 5 and board[i][j] == board[i + 1][j] == board[i + 2][j] == board[i + 3][j]:
return True
if j < 5 and board[i][j] == board[i][j + 1] == board[i][j + 2] == board[i][j + 3]:
return True
if i < 5 and j < 5 and board[i][j] == board[i + 1][j + 1] == board[i + 2][j + 2] == board[i + 3][j + 3]:
return True
if i < 5 and j > 2 and board[i][j] == board[i + 1][j - 1] == board[i + 2][j - 2] == board[i + 3][j - 3]:
return True
return False
# 电脑下棋
def computer_move():
while True:
row = random.randint(0, 7)
col = random.randint(0, 7)
if board[row][col] == 0:
board[row][col] = 2
draw_piece(row, col, COMPUTER_COLOR)
break
# 主循环
while not game_over:
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 1:
x, y = event.pos
row = (y - BOARD_START[1]) // CELL_SIZE
col = (x - BOARD_START[0]) // CELL_SIZE
if board[row][col] == 0:
board[row][col] = 1
draw_piece(row, col, PLAYER_COLOR)
if check_game_over():
print('玩家获胜')
game_over = True
else:
computer_move()
if check_game_over():
print('电脑获胜')
game_over = True
# 绘制背景和棋盘
screen.fill(BACKGROUND_COLOR)
draw_board()
# 绘制棋子
for i in range(8):
for j in range(8):
if board[i][j] == 1:
draw_piece(i, j, PLAYER_COLOR)
elif board[i][j] == 2:
draw_piece(i, j, COMPUTER_COLOR)
# 更新窗口
pygame.display.update()
# 退出Pygame
pygame.quit()
```
运行代码后,将会出现一个四子棋游戏的窗口,玩家先手,点击窗口中的棋盘可以下棋,电脑会随机下棋,直到有一方获胜或者平局。在游戏结束后,控制台会输出获胜方。
希望这个示例代码可以帮助到你!
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![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_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)