magic_pet_plus/main.py

223 lines
7.2 KiB
Python
Raw Normal View History

import tkinter as tk
from tkinter import messagebox, scrolledtext
from PIL import Image, ImageTk
import os
import random
import pygame
import requests
import threading
# DeepSeek API配置
DEEPSEEK_API_KEY = "sk-ef8d5c251461489e86e9b9f6bf136900" # 替换为您的实际API密钥
DEEPSEEK_API_URL = "https://api.deepseek.com/v1/chat/completions"
try:
pygame.mixer.init()
print("pygame音频初始化成功")
except Exception as e:
print("pygame音频初始化失败:", e)
# 宠物信息(名字 + 描述 + 图像路径 + 声音路径 + 性格)
pets = {
"fire": {
"name": "小火星",
"desc": "热情似火的小精灵,像火焰一样跳跃,能照亮黑暗的角落!",
"personality": "热情活泼,充满能量,乐于助人",
"image": "./images/fire_pet.png",
"sound": "./sounds/fire.mp3"
},
"water": {
"name": "小水灵",
"desc": "爱在水中嬉戏的小可爱,总能带来清凉和欢乐的气息!",
"personality": "温柔亲切,聪明伶俐,喜欢玩耍",
"image": "./images/water_pet.png",
"sound": "./sounds/water.mp3"
},
"thunder": {
"name": "小电闪",
"desc": "全身带电的小家伙,跑得飞快,一不小心就“嗞啦”一下!",
"personality": "调皮捣蛋,反应灵敏,喜欢恶作剧",
"image": "./images/thunder_pet.png",
"sound": "./sounds/thunder.mp3"
},
"wind": {
"name": "小风灵",
"desc": "像风一样自由自在,轻盈地守护着大家,还会吹走烦恼哦!",
"personality": "自由洒脱,爱冒险,安静细腻",
"image": "./images/wind_pet.png",
"sound": "./sounds/wind.mp3"
},
"earth": {
"name": "小土地",
"desc": "稳重又强壮的大地守护者,是伙伴们最坚实的后盾!",
"personality": "可靠沉稳,忠诚守护,有安全感",
"image": "./images/earth_pet.png",
"sound": "./sounds/earth.mp3"
},
"dark": {
"name": "小黑星",
"desc": "神秘的黑夜精灵,能在安静中保护大家不受惊吓!",
"personality": "神秘低调,冷静敏锐,喜欢黑夜",
"image": "./images/dark_pet.png",
"sound": "./sounds/dark.mp3"
},
"light": {
"name": "小光星",
"desc": "闪闪发光的光明使者,总能把温暖和希望带给大家!",
"personality": "阳光开朗,善解人意,充满正能量",
"image": "./images/light_pet.png",
"sound": "./sounds/light.mp3"
},
}
# 创建主窗口
root = tk.Tk()
root.title("魔法宠物伙伴")
root.geometry("600x700")
root.config(bg="lightyellow")
# 标题标签
title_label = tk.Label(root, text="🎉 你的魔法宠物伙伴", font=("Arial", 16, "bold"), bg="lightyellow")
title_label.pack(pady=10)
# 图像显示标签
image_label = tk.Label(root, bg="lightyellow")
image_label.pack(pady=10)
# 宠物介绍标签
info_label = tk.Label(root, text="", font=("Arial", 12), wraplength=300, justify="center", bg="lightyellow")
info_label.pack(pady=10)
# 对话历史显示区域
chat_frame = tk.Frame(root, bg="lightyellow")
chat_frame.pack(pady=10, fill=tk.BOTH, expand=True)
chat_history = scrolledtext.ScrolledText(chat_frame, width=60, height=10, font=("Arial", 10), wrap=tk.WORD)
chat_history.pack(fill=tk.BOTH, expand=True)
chat_history.insert(tk.END, "系统: 欢迎来到魔法宠物世界!点击按钮召唤你的宠物伙伴吧!\n")
chat_history.config(state=tk.DISABLED)
# 用户输入框
input_frame = tk.Frame(root, bg="lightyellow")
input_frame.pack(pady=5)
user_input = tk.Entry(input_frame, width=50, font=("Arial", 12))
user_input.pack(side=tk.LEFT, padx=5)
# 当前宠物和图像引用
current_photo = None
current_pet = None
def chat_with_pet(message):
if not current_pet:
return "请先召唤一只宠物伙伴吧!点击'召唤宠物'按钮~"
pet_info = pets[current_pet['type']]
prompt = f"""
你是一只名叫{pet_info['name']}的魔法宠物你的性格特点是{pet_info['personality']}
你现在正在和主人对话请用可爱活泼的语气回应可以适当使用表情符号和语气词~
当前对话内容{message}
"""
headers = {
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
"Content-Type": "application/json"
}
data = {
"model": "deepseek-chat",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.8,
"max_tokens": 150
}
try:
response = requests.post(DEEPSEEK_API_URL, headers=headers, json=data)
if response.status_code == 200:
return response.json()["choices"][0]["message"]["content"]
return f"{pet_info['name']}好像有点困惑... (API错误: {response.status_code})"
except Exception as e:
return f"哎呀,{pet_info['name']}暂时无法回应... ({str(e)})"
def send_message():
message = user_input.get()
if not message:
return
chat_history.config(state=tk.NORMAL)
chat_history.insert(tk.END, f"\n你: {message}\n")
chat_history.config(state=tk.DISABLED)
chat_history.see(tk.END)
user_input.delete(0, tk.END)
def run_chat():
response = chat_with_pet(message)
chat_history.config(state=tk.NORMAL)
pet_name = pets[current_pet['type']]['name'] if current_pet else "系统"
chat_history.insert(tk.END, f"{pet_name}: {response}\n")
chat_history.config(state=tk.DISABLED)
chat_history.see(tk.END)
threading.Thread(target=run_chat).start()
user_input.bind("<Return>", lambda event: send_message())
def generate_pet():
global current_photo, current_pet
# 随机选择宠物类型
pet_type = random.choice(list(pets.keys()))
pet = pets[pet_type]
current_pet = {"name": pet["name"], "type": pet_type}
# 更新UI显示
info_label.config(text=f"名字:{pet['name']}\n\n介绍:{pet['desc']}")
image_label.config(image='') # 清除旧图片
# 显示宠物图像
if os.path.exists(pet['image']):
img = Image.open(pet['image']).resize((200, 200))
current_photo = ImageTk.PhotoImage(img)
image_label.config(image=current_photo)
# 播放宠物声音
if os.path.exists(pet['sound']):
pygame.mixer.music.stop()
pygame.mixer.music.load(pet['sound'])
pygame.mixer.music.play()
# 在聊天框显示欢迎语
chat_history.config(state=tk.NORMAL)
chat_history.insert(tk.END, f"\n{pet['name']}出现了!\n")
chat_history.insert(tk.END, f"{pet['name']}: {random.choice(['你好呀主人!我是你的新伙伴~','哇!见到你好开心!','准备好和我一起冒险了吗?'])}\n")
chat_history.config(state=tk.DISABLED)
chat_history.see(tk.END)
# 按钮
button_frame = tk.Frame(root, bg="lightyellow")
button_frame.pack(pady=10)
generate_button = tk.Button(
button_frame,
text="✨ 召唤宠物",
font=("Arial", 14),
command=generate_pet,
bg="skyblue",
fg="white"
)
generate_button.pack(side=tk.LEFT, padx=10)
send_button = tk.Button(
button_frame,
text="💌 发送",
font=("Arial", 14),
command=send_message,
bg="pink",
fg="white"
)
send_button.pack(side=tk.LEFT, padx=10)
# 启动主循环
root.mainloop()