找回密码
 中文实名注册
搜索
查看: 1234|回复: 52

崔梦辰的作品

[复制链接]

2

主题

38

回帖

383

积分

中级会员

积分
383
发表于 2026-5-4 16:27:09 | 显示全部楼层 |阅读模式
本帖最后由 崔梦辰 于 2026-5-16 09:41 编辑

崔梦辰的作品
回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 4 小时前 | 显示全部楼层
033.生日提醒器

import pygameimport datetimeimport jsonimport osimport mathimport randompygame.init()WIDTH, HEIGHT = 750, 600screen = pygame.display.set_mode((WIDTH, HEIGHT))pygame.display.set_caption("🎂 生日提醒器【美化科技版】")clock = pygame.time.Clock()FPS = 60# ========== 配色 ==========BG_COLOR = (10, 14, 24)TEXT_NORMAL = (210, 225, 250)COLOR_TITLE = (0, 220, 255)COLOR_TODAY = (255, 210, 80)COLOR_RED = (255, 60, 90)COLOR_BLUE = (0, 170, 255)GRAY_DARK = (30, 36, 52)GRAY_LINE = (60, 80, 110)# ========== 字体 ==========try:    font_title = pygame.font.SysFont("simhei", 40)    font_head = pygame.font.SysFont("simhei", 28)    font_text = pygame.font.SysFont("simhei", 23)    font_small = pygame.font.SysFont("simhei", 20)    font_popup = pygame.font.SysFont("simhei", 52)except:    font_title = pygame.font.SysFont("arial", 40)    font_head = pygame.font.SysFont("arial", 28)    font_text = pygame.font.SysFont("arial", 23)    font_small = pygame.font.SysFont("arial", 20)    font_popup = pygame.font.SysFont("arial", 52)# ========== 音效加载 ==========birthday_sound = Nonesound_path = "sound/birthday.wav"if os.path.exists(sound_path):    try:        birthday_sound = pygame.mixer.Sound(sound_path)    except:        birthday_sound = None# ========== 工具函数:发光文字 ==========def draw_glow_text(surface, font, text, color, x, y, glow=3):    surf = font.render(text, True, color)    for ox in [-glow, -1, 1, glow]:        for oy in [-glow, -1, 1, glow]:            surface.blit(surf, (x + ox, y + oy))    surface.blit(surf, (x, y))# ========== 数据 ==========DATA_FILE = "birthday.json"birthday_list = []def load_data():    global birthday_list    if os.path.exists(DATA_FILE):        try:            with open(DATA_FILE, "r", encoding="utf-8") as f:                birthday_list = json.load(f)        except:            birthday_list = []def save_data():    with open(DATA_FILE, "w", encoding="utf-8") as f:        json.dump(birthday_list, f, ensure_ascii=False, indent=2)def get_days_left(month, day):    today = datetime.date.today()    try:        next_birth = datetime.date(today.year, month, day)    except ValueError:        return 999    if next_birth < today:        next_birth = datetime.date(today.year + 1, month, day)    return (next_birth - today).daysload_data()# ========== 界面控件 ==========input_name = ""input_month = ""input_day = ""active_input = Nonebtn_add_rect = pygame.Rect(560, 510, 140, 50)input_name_rect = pygame.Rect(30, 510, 180, 42)input_m_rect = pygame.Rect(230, 510, 80, 42)input_d_rect = pygame.Rect(330, 510, 80, 42)# 背景粒子particles = []for _ in range(50):    particles.append({        "x": random.randint(0, WIDTH),        "y": random.randint(0, HEIGHT),        "size": random.uniform(1, 2.5),        "speed": random.uniform(0.15, 0.6),        "alpha": random.randint(40, 130)    })# ========== 生日弹窗变量 ==========show_popup = Falseconfetti_list = []popup_played_sound = Falsetoday_names = []# 生成彩带粒子def spawn_confetti():    colors = [(255,80,80),(255,220,60),(80,255,160),(80,180,255),(230,100,230)]    for _ in range(120):        confetti_list.append({            "x": random.randint(0, WIDTH),            "y": random.randint(-60, -10),            "vx": random.uniform(-2.5, 2.5),            "vy": random.uniform(1, 3.5),            "color": random.choice(colors),            "size": random.uniform(3,7),            "rotate": random.uniform(0,360)        })running = Trueframe_count = 0while running:    screen.fill(BG_COLOR)    frame_count += 1    mouse_pos = pygame.mouse.get_pos()    # ========== 事件 ==========    for event in pygame.event.get():        if event.type == pygame.QUIT:            running = False        if event.type == pygame.MOUSEBUTTONDOWN:            # 如果弹窗打开,点击任意位置关闭弹窗            if show_popup:                show_popup = False                confetti_list.clear()                popup_played_sound = False            else:                active_input = None                if input_name_rect.collidepoint(mouse_pos):                    active_input = "name"                elif input_m_rect.collidepoint(mouse_pos):                    active_input = "month"                elif input_d_rect.collidepoint(mouse_pos):                    active_input = "day"                elif btn_add_rect.collidepoint(mouse_pos):                    try:                        m = int(input_month)                        d = int(input_day)                        if 1 <= m <= 12 and 1 <= d <= 31 and input_name.strip():                            birthday_list.append({"name": input_name.strip(), "month": m, "day": d})                            save_data()                            input_name = input_month = input_day = ""                    except:                        pass        if event.type == pygame.KEYDOWN and active_input and not show_popup:            if event.key == pygame.K_BACKSPACE:                if active_input == "name":                    input_name = input_name[:-1]                elif active_input == "month":                    input_month = input_month[:-1]                elif active_input == "day":                    input_day = input_day[:-1]            else:                char = event.unicode                if active_input == "name":                    input_name += char                else:                    if char in "0123456789":                        if active_input == "month":                            input_month += char                        else:                            input_day += char    # ========== 背景特效 ==========    grid_alpha = 28    grid_color = (*GRAY_LINE, grid_alpha)    grid_size = 50    for x in range(0, WIDTH, grid_size):        pygame.draw.line(screen, grid_color, (x, 0), (x, HEIGHT))    for y in range(0, HEIGHT, grid_size):        pygame.draw.line(screen, grid_color, (0, y), (WIDTH, y))    scan_y = (frame_count * 1.6) % HEIGHT    pygame.draw.line(screen, (*COLOR_BLUE, 50), (0, scan_y), (WIDTH, scan_y), 1)    for p in particles:        p["y"] += p["speed"]        if p["y"] > HEIGHT:            p["y"] = 0            p["x"] = random.randint(0, WIDTH)        surf = pygame.Surface((int(p["size"]*2), int(p["size"]*2)), pygame.SRCALPHA)        pygame.draw.circle(surf, (*COLOR_BLUE, p["alpha"]), (p["size"], p["size"]), p["size"])        screen.blit(surf, (p["x"], p["y"]))    # ========== 整理生日数据 ==========    today_list = []    soon_list = []    today_names.clear()    for item in birthday_list:        days = get_days_left(item["month"], item["day"])        info = f"{item['name']} | {item['month']}月{item['day']}日 | 剩余{days}天"        if days == 0:            today_list.append(info)            today_names.append(item["name"])        else:            soon_list.append((days, info))    soon_list.sort(key=lambda x: x[0])    # 检测:今天有人过生日,自动弹出庆祝窗口    if len(today_list) > 0 and not show_popup:        show_popup = True        spawn_confetti()    # ========== 绘制主界面文字 ==========    draw_glow_text(screen, font_title, "🎂 生日提醒器", COLOR_TITLE, 30, 25)    y = 90    if today_list:        draw_glow_text(screen, font_head, "✨ 今日寿星", COLOR_RED, 30, y)        y += 40        breath = int(160 + 95 * math.sin(frame_count * 0.05))        live_color = (255, breath, 80)        for text in today_list:            draw_glow_text(screen, font_text, text, live_color, 50, y)            y += 34        y += 12    draw_glow_text(screen, font_head, "📅 近期生日列表", TEXT_NORMAL, 30, y)    y += 40    for _, txt in soon_list:        draw_glow_text(screen, font_small, txt, TEXT_NORMAL, 50, y)        y += 28    # ========== 输入框绘制 ==========    def draw_input_box(rect, hint, text, active):        color = COLOR_BLUE if active else GRAY_LINE        pygame.draw.rect(screen, GRAY_DARK, rect, border_radius=8)        pygame.draw.rect(screen, color, rect, 2, border_radius=8)        show_text = text if text else hint        ts = font_small.render(show_text, True, TEXT_NORMAL)        screen.blit(ts, (rect.x + 10, rect.y + 8))    draw_input_box(input_name_rect, "姓名", input_name, active_input=="name")    draw_input_box(input_m_rect, "月", input_month, active_input=="month")    draw_input_box(input_d_rect, "日", input_day, active_input=="day")    # 添加按钮    btn_hover = btn_add_rect.collidepoint(mouse_pos)    radius = 8    if btn_hover:        pygame.draw.rect(screen, (0, 90, 150), btn_add_rect, border_radius=radius)        pygame.draw.rect(screen, COLOR_BLUE, btn_add_rect, 3, border_radius=radius)    else:      

main.py

10.64 KB, 下载次数: 0

售价: 2 金钱  [记录]  [购买]

点评

8.8打字55积分25  发表于 4 小时前
回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 4 小时前 | 显示全部楼层
032.画图板(简易)

import pygamepygame.init()# 窗口设置WIDTH, HEIGHT = 800, 600screen = pygame.display.set_mode((WIDTH, HEIGHT))pygame.display.set_caption("简易Pygame画图板")# 颜色WHITE = (255, 255, 255)BLACK = (0, 0, 0)RED = (255, 0, 0)GREEN = (0, 220, 0)BLUE = (0, 80, 255)# 画布(底层画面)canvas = pygame.Surface((WIDTH, HEIGHT - 80))canvas.fill(WHITE)# 字体font = pygame.font.SysFont("simhei", 24)# 变量drawing = Falselast_pos = Nonecurrent_color = BLACKbrush_size = 5# 按钮区域btn_clear = pygame.Rect(10, HEIGHT - 70, 100, 60)btn_black = pygame.Rect(130, HEIGHT - 70, 50, 60)btn_red = pygame.Rect(190, HEIGHT - 70, 50, 60)btn_green = pygame.Rect(250, HEIGHT - 70, 50, 60)btn_blue = pygame.Rect(310, HEIGHT - 70, 50, 60)# 粗细加减按钮btn_plus = pygame.Rect(400, HEIGHT - 70, 50, 60)btn_minus = pygame.Rect(460, HEIGHT - 70, 50, 60)running = Trueclock = pygame.time.Clock()while running:    screen.fill((40, 40, 40))    # 贴画布    screen.blit(canvas, (0, 0))    # 事件循环    for event in pygame.event.get():        if event.type == pygame.QUIT:            running = False        # 鼠标按下        if event.type == pygame.MOUSEBUTTONDOWN:            pos = event.pos            if event.button == 1:                # 判断按钮点击                if btn_clear.collidepoint(pos):                    canvas.fill(WHITE)                elif btn_black.collidepoint(pos):                    current_color = BLACK                elif btn_red.collidepoint(pos):                    current_color = RED                elif btn_green.collidepoint(pos):                    current_color = GREEN                elif btn_blue.collidepoint(pos):                    current_color = BLUE                elif btn_plus.collidepoint(pos):                    brush_size += 2                elif btn_minus.collidepoint(pos):                    if brush_size > 1:                        brush_size -= 2                else:                    # 在画布区域开始画画                    drawing = True                    last_pos = pos        # 鼠标松开        if event.type == pygame.MOUSEBUTTONUP:            drawing = False            last_pos = None        # 鼠标拖动        if event.type == pygame.MOUSEMOTION:            if drawing and last_pos:                # 在画布上画线                pygame.draw.line(canvas, current_color, last_pos, event.pos, brush_size)                last_pos = event.pos    # ========== 绘制底部控制栏按钮 ==========    # 清空按钮    pygame.draw.rect(screen, (80, 80, 80), btn_clear)    text_clear = font.render("清空", True, WHITE)    screen.blit(text_clear, text_clear.get_rect(center=btn_clear.center))    # 颜色方块    pygame.draw.rect(screen, BLACK, btn_black)    pygame.draw.rect(screen, RED, btn_red)    pygame.draw.rect(screen, GREEN, btn_green)    pygame.draw.rect(screen, BLUE, btn_blue)    # 粗细按钮    pygame.draw.rect(screen, (60,60,60), btn_plus)    pygame.draw.rect(screen, (60,60,60), btn_minus)    text_plus = font.render("+", True, WHITE)    text_minus = font.render("-", True, WHITE)    screen.blit(text_plus, text_plus.get_rect(center=btn_plus.center))    screen.blit(text_minus, text_minus.get_rect(center=btn_minus.center))    # 显示当前画笔大小    size_text = font.render(f"粗细:{brush_size}", True, WHITE)    screen.blit(size_text, (530, HEIGHT - 50))    pygame.display.flip()    clock.tick(60)pygame.quit()

main.py

3.46 KB, 下载次数: 0

回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 5 小时前 | 显示全部楼层
031.抽签/随机点名器

main.py

6.76 KB, 下载次数: 0

售价: 1 金钱  [记录]  [购买]

回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 前天 09:50 | 显示全部楼层
030.转盘抽奖

main.py

4.43 KB, 下载次数: 22

点评

8.6打字47积分20  发表于 前天 09:52
回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 前天 09:47 | 显示全部楼层
另外

main.py

5.63 KB, 下载次数: 23

回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 前天 09:35 | 显示全部楼层
029.重量单位转换器

main.py

5.09 KB, 下载次数: 0

售价: 1 金钱  [记录]  [购买]

回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 前天 09:29 | 显示全部楼层
028.长度单位转换器

main.py

1.83 KB, 下载次数: 0

售价: 2 金钱  [记录]  [购买]

回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 4 天前 | 显示全部楼层
027.温度单位转换器

main.py

1.62 KB, 下载次数: 27

回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 4 天前 | 显示全部楼层
026.货币单位转换器

main.py

7.49 KB, 下载次数: 0

售价: 2 金钱  [记录]  [购买]

回复

使用道具 举报

2

主题

38

回帖

383

积分

中级会员

积分
383
 楼主| 发表于 4 天前 | 显示全部楼层
025.是男人上一百层

main.py

6.16 KB, 下载次数: 20

点评

8.4打字49积分20  发表于 4 天前
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

快速回复 返回顶部 返回列表