用panda3d写出对话系统,给出代码。
时间: 2023-03-24 18:01:48 浏览: 89
用Panda3D开发3D枪战类游戏源码,脚本可以直接运行
好的,以下是一个简单的 Panda3D 对话系统的示例代码:
```python
from direct.showbase.DirectObject import DirectObject
from direct.gui.OnscreenText import OnscreenText
from direct.gui.DirectGui import *
from panda3d.core import TextNode
class DialogSystem(DirectObject):
def __init__(self):
DirectObject.__init__(self)
# 初始化对话框
self.dialogBox = DirectFrame(
frameColor=(0, 0, 0, 0.8),
frameSize=(-1, 1, -0.2, 0.2),
pos=(0, 0, -0.9),
parent=base.aspect2d
)
self.dialogText = OnscreenText(
text="",
parent=self.dialogBox,
pos=(0, 0.07),
scale=0.07,
fg=(1, 1, 1, 1),
align=TextNode.ACenter,
mayChange=True
)
self.dialogOptions = []
for i in range(3):
option = DirectButton(
text="",
text_scale=0.05,
text_fg=(1, 1, 1, 1),
frameColor=(0.5, 0.5, 0.5, 1),
pos=(-0.4, 0, -0.1 - 0.15 * i),
parent=self.dialogBox,
relief=DGG.FLAT,
command=self.onOptionSelected,
extraArgs=[i],
scale=1.25
)
self.dialogOptions.append(option)
# 测试对话框
self.currentDialog = [
{
"speaker": "NPC1",
"text": "你好,我是 NPC1。",
"options": [
{"text": "你好,NPC1。", "next": 1},
{"text": "我不想和你交谈。", "next": -1}
]
},
{
"speaker": "NPC1",
"text": "你想问我什么吗?",
"options": [
{"text": "你是谁?", "next": -1},
{"text": "你有什么事吗?", "next": 2}
]
},
{
"speaker": "NPC1",
"text": "我需要你帮我找到一些东西。",
"options": [
{"text": "好的,我会帮助你。", "next": -1},
{"text": "不,我没兴趣。", "next": -1}
]
}
]
# 开始对话
self.startDialog(self.currentDialog[0])
def startDialog(self, dialog):
# 显示对话框
self.dialogBox.show()
self.dialogText.setText(dialog["speaker"] + ": " + dialog["text"])
for i, option in enumerate(dialog["options"]):
self.dialogOptions[i]["text"] = option["text"]
for option in self.dialogOptions[len(dialog["options"]):]:
option.hide()
def endDialog(self):
# 隐藏对话框
self.dialogBox.hide()
def onOptionSelected(self, optionIndex):
# 处理选择的选项
selectedOption = self.currentDialog[0]["options"][optionIndex]
if selectedOption["next"]
阅读全文