用户:顾灏辰查看:0 回复:3 评论:0 创建时间:2024-03-08T21:08:18
【作品展示】

【作品介绍】
上下左右键移动空格键出菜单栏
回车选择
新增改喵和打靶
游戏亮点:
调用星火大模型
【作品源代码】
import pgzrun
import SparkApi
# ---------------初始化设置区----------------
# 包括常量、角色、变量等
# 定义游戏窗口的大小
WIDTH, HEIGHT = 600, 560
bg = Actor('my_bg.png', (WIDTH / 2, HEIGHT / 2))
player = Actor('player.jpg', (400, 300))
cave = Actor('图标.jpg', (300, 100))
# 创建npc角色及状态属性
npc = Actor('npc.jpg', (200, 200))
npc.states = ['idle', 'make_fire', 'chop', ]
npc.state = 'idle'
# 创建npc对话文本
npc.text_list = [
' 你好,我是乔尔·加里森'+'\n'+' 以后我就是你的教官了。',
' 看来你这把喵得改改了...',
' 来吧,试试你的新喵怎么样吧!',
]
npc.text = npc.text_list[0]
# 创建菜单栏相关元素
menu_rect = Rect((150, 100), (300, 250)) # 菜单位置和大小
menu_item1_rect = Rect((喵)) # 选项1位置和大小
menu_item2_rect = Rect((120, 190), (360, 40)) # 选项2位置和大小
menu_item3_rect = Rect((120, 240), (360, 40)) # 选项3位置和大小
focus = '' # 焦点
# 创建对话栏相关元素
chat_box = Actor('对话框.png', (300, 405))
npc_foc = Actor('npc_foc.jpg', (80, 405))
# 创建烤火相关元素
fire = Actor('改喵前.jpg', (50, 100))
fire.costumes = ['改喵前.jpg', '改喵后.png', ]
fire.cur_costume = 0
fire.image = fire.costumes[fire.cur_costume]
# 创建砍柴相关元素
wood = Actor('靶场_未射击.jpg', (500, 150))
wood.costumes = ['靶场_未射击.jpg', '靶场_射击时.jpg',]
wood.cur_costume = 0
wood.image = wood.costumes[wood.cur_costume]
# 碰撞检测相关元素
blocks = [npc, cave, fire, wood, ]
# 创建气泡相关元素
balloon = Actor('idle0.png', (300, 300))
balloon.costumes = [
'idle0.png', 'idle1.png', 'idle2.png', 'idle3.png', 'idle4.png', 'idle5.png', 'idle6.png', 'idle7.png',
'chop0.png', 'chop1.png', 'chop2.png', 'chop3.png', 'chop4.png', 'chop5.png', 'chop6.png', 'chop7.png',
]
balloon.cur_costume = 0
balloon.i = 0
appid = "a4033c1c" #填写控制台中获取的 APPID 信息
api_secret = "MzIzZmVmNjVmMjAxYTRlOWJlMzM5MDZj" #填写控制台中获取的 APISecret 信息
api_key ="45dca9e002ad1a98b92988eeb97aacb7" #填写控制台中获取的 APIKey 信息
#用于配置大模型版本,默认“general
domain = "general" # v1.5版本
Spark_url = "ws://spark-api.xf-yun.com/v1.1/chat" # v1.5环境的地址
role_setting='扮演一位新兵教官,回复字数不超过50'
npc.turns=0
# ----------------动作函数区--------------------
# 处理自定义的NPC动作
def chat():
if npc.state == 'idle':
npc.text = npc.text_list[0]
elif npc.state == 'make_fire':
npc.text = npc.text_list[1]
elif npc.state == 'chop':
npc.text = npc.text_list[2]
def make_fire():
interrupt_npc()
npc.state = 'make_fire'
npc.pos = 100, 100
fire.cur_costume = 1
def chop():
interrupt_npc()
npc.state = 'chop'
npc.pos = 400, 180
wood.cur_costume = 1
def interrupt_npc():
# 终止npc状态
if npc.state == 'make_fire':
fire.cur_costume = 0
elif npc.state == 'chop':
wood.cur_costume = 0
# ----------------事件函数区--------------------
# 处理键盘、鼠标等事件
def on_key_down(key):
global focus
if keyboard.space and player.distance_to(npc) < 100:
if focus == '':
focus = 'menu'
elif focus == 'menu':
focus = ''
elif keyboard.RETURN and player.distance_to(npc) < 100:
if focus == '':
dialog()
def dialog():
global focus
Input = input("\n" +"我:")
if npc.turns % 5==0:
Input = role_setting + Input
npc.turns += 1
answer = SparkApi.main(appid, api_key, api_secret, Spark_url, domain, Input)
answer_fmt=format_text(answer,17)
focus='chat'
npc.text=answer_fmt
print(answer)
def format_text(text,interval):
result=''
for i in range(0,len(text),interval):
result+=text[i:i+interval]+'\n'
return result
def on_mouse_down(pos):
global focus
if focus == 'menu':
if menu_item1_rect.collidepoint(pos):
focus = 'chat'
chat()
elif menu_item2_rect.collidepoint(pos):
focus = ''
make_fire()
elif menu_item3_rect.collidepoint(pos):
focus = ''
chop()
elif focus == 'chat':
if chat_box.collidepoint(pos):
focus = ''
# -----------------主循环区--------------------
# 处理游戏运行过程中绘制和更新等
def display_menu():
# 绘制一个矩形作为菜单背景
screen.draw.filled_rect(menu_rect, 'blue') # 菜单栏可替换成图片
screen.draw.text('闲聊', midbottom=menu_item1_rect.midbottom, fontname='simhei.ttf', color='white')
screen.draw.text('改喵', midbottom=menu_item2_rect.midbottom, fontname='simhei.ttf', color='white')
screen.draw.text('射击', midbottom=menu_item3_rect.midbottom, fontname='simhei.ttf', color='white')
def display_chat():
# 显示对话栏相关元素
chat_box.draw()
npc_foc.draw()
screen.draw.text(npc.text, (180, 350), fontname='simhei.ttf', color='white')
def draw():
screen.clear()
bg.draw()
cave.draw()
player.draw()
npc.draw()
fire.image = fire.costumes[fire.cur_costume]
fire.draw()
wood.image = wood.costumes[wood.cur_costume]
wood.draw()
balloon.image = balloon.costumes[balloon.cur_costume]
balloon.draw()
if focus == 'menu':
display_menu()
elif focus == 'chat':
display_chat()
def update():
# 角色的移动代码
prev_player_pos = player.pos
if keyboard.left:
player.x -= 4
if keyboard.right:
player.x += 4
if keyboard.up:
player.y -= 4
if keyboard.down:
player.y += 4
# 碰撞检测
for block in blocks:
if player.colliderect(block):
player.pos = prev_player_pos
# balloon 跟随 npc 右上方,指示npc的状态
balloon.pos = (npc.x + 20, npc.y - 20)
# 切换balloon的造型
def switch_bal_costume():
if npc.state in ['idle', 'make_fire']:
base_index = 0
length = 8
elif npc.state == 'chop':
base_index = 8
length = 8
balloon.cur_costume = balloon.i % length + base_index # 循环切换造型
balloon.i += 1
# 将switch_image函数每0.5秒调用一次,从而切换一次角色造型
clock.schedule_interval(switch_bal_costume, 0.5)
pgzrun.go()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn