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

106 lines
4.0 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 tkinter as tk
from PIL import Image, ImageTk
from tkinter import messagebox
# 预置内容,请勿改动
def check(month, day): # 判断星座的函数
if (month == 1 and day >= 20) or (month == 2 and day <= 18):
return "水瓶座♒"
elif (month == 2 and day >= 19) or (month == 3 and day <= 20):
return "双鱼座♓"
elif (month == 3 and day >= 21) or (month == 4 and day <= 19):
return "白羊座♈"
elif (month == 4 and day >= 20) or (month == 5 and day <= 20):
return "金牛座♉"
elif (month == 5 and day >= 21) or (month == 6 and day <= 21):
return "双子座♊"
elif (month == 6 and day >= 22) or (month == 7 and day <= 22):
return "巨蟹座♋"
elif (month == 7 and day >= 23) or (month == 8 and day <= 22):
return "狮子座♌"
elif (month == 8 and day >= 23) or (month == 9 and day <= 22):
return "处女座♍"
elif (month == 9 and day >= 23) or (month == 10 and day <= 23):
return "天秤座♎"
elif (month == 10 and day >= 24) or (month == 11 and day <= 22):
return "天蝎座♏"
elif (month == 11 and day >= 23) or (month == 12 and day <= 21):
return "射手座♐"
elif (month == 12 and day >= 22) or (month == 1 and day <= 19):
return "摩羯座♑"
# 创建Tkinter窗口
root = tk.Tk()
root.geometry('320x360')
root.title("测测你的星座吧")
# 创建一个标签用于显示gif动画
label_bg = tk.Label(root)
label_bg.place(x=0, y=0, relwidth=1, relheight=1) #将标签放在窗口顶部并充斥整个窗口
# 加载gif图片并将每一帧存储在frames列表中
image = Image.open('星图.gif') # 打开gif图片文件
frames = [] # 用于存储gif的所有帧
try:
while True:
frames.append(ImageTk.PhotoImage(image)) # 将每一帧添加到frames列表中
image.seek(len(frames)) # 跳转到gif的下一帧
except EOFError:
pass # 当到达gif结束时跳出循环
# 用于更新gif动画帧的函数
def upd(idx):
frame = frames[idx] # 获取当前帧
label_bg.configure(image=frame) # 更新标签中显示的帧
idx = (idx + 1) % len(frames) # 计算下一帧的索引
root.after(100, upd, idx) # 100毫秒后调用自己继续更新帧
upd(0) # 开始播放gif动画
# 创建标签和文本框
label_year = tk.Label(root, text="出生年:")
label_year.grid(row=0, column=0, padx=(60,10), pady=(140,10))
entry_year = tk.Entry(root)
entry_year.grid(row=0, column=1, pady=(140,10))
label_month = tk.Label(root, text="出生月:")
label_month.grid(row=1, column=0, padx=(60,10), pady=10)
entry_month = tk.Entry(root)
entry_month.grid(row=1, column=1, pady=10)
label_day = tk.Label(root, text="出生日:")
label_day.grid(row=2, column=0, padx=(60,10), pady=10)
entry_day = tk.Entry(root)
entry_day.grid(row=2, column=1, pady=10)
# 按钮点击处理函数
# 点击提交按钮时调用,它获取用户输入的出生年月日,验证输入有效性,并显示对应的星座信息
def click():
year = entry_year.get() # 获取用户输入的出生年份
month = entry_month.get() # 获取用户输入的出生月份
day = entry_day.get() # 获取用户输入的出生日
# 检查用户输入的年、月、日是否为有效的数字
if not year.isdigit() or not month.isdigit() or not day.isdigit():
messagebox.showerror("输入错误", "请输入有效的日期")
month = int(month) # 将月份转换为整数
day = int(day) # 将日期转换为整数
# 检查日期是否在有效范围内
if month < 1 or month > 12 or day < 1 or day > 31:
messagebox.showerror("输入错误", "请输入有效的日期")
result = check(month, day) # 调用check函数获取星座信息
messagebox.showinfo("星座", f"你的星座是:{result}") # 显示用户的星座信息
# 创建提交按钮
btn = tk.Button(root, text="提交", command=click)
btn.grid(row=3, columnspan=2, padx=(80,10), pady=10)
# 运行Tkinter主循环
root.mainloop()