sairate ef9378bdd4 feat(第7讲 字母卡牌): 实现记忆游戏并添加开始界面
- 新增 jiyi.py 文件,实现字母翻牌记忆游戏功能
- 添加 youxijiemian.py 文件,创建游戏开始界面
- 使用 turtle 和 tkinter 模块分别实现游戏和界面
- 支持选择不同难度的游戏模式
2025-07-02 16:14:01 +08:00

55 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import turtle
# 设置屏幕和画笔
screen = turtle.Screen()
screen.title("简易井字棋")
pen = turtle.Turtle()
pen.hideturtle()
pen.speed(0)
pen.pensize(3)
# 当前玩家X 先手)
current_player = "X"
# 保存每格的内容3x3
board = [["" for _ in range(3)] for _ in range(3)]
# 画井字棋的格子
def draw_board():
for i in range(0, 3): # 画2条竖线
pen.penup()
pen.goto(-100 + i * 100, -150)
pen.pendown()
pen.goto(-100 + i * 100, 150)
for i in range(0, 3): # 画2条横线
pen.penup()
pen.goto(-150, -100 + i * 100)
pen.pendown()
pen.goto(150, -100 + i * 100)
# 在某个格子画 X 或 O
def draw_symbol(row, col, symbol):
x = -150 + col * 100 + 50
y = 150 - row * 100 - 90
pen.penup()
pen.goto(x, y)
pen.write(symbol, align="center", font=("Arial", 36, "bold"))
# 处理点击事件
def click(x, y):
global current_player
# 将坐标转换为格子位置
if not (-150 < x < 150 and -150 < y < 150):
return
col = int((x + 150) // 100)
row = int((150 - y) // 100)
if board[row][col] == "":
board[row][col] = current_player
draw_symbol(row, col, current_player)
# 切换玩家
current_player = "O" if current_player == "X" else "X"
# 运行程序
draw_board()
screen.onclick(click)
screen.mainloop()