feat(第7讲 字母卡牌): 实现记忆游戏并添加开始界面

- 新增 jiyi.py 文件,实现字母翻牌记忆游戏功能
- 添加 youxijiemian.py 文件,创建游戏开始界面
- 使用 turtle 和 tkinter 模块分别实现游戏和界面
- 支持选择不同难度的游戏模式
This commit is contained in:
sairate 2025-07-02 16:14:01 +08:00
parent 2cd753d3d9
commit ef9378bdd4
6 changed files with 165 additions and 1 deletions

4
test.py Normal file
View File

@ -0,0 +1,4 @@
import turtle
turtle.listen()
turtle.onscreenclick(print)
turtle.done()

View File

@ -0,0 +1,3 @@
```bash
pip install pgzrun -i https://pypi.tuna.tsinghua.edu.cn/simple
```

View File

@ -1,3 +1,3 @@
```bash
pip install pillow -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install pillow amzqr -i https://pypi.tuna.tsinghua.edu.cn/simple
```

View File

@ -0,0 +1,35 @@
import turtle
qipan=[(-50,150),(50,150),(-150,50),(-150,-50)]
t1=turtle.Turtle()
t1.hideturtle()
for i in qipan:
t1.penup()
t1.goto(i[0], i[1])
t1.pendown()
if qipan.index(i)<=1:
t1.setheading(-90)
t1.forward(300)
else:
t1.setheading(0)
t1.forward(300)
text1="O"
text2="X"
num = 0
def click(x,y):
global num
t1.penup()
t1.goto(x-20,y-40)
t1.pendown()
if num%2==0:
t1.write(text1, font=("Arial", 50, "normal"))
else:
t1.write(text2, font=("Arial", 50, "normal"))
num+=1
turtle.listen()
turtle.onscreenclick(click)
turtle.done()

View File

@ -0,0 +1,68 @@
import tkinter as tk
from tkinter import messagebox
class SneakyTicTacToe:
def __init__(self, root):
self.root = root
self.size = 7 # 实际棋盘大小
self.board = [["" for _ in range(self.size)] for _ in range(self.size)]
self.current_player = "X"
self.buttons = [[None for _ in range(self.size)] for _ in range(self.size)]
for i in range(self.size):
for j in range(self.size):
btn = tk.Button(root, text="", font=("Arial", 24), width=3, height=1,
command=lambda r=i, c=j: self.on_click(r, c))
btn.grid(row=i+1, column=j)
self.buttons[i][j] = btn
# 默认只设置窗口足够显示中心3x3
root.geometry(f"{3*65}x{3*65}") # 宽x高像素+40 留状态栏
def on_click(self, r, c):
if self.board[r][c] != "":
return
self.board[r][c] = self.current_player
self.buttons[r][c].config(text=self.current_player)
if self.check_winner(self.current_player):
messagebox.showinfo("🎉 游戏结束", f"玩家 {self.current_player} 获胜!")
self.reset()
elif self.is_draw():
messagebox.showinfo("😐 游戏结束", "平局")
self.reset()
else:
self.current_player = "O" if self.current_player == "X" else "X"
self.status.config(text=f"当前轮到: {self.current_player}")
def check_winner(self, player):
for i in range(self.size):
for j in range(self.size):
if j + 2 < self.size and all(self.board[i][j+k] == player for k in range(3)):
return True
if i + 2 < self.size and all(self.board[i+k][j] == player for k in range(3)):
return True
if i + 2 < self.size and j + 2 < self.size and all(self.board[i+k][j+k] == player for k in range(3)):
return True
if i + 2 < self.size and j - 2 >= 0 and all(self.board[i+k][j-k] == player for k in range(3)):
return True
return False
def is_draw(self):
return all(self.board[i][j] != "" for i in range(self.size) for j in range(self.size))
def reset(self):
for i in range(self.size):
for j in range(self.size):
self.board[i][j] = ""
self.buttons[i][j].config(text="")
self.current_player = "X"
self.status.config(text="当前轮到: X")
# 启动游戏
if __name__ == "__main__":
root = tk.Tk()
game = SneakyTicTacToe(root)
root.mainloop()

View File

@ -0,0 +1,54 @@
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()