在Godot中追加数组

本文概述

  • 一维数组
  • 二维数组或矩阵
  • 多维或三维数组
  • 字符串的字符数组
数组是一个或多个相同类型值的集合。每个值称为数组的元素。数组的组件共享相同的变量名, 但是每个部分都有其唯一的索引号(也称为下标)。
数组可以是任何类型, 例如int, float, char等。
永远记住, 数组总是从其索引值开始, 并且数组的索引从0到n-1开始。
假设我们要访问数组的第5个元素, 那么我们将帮助我们第4个元素, 因为数组从0开始, 并且数组始终存储在连续的内存位置中。数组元素的下标标识的元素数和数组类型。 c提供的各种类型的Array如下:
  1. 一维数组
  2. 二维阵列
  3. 三维阵列
  4. 字符数组或字符串
一维数组 使用维表示数组的元素
例如
整数a [5]
[]用于数组的维数或下标, 通常用于声明数组的元素。
为了从集合中访问元素, 我们可以像这样使用数组的下标
a [3] = 100
这将设置数组的第4个元素的值
所以只有一个方括号, 然后称为单维数组
这也称为一维数组
二维数组或矩阵 二维数组用于以行和列的形式表示数组的元素, 它们用于表示矩阵。二维数组使用两个下标来声明数组的元素, 例如int一个[3] [3]
多维或三维数组 多维数组用于表示矩阵表的总数。当我们要制作矩阵元素的两个或多个表来声明数组元素时, 可以使用三维数组, 我们可以使用这种方式
诠释为[3] [3] [3]
前3个代表表的总数, 后3个代表每个表的总行数, 第3个3代表表的总列数。
因此, 这使得具有三行三列的3个表成为数组的主要要素, 因为元素始终存储在计算机内存中。
字符串的字符数组 作为整数, 字符也位于数组中。字符数组称为字符串。它们通常用于表示字符串, 请始终记住字符串以\ o或null字符终止。
有内置的字符串操作;字符串中的C语言提供了that.h头文件, 例如
  • strLen:用于获取字符串的长度或字符总数
  • strconcat:这用于连接两个字符串, 或者此函数用于连接两个字符串。
  • strRev:此函数用于获取字符串的反向
  • strcmp:此函数用于比较两个字符串, 比较两个字符串后, 结果如下。
打开Godot Engine, 然后单击脚本。
代码如下:
extends Controlvar player_words=[] #arrayvar story= "There are sometrees of %s and %s in the %s garden. And a %s morning" var prompts = ["MANGO", "Papaya", "Glorious", "pleasant"] #Using PlayerText, DisplayText, And VBox and HBoxContainer.onready var PlayerText= $VBoxContainer/HBoxContainer/PlayerTextonready var DisplayText= $VBoxContainer/DisplayText#func _ready(): #DisplayText.text= story%promptsfunc _on_PlayerText_text_entered(new_text): update_DisplayText(new_text) story=""#Using Button func _on_TextureButton_pressed(): var words= PlayerText.text update_DisplayText(words)func update_DisplayText(words): $VBoxContainer/DisplayText.text= words $VBoxContainer/HBoxContainer/PlayerText.clear()

屏幕截图:
在Godot中追加数组

文章图片
在Godot中追加数组

文章图片
更新了编码部分:
extends Controlvar player_words=[]var story= "There are many trees of %s and %s in the %s amazing garden. And a %s morning" var prompts = ["MANGO", "Papaya", "Glorious", "pleasant"]onready var PlayerText= $VBoxContainer/HBoxContainer/PlayerTextonready var DisplayText= $VBoxContainer/DisplayTextfunc _ready(): DisplayText.text= story%promptsfunc _on_PlayerText_text_entered(new_text): update_DisplayText(new_text)story = ""func _on_TextureButton_pressed(): var words = PlayerText.text update_DisplayText(words)func update_DisplayText(words): $VBoxContainer/DisplayText.text=words $VBoxContainer/HBoxContainer/PlayerText.clear()func add_to_player_words(): player_words.append(PlayerText.text)

如果我们使用ctrl按钮按下任何功能, 那么我们将看到如下所示的功能描述和功能类型, 我们点击size(), 然后将打开以下描述。
在Godot中追加数组

文章图片
你可以向下滚动以查看更多信息吗?
完整的代码:
extends Controlvar player_words=[]var story= "There are many companies like of %s and %s are in the %s section. And many %s companies also which are help under forture 500"var prompts = ["a company name", "a mnc company name", "adverb", "adjective"]onready var PlayerText= $VBoxContainer/HBoxContainer/PlayerTextonready var DisplayText= $VBoxContainer/DisplayTextfunc _ready(): check_player_words_length()func _on_PlayerText_text_entered(new_text): add_to_player_words()func _on_TextureButton_pressed(): add_to_player_words()#Using TextureButtonfunc add_to_player_words(): player_words.append(PlayerText.text) #Using .append method PlayerText.clear() check_player_words_length() #Players word count to calculating lengthfunc is_story_done(): return player_words.size() == prompts.size() # here we are using array .size methodfunc check_player_words_length(): if is_story_done():tell_story() else:prompt_player() # Using If/Else condition to check the logicfunc tell_story(): DisplayText.text= story % player_wordsfunc prompt_player(): DisplayText.text = "May I have" +prompts[player_words.size()]+"please?" #For displaying question to player or user.

然后我们运行代码, 输出将如下所示:
在Godot中追加数组

文章图片
在Godot中追加数组

文章图片
在Godot中追加数组

文章图片
在Godot中追加数组

文章图片
在回答问题之后, 将在下面生成故事:
在Godot中追加数组

文章图片
现在, 我们也可以根据需要更改一些DisplayText:最后, 我们应该添加一个加号(+)将显示文本添加到其中。
这些行是添加的行。
在Godot中追加数组

文章图片
在Godot中追加数组

文章图片
更新的代码是:
extends Controlvar player_words=[]var story= "Optimism is the %s that leads to %s. Nothing can be done without %s and %s."var prompts = ["a verb", "a noun", "verb", "adjective"]onready var PlayerText= $VBoxContainer/HBoxContainer/PlayerTextonready var DisplayText= $VBoxContainer/DisplayTextfunc _ready(): DisplayText.text="Welcome all of u in the world of game and have a wondeful time!" # added line check_player_words_length()func _on_PlayerText_text_entered(new_text): add_to_player_words()func _on_TextureButton_pressed(): add_to_player_words() func add_to_player_words(): player_words.append(PlayerText.text) DisplayText.text="" # Added line PlayerText.clear() check_player_words_length()func is_story_done(): return player_words.size() == prompts.size()func check_player_words_length(): if is_story_done():tell_story() else:prompt_player()func tell_story(): DisplayText.text= story % player_wordsfunc prompt_player(): DisplayText.text += "May I have" +prompts[player_words.size()]+"please?"

添加行后的输出:
在Godot中追加数组

文章图片
在Godot中追加数组

文章图片
在Godot中追加数组

文章图片
在Godot中追加数组

文章图片
【在Godot中追加数组】故事已经准备就绪。
在Godot中追加数组

文章图片

    推荐阅读