feat(第7讲 字母卡牌): 实现记忆游戏并添加开始界面
- 新增 jiyi.py 文件,实现字母翻牌记忆游戏功能 - 添加 youxijiemian.py 文件,创建游戏开始界面 - 使用 turtle 和 tkinter 模块分别实现游戏和界面 - 支持选择不同难度的游戏模式
This commit is contained in:
parent
fa4660abed
commit
d4c4cd1af9
89
test.py
89
test.py
@ -1,74 +1,19 @@
|
||||
students={
|
||||
"0001":{
|
||||
"name":"张三",
|
||||
"sex":"男",
|
||||
"age":18,
|
||||
"score":[90,80,70]
|
||||
},
|
||||
"0002":{
|
||||
"name":"李四",
|
||||
"sex":"女",
|
||||
"age":19,
|
||||
"score":[80,90,80]
|
||||
}
|
||||
import datetime
|
||||
dict={
|
||||
0:'星期一',
|
||||
1:'星期二',
|
||||
2:'星期三',
|
||||
3:'星期四',
|
||||
4:'星期五',
|
||||
5:'星期六',
|
||||
6:'星期日'
|
||||
}
|
||||
print(datetime.datetime.now())
|
||||
print(datetime.datetime.now().year)
|
||||
print(datetime.datetime.now().month)
|
||||
print(datetime.datetime.now().day)
|
||||
print(datetime.datetime.now().hour)
|
||||
print(datetime.datetime.now().minute)
|
||||
print(datetime.datetime.now().second)
|
||||
print(dict[datetime.datetime.now().weekday()])
|
||||
|
||||
while True:
|
||||
print("="*5+"学生管理系统"+"="*5)
|
||||
print("1.查询学生信息")
|
||||
print("2.添加学生信息")
|
||||
print("3.删除学生信息")
|
||||
print("4.修改学生信息")
|
||||
print("5.列出所有学生信息")
|
||||
print("6.退出")
|
||||
choice=input("请输入你的选择:")
|
||||
if choice=="1":
|
||||
name=input("请输入学生姓名:")
|
||||
for k,v in students.items():
|
||||
if v["name"]==name:
|
||||
print("学号 \t姓名 \t性别 \t年龄 \t语文成绩 \t数学成绩 \t英语成绩")
|
||||
print("%s \t%s \t%s \t%d \t%d \t%d \t%d" % (k, v["name"], v["sex"], v["age"], v["score"][0], v["score"][1], v["score"][2]))
|
||||
break
|
||||
else:
|
||||
print("没有此学生")
|
||||
elif choice=="2":
|
||||
sid=input("请输入学号:")
|
||||
if students.get(sid):
|
||||
print("此学号已存在")
|
||||
else:
|
||||
name=input("请输入学生姓名:")
|
||||
sex=input("请输入学生性别:")
|
||||
age=int(input("请输入学生年龄:"))
|
||||
score=[int(x) for x in input("请输入学生成绩:").split(",")]
|
||||
students[sid]={"name":name,"sex":sex,"age":age,"score":score}
|
||||
print("添加成功")
|
||||
elif choice== "3":
|
||||
name=input("请输入学生姓名:")
|
||||
for k,v in students.items():
|
||||
if v["name"]==name:
|
||||
del students[k]
|
||||
print("删除成功")
|
||||
break
|
||||
else:
|
||||
print("没有此学生")
|
||||
elif choice=="4":
|
||||
name=input("请输入学生姓名:")
|
||||
for k,v in students.items():
|
||||
if v["name"]==name:
|
||||
v["name"]=input("请输入学生姓名:")
|
||||
v["sex"]=input("请输入学生性别:")
|
||||
v["age"]=int(input("请输入学生年龄:"))
|
||||
v["score"]=[int(x) for x in input("请输入学生成绩:").split(",")]
|
||||
print("修改成功")
|
||||
break
|
||||
else:
|
||||
print("没有此学生")
|
||||
elif choice=="5":
|
||||
print("所有学生信息为:")
|
||||
print("学号 \t姓名 \t性别 \t年龄 \t语文成绩 \t数学成绩 \t英语成绩")
|
||||
for k,v in students.items():
|
||||
print("%s\t%s \t%s \t%d \t%d \t%d \t%d"%(k,v["name"],v["sex"],v["age"],v["score"][0],v["score"][1],v["score"][2]))
|
||||
elif choice== "6":
|
||||
break
|
||||
else:
|
||||
print("输入错误")
|
||||
3
第10讲识图精灵/课堂成果/README.md
Normal file
3
第10讲识图精灵/课堂成果/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
```bash
|
||||
pip install baidu-aip chardet -i https://pypi.tuna.tsinghua.edu.cn/simple
|
||||
```
|
||||
75
第10讲识图精灵/课堂成果/摄像头.py
Normal file
75
第10讲识图精灵/课堂成果/摄像头.py
Normal file
@ -0,0 +1,75 @@
|
||||
import cv2
|
||||
import tkinter as tk
|
||||
from tkinter import messagebox
|
||||
from PIL import Image, ImageTk
|
||||
from aip import AipImageClassify
|
||||
import tempfile
|
||||
|
||||
# 百度API信息
|
||||
APP_ID = '118721834'
|
||||
API_KEY = 'ib9k87nafhhLc8yHJh8mToXA'
|
||||
SECRET_KEY = 'PJsKue6TCMsTHWjtKtCRI25onh4YfZ5j'
|
||||
|
||||
client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)
|
||||
|
||||
# 识别函数
|
||||
def recognize_image(image_path):
|
||||
with open(image_path, 'rb') as f:
|
||||
img_data = f.read()
|
||||
result = client.advancedGeneral(img_data)
|
||||
if 'result' in result:
|
||||
return [(i['keyword'], i['score']) for i in result['result'] if i['score'] >= 0.6]
|
||||
else:
|
||||
return [("识别失败", 0.0)]
|
||||
|
||||
# UI类
|
||||
class CameraApp:
|
||||
def __init__(self, window):
|
||||
self.window = window
|
||||
self.window.title("拍照识别 - 百度AI")
|
||||
self.video_capture = cv2.VideoCapture(0)
|
||||
|
||||
self.canvas = tk.Canvas(window, width=640, height=480)
|
||||
self.canvas.pack()
|
||||
|
||||
self.btn = tk.Button(window, text="📸 拍照识别", command=self.capture_and_recognize)
|
||||
self.btn.pack(pady=10)
|
||||
|
||||
self.result_label = tk.Label(window, text="", font=("微软雅黑", 12))
|
||||
self.result_label.pack()
|
||||
|
||||
self.update_video()
|
||||
|
||||
def update_video(self):
|
||||
ret, frame = self.video_capture.read()
|
||||
if ret:
|
||||
self.current_frame = frame
|
||||
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||||
img = Image.fromarray(cv2image)
|
||||
imgtk = ImageTk.PhotoImage(image=img)
|
||||
self.canvas.create_image(0, 0, anchor=tk.NW, image=imgtk)
|
||||
self.canvas.imgtk = imgtk
|
||||
self.window.after(30, self.update_video)
|
||||
|
||||
def capture_and_recognize(self):
|
||||
if hasattr(self, 'current_frame'):
|
||||
with tempfile.NamedTemporaryFile(suffix='.jpg', delete=False) as tmp:
|
||||
img_path = tmp.name
|
||||
cv2.imwrite(img_path, self.current_frame)
|
||||
|
||||
results = recognize_image(img_path)
|
||||
if results:
|
||||
text = "\n".join([f"✅ {k} ({score*100:.1f}%)" for k, score in results])
|
||||
self.result_label.config(text=text)
|
||||
else:
|
||||
self.result_label.config(text="❌ 无法识别")
|
||||
|
||||
def __del__(self):
|
||||
if self.video_capture.isOpened():
|
||||
self.video_capture.release()
|
||||
|
||||
# 启动应用
|
||||
if __name__ == '__main__':
|
||||
root = tk.Tk()
|
||||
app = CameraApp(root)
|
||||
root.mainloop()
|
||||
@ -9,9 +9,9 @@ https://huewq7h021.feishu.cn/wiki/Ry3UwaoceiMRXgklbWtc9mEUn9f?from=from_copylink
|
||||
from aip import AipImageClassify # 调用百度智能模块中图像识别类方法
|
||||
|
||||
# 填写个人的授权使用信息
|
||||
APP_ID = '你的 AppID'
|
||||
API_KEY = '你的 API Key'
|
||||
SECRET_KEY = '你的 Secret Key'
|
||||
APP_ID = '118721834'
|
||||
API_KEY = 'ib9k87nafhhLc8yHJh8mToXA'
|
||||
SECRET_KEY = 'PJsKue6TCMsTHWjtKtCRI25onh4YfZ5j'
|
||||
|
||||
# 准备【图像识别器】
|
||||
client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)
|
||||
|
||||
@ -9,15 +9,15 @@ https://huewq7h021.feishu.cn/wiki/Ry3UwaoceiMRXgklbWtc9mEUn9f?from=from_copylink
|
||||
from aip import AipSpeech
|
||||
|
||||
# 定义常量
|
||||
APP_ID = '你的 AppID'
|
||||
API_KEY = '你的 API Key'
|
||||
SECRET_KEY = '你的 Secret Key'
|
||||
APP_ID = '117920031'
|
||||
API_KEY = '4icZSO1OlMCU2ZiRMhgGCXFu'
|
||||
SECRET_KEY = '6wJldJ08m1jIX9hb0ULcJrIJ9D1OJW3c'
|
||||
|
||||
# 准备【语音合成器】
|
||||
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
|
||||
|
||||
# 要合成的文本
|
||||
text = "你好,我是P仔,很高兴认识你"
|
||||
text = "歪比巴卜"
|
||||
|
||||
# 调用【语音合成器】
|
||||
res = client.synthesis(text, 'zh', 1, {
|
||||
|
||||
@ -5,13 +5,13 @@
|
||||
账号注册指南:
|
||||
https://huewq7h021.feishu.cn/wiki/Ry3UwaoceiMRXgklbWtc9mEUn9f?from=from_copylink
|
||||
"""
|
||||
|
||||
# pip install -i https://mirrors.aliyun.com/pypi/simple/ baidu-aip pyaudio keyboard chardet
|
||||
from aip import AipSpeech
|
||||
|
||||
# 定义常量
|
||||
APP_ID = '你的 AppID'
|
||||
API_KEY = '你的 API Key'
|
||||
SECRET_KEY = '你的 Secret Key'
|
||||
APP_ID = '117920031'
|
||||
API_KEY = '4icZSO1OlMCU2ZiRMhgGCXFu'
|
||||
SECRET_KEY = '6wJldJ08m1jIX9hb0ULcJrIJ9D1OJW3c'
|
||||
|
||||
# 准备【语音识别器】
|
||||
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
|
||||
|
||||
@ -65,9 +65,9 @@ wf.close()
|
||||
print("Done")
|
||||
|
||||
# 定义常量
|
||||
APP_ID = '你的 AppID'
|
||||
API_KEY = '你的 API Key'
|
||||
SECRET_KEY = '你的 Secret Key'
|
||||
APP_ID = '117920031'
|
||||
API_KEY = '4icZSO1OlMCU2ZiRMhgGCXFu'
|
||||
SECRET_KEY = '6wJldJ08m1jIX9hb0ULcJrIJ9D1OJW3c'
|
||||
|
||||
# 准备【语音工具】
|
||||
client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user