使用python中的turtle模块写一个井字游戏
时间: 2024-02-08 07:24:19 浏览: 39
好的,下面是一个使用Python的turtle模块实现的井字游戏的示例代码:
```python
import turtle
# 初始化游戏界面
screen = turtle.Screen()
screen.title("井字游戏")
screen.bgcolor("white")
screen.setup(width=600, height=600)
screen.tracer(0)
# 创建井字棋盘
board = [[" " for _ in range(3)] for _ in range(3)]
# 绘制井字棋盘格子
def draw_board():
turtle.penup()
turtle.goto(-200, 200)
turtle.pendown()
turtle.pensize(3)
for _ in range(2):
turtle.forward(600)
turtle.right(90)
turtle.forward(200)
turtle.right(90)
turtle.penup()
# 绘制棋子
def draw_piece(row, col, piece):
x = -200 + col * 200
y = 200 - row * 200
turtle.goto(x, y)
turtle.pendown()
if piece == "X":
turtle.goto(x + 150, y - 150)
turtle.goto(x, y)
turtle.goto(x + 150, y)
turtle.goto(x, y - 150)
else:
turtle.circle(75)
turtle.penup()
# 处理玩家点击事件
def handle_click(x, y):
col = int((x + 200) // 200)
row = int((200 - y) // 200)
if board[row][col] == " ":
board[row][col] = "X"
draw_piece(row, col, "X")
if check_winner("X"):
turtle.goto(-200, -250)
turtle.write("玩家 X 胜利!", align="center", font=("Arial", 24, "normal"))
turtle.done()
return
if is_board_full():
turtle.goto(-200, -250)
turtle.write("平局!", align="center", font=("Arial", 24, "normal"))
turtle.done()
return
computer_move()
if check_winner("O"):
turtle.goto(-200, -250)
turtle.write("玩家 O 胜利!", align="center", font=("Arial", 24, "normal"))
turtle.done()
return
# 检查是否有玩家获胜
def check_winner(piece):
for i in range(3):
if board[i][0] == board[i][1] == board[i][2] == piece:
return True
if board[0][i] == board[1][i] == board[2][i] == piece:
return True
if board[0][0] == board[1][1] == board[2][2] == piece:
return True
if board[0][2] == board[1][1] == board[2][0] == piece:
return True
return False
# 检查棋盘是否已满
def is_board_full():
for row in board:
if " " in row:
return False
return True
# 计算电脑下棋位置
def computer_move():
for i in range(3):
for j in range(3):
if board[i][j] == " ":
board[i][j] = "O"
draw_piece(i, j, "O")
return
# 注册点击事件处理函数
turtle.onscreenclick(handle_click)
# 绘制井字棋盘
draw_board()
turtle.done()
```
运行以上代码,将会弹出一个窗口,你可以点击窗口中的格子来进行游戏。玩家使用 X 表示,电脑使用 O 表示。当有玩家获胜或者平局时,会在窗口底部显示相应的消息并结束游戏。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](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)