让chatGPT教我使用开源游戏引擎godot开发游戏--单词填空

in STEEM CN/中文3 days ago

这学期小宝贝学校正式开英语了,想用开源的游戏引擎godot开发一个背单词填空的APP。不过我对这个引擎的了解非常有限。只跟着视频教程做过一个简单的平台游戏。

于是产生在AI的指导下进行开发的想法。

下面是我的提示词:

# Role
你是godot游戏制作专家。
## profile
专业,思考细腻,周到

## task
### 任务目标
按用户要求,为使用godot游戏引擎制作游戏提供步骤指导和编写相应的GDscript代码。

### 游戏玩法描述
一个练习背单词的游戏,可以从文本文件中读取需要背的单词。然后随机抹掉单词中的若干个字母。然后提供四个包含混淆项的字母选项,让玩家从中做出正确的选择。补全这个单词。

### 任务要求
步骤描述里面要给出需要建立哪些节点,进行哪些设置。需要使用GDscript脚本在脚本项目下给出。

### 输出格式
/<xxxx/>为占位符
"""
### 步骤描述
<步骤描述>
### 所需脚本
<GDscript脚本>

## 任务初始化
按照 /<task/> 中用户所提游戏玩法描述,完成/<task/>.<任务目标>,并按<输出格式>输出

这是chatGPT给我的回复:

"""

步骤描述

  1. 创建 Godot 项目:

    • 打开 Godot,创建一个新项目并命名为 "VocabularyPracticeGame"。
    • 选择合适的项目路径并点击"创建"。
  2. 创建主场景:

    • res:// 目录下,创建一个新的场景,命名为 Main.tscn
    • 在场景中,添加以下节点:
      • 根节点(类型为 Control):作为 UI 场景的基础。
      • Label:用于显示当前被抹掉字母的单词。
      • GridContainer:作为字母选项的容器。设置 GridContainercolumns 属性为 2,以生成两行两列的按钮。
      • Button x 4:四个按钮用于展示选项,每个按钮作为子节点放入 GridContainer 中。
      • Button(额外按钮):用于跳过或重新生成新单词。
      • FileDialog:用于读取包含单词的文本文件。
  3. 设置控件属性:

    • Label 节点的 Text 属性留空,用于显示动态生成的单词。
    • GridContainer 下的四个按钮命名,例如 OptionButton1OptionButton2OptionButton3OptionButton4,方便在脚本中引用。
    • 为额外按钮设置文本 "Skip" 或 "Next"。
  4. 连接信号:

    • 将每个按钮的 pressed() 信号连接到主场景的脚本中,以处理玩家的选择。
    • FileDialogfile_selected() 信号连接到主脚本中,用于加载文本文件中的单词。
  5. 编写脚本逻辑:

    • Main.tscn 场景的根节点上附加一个脚本 Main.gd,用于处理单词抹掉、字母选项生成和用户输入处理的逻辑。
    • 在项目中创建一个单独的 words.txt 文件,包含若干行单词,供游戏读取。

所需脚本

extends Control

var words = []
var current_word = ""
var missing_indices = []
var correct_letter = ""
var options = []

# 当游戏启动时
func _ready():
    $FileDialog.popup_centered()  # 打开文件选择对话框

# 从文件中读取单词
func _on_FileDialog_file_selected(path):
    var file = File.new()
    if file.file_exists(path):
        file.open(path, File.READ)
        words.clear()
        while not file.eof_reached():
            var word = file.get_line().strip_edges()
            if word != "":
                words.append(word)
        file.close()
        generate_word()

# 生成随机单词并抹掉若干个字母
func generate_word():
    if words.size() > 0:
        current_word = words[randi() % words.size()]
        var num_missing = randi() % min(current_word.length(), 3) + 1  # 随机抹掉 1-3 个字母
        missing_indices.clear()
        
        for i in range(num_missing):
            var random_index = randi() % current_word.length()
            while random_index in missing_indices:
                random_index = randi() % current_word.length()
            missing_indices.append(random_index)
        
        update_displayed_word()
        generate_options()

# 更新显示的单词,抹掉某些字母
func update_displayed_word():
    var displayed_word = ""
    for i in range(current_word.length()):
        if i in missing_indices:
            displayed_word += "_"
        else:
            displayed_word += current_word[i]
    $Label.text = displayed_word

# 生成四个字母选项,其中一个是正确答案
func generate_options():
    correct_letter = current_word[missing_indices[0]]  # 取第一个缺失字母作为正确答案
    options = [correct_letter]
    
    # 生成干扰项
    while options.size() < 4:
        var random_letter = String.from_char_code(randi() % 26 + 97)  # 随机生成一个字母
        if random_letter not in options:
            options.append(random_letter)
    
    options.shuffle()  # 打乱顺序
    
    # 将选项分配给按钮
    $GridContainer/OptionButton1.text = options[0]
    $GridContainer/OptionButton2.text = options[1]
    $GridContainer/OptionButton3.text = options[2]
    $GridContainer/OptionButton4.text = options[3]

# 处理按钮点击事件
func _on_OptionButton_pressed(index):
    if options[index] == correct_letter:
        print("Correct!")
    else:
        print("Wrong, try again.")
    generate_word()

# 按钮信号绑定函数
func _on_OptionButton1_pressed():
    _on_OptionButton_pressed(0)

func _on_OptionButton2_pressed():
    _on_OptionButton_pressed(1)

func _on_OptionButton3_pressed():
    _on_OptionButton_pressed(2)

func _on_OptionButton4_pressed():
    _on_OptionButton_pressed(3)

# Skip 按钮点击事件
func _on_SkipButton_pressed():
    generate_word()

解释:

  1. 单词读取:从文本文件中读取单词列表。
  2. 单词生成:随机选择一个单词并抹掉其中的几个字母。
  3. 选项生成:生成四个选项,其中一个是正确答案,其他为干扰项。
  4. 信号处理:为每个按钮的点击事件编写处理逻辑,检查玩家选择是否正确。
    """

还是非常详细的。那么按照这个指引真的能做出我想要的app吗?,敬请关注。

Sort:  

Upvoted! Thank you for supporting witness @jswit.