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

46 lines
1.5 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
import random
from PIL import Image, ImageTk
# 弹幕类
class MovingLabel:
def __init__(self, window, text):
self.text = text
self.label = tk.Label(window, image=kuang, text=self.text, compound="center", font=("黑体", 20), fg='white',width=190, height=45)
self.label.place(x=800, y=random.randint(50, 400))
self.x = 800 # 初始x坐标800
self.move()
def move(self):
if self.x > -200:
self.x -= 2
self.label.place(x=self.x) # 更新标签的x坐标实现向左移动
# 每隔20毫秒调用一次move方法实现连续移动效果
self.label.after(20, self.move)
else:
self.label.destroy() # 销毁移出窗口的标签
# 发送弹幕
def send():
text = e1.get() # 获取弹幕文本
ml = MovingLabel(window, text)
# 创建窗口
window = tk.Tk()
window.geometry('1000x670')
window.resizable(0, 0)
kuang = ImageTk.PhotoImage(file='kuang.png') # 创建可以在Tkinter中使用的图像对象
bg_image = Image.open("tv.png") # 导入背景图片
bg_image = ImageTk.PhotoImage(bg_image) # 转换为PhotoImage对象才可以在tkinter中显示图形
bg_label = tk.Label(window, image=bg_image) # 创建标签显示背景图
bg_label.pack() # 放置标签
# 弹幕输入框
e1 = tk.Entry(window, font=("黑体", 20))
e1.place(x=280, y=620)
# 发送按钮
b1 = tk.Button(window, text="发送弹幕", font=("黑体", 20), command=send)
b1.place(x=580, y=613)
window.mainloop()