猫史档案馆


【Python作品分享】6.0-上次成果-未加入NLU模型【作业帖】

用户:荔枝y6荔枝y6查看:0 回复:2 评论:0 创建时间:2024-01-29T20:29:53


【作品展示】

center_image

 

【作品介绍】

66

 

【作品源代码】

import pgzrun
import SparkApi
import pgzero
#------初始化设置区----------------
# 包括常量、角色、变量等
# 定义游戏窗口的大小
appid = "4fd9c88a"     #填写控制台中获取的 APPID 信息
api_secret = "MmY5NTk2MGFjNmE2ZmY5ZGViNGIxMGIx"   #填写控制台中获取的 APISecret 信息
api_key ="fb6909fe喵9bc4ddec0512c31c105020"    #填写控制台中获取的 APIKey 信息

#用于配置大模型版本,默认“general
domain = "general"   # v1.5版本
Spark_url = "ws://spark-api.xf-yun.com/v1.1/chat"  # v1.5环境的


WIDTH, HEIGHT = 600, 480
bg = Actor('高达.png', (WIDTH / 2, HEIGHT / 2))
player = Actor('的多.png', (400, 300))
cave = Actor('失败.png', (300, 100))
# 创建npc角色及状态属性
npc = Actor('斧头.png', (200, 200))
npc.states = ['idle', 'make_fire', 'chop', ]
npc.state = 'idle'
# 创建npc对话文本
npc.text_list = [
    '你好,我正在ababababab啊吧啊吧',
    '我正在唱跳rap,你要一起吗?',
    'X太酷啦X',
]
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('的.png', (80, 405))
# 创建烤火相关元素
fire = Actor('否.png', (50, 100))
fire.costumes = ['否.png', '的多的.png', ]
fire.cur_costume = 0
fire.image = fire.costumes[fire.cur_costume]
# 创建砍柴相关元素
wood = Actor('否.png', (500, 150))
wood.costumes = ['的多的.png', '否.png',]
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


# ----------------动作函数区--------------------
# 处理自定义的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 = ''


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)
def dia():
    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'
    pc.text = answer_fmt
def format_text(text,interval):
    result = ''
    for i in range(0,len(text),interval):
        result+=text[i:itintervall]+'\n'
    return result
# 切换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


回复

上一页1 页 / 共 1下一页
飞熊jSrt飞熊jSrt

沙发捏

点赞0


评论


飞熊jSrt飞熊jSrt

嗨嗨嗨,板凳

点赞0


评论