个性化阅读
专注于IT技术分析

字典-添加故事模板

字典类型。关联容器包含唯一键引用的值。参考始终通过字典。

Godot不支持在迭代过程中擦除元素。

创建字典:

var d = {4: 5, "A key": "A value", 28: [1, 2, 3]}

要将键添加到现有字典, 请像现有键一样对其进行访问并分配给它:

d[4] = "hello"  # Add integer four as a key and assign the string "hello" as its value.
d["Godot"] = 3.01  # Add String "Godot" as a key and assign the value 3.01 to it.

这是处理库存管理之类的完美方法, 而不仅仅是库存管理, 我们可以做到的所有事情都是一个很好的用例。在这里, 我们了解到它如何对我们的游戏有帮助。

为了制作字典, 我们有一个名为My_ dictionary = {}的函数。

现在, 如果我们有一个数组, 那么也会有一个值。

my_array = [value1, value2]

如果我们有字典, 那么我们就有键值。

My_ dictionary = {key: value, key: value}

我们也可以有一个字符串数组。

current_story = {prompt:[" ", " "], story:" "}

例子:

extends Control
var player_words=[]
var current_story = {
	"prompts":["a name", "a noun", "adverb", "adjective"], "story" : "It takes many %s deeds to build a %s reputation, and %s bad one to %s it."
    }


onready var PlayerText= $VBoxContainer/HBoxContainer/PlayerText
onready var DisplayText= $VBoxContainer/DisplayText

func _ready():
	DisplayText.text="Welcome all of u in the world of game and have a wondeful time!"
	check_player_words_length()
	PlayerText.grab_focus()

func _on_PlayerText_text_entered(new_text):
	add_to_player_words()

func _on_TextureButton_pressed():
	if is_story_done():
		get_tree().reload_current_scene()
	else:
		add_to_player_words() 

func add_to_player_words():
	player_words.append(PlayerText.text)
	DisplayText.text=""
	PlayerText.clear()
	check_player_words_length()

func is_story_done():
	return player_words.size() == current_story.prompts.size()

func check_player_words_length():
	if is_story_done():
		end_game()
	else:
		prompt_player()

func tell_story():
	DisplayText.text= current_story.story % player_words

func prompt_player():
	DisplayText.text += "May I have" +current_story.prompts[player_words.size()]+"please?"

func end_game():
	PlayerText.queue_free()
	$VBoxContainer/HBoxContainer/Label.text="Again!"
	tell_story()

输出如下:

字典-添加故事模板

之后, 完成故事。

使用字典完成代码:

extends Control
var player_words=[]
var template = [
{
	"prompts":["a name", "a noun", "adverb", "adjective"], "story" : "It takes many %s deeds to build a %s reputation, and %s bad one to %s it."
    }, {
		"story":"There once was %s called %s who searched far and wide for the mythical %s noun of %s.", "prompts": ["a noun", "a name", "an adjective", "another name"], }, {
		"story":"Once upon a time a %s ate a %s felt very %s. It was a %s day for all good %s.", "prompts": ["a name", "a thing", "a feeling", "another feeling", "some things"], }, {
		"story":"There once was %s called %s that lived as %s as a %s." , "prompts": ["a thing", "a name", "a descrition", "a thing"], }, {
		"story":"There once was %s called %s who searched far and wide for the mythical %s noun of %s.", "prompts": ["a noun", "a name", "an adjective", "another name"], }, {
		"story":"a Poem.\n\n1 I was a %s as can be, \n then we could call me %s, \n and i would talk to %s ", "prompts": ["a noun (a thing)", "an adjective(a description word)", "a person's name"], }, {
		"story":"Dear %s, I hope this letter finds we well. I have spent the past three weeks in %s researching the %s of donkies for my new book. I miss you hesitantly, and whenever i see a %s I think of u. ", "prompts": ["a person's name", "the name of a place", "the plural of a noun", "another name"] 
	}, {
		"story":"Once upon a time, a %s hero called %s was sent to %s, tio defeat a %s %s. He did so, return to defeat %s", "prompts": ["an adjective", "a person's name", "a place", "an adjective", "a noun", "another place"], }, {
		"story":"The ultimate pizza recipe. \n\n Moix one packet of %s with three spoonfuls of flour. Knead it as it %s then put the %s and %s on it, after that open the oven and %s it.", "prompts": ["a noun", "an adjective", "another noun", "yet another noun", "adjective"] 
	}
	] # Dictionaries stories
var current_story

onready var PlayerText= $VBoxContainer/HBoxContainer/PlayerText
onready var DisplayText= $VBoxContainer/DisplayText

func _ready():
	randomize()
	current_story=template[randi()% template.size()]
	set_current_story()
	DisplayText.text="Welcome all of u in the world of game and have a wondeful time!"
	check_player_words_length()
	PlayerText.grab_focus()

func set_current_story():
	randomize()
	current_story=template[randi()% template.size()]
	
func _on_PlayerText_text_entered(new_text):
	add_to_player_words()

func _on_TextureButton_pressed():
	if is_story_done():
		get_tree().reload_current_scene()
	else:
		add_to_player_words() 

func add_to_player_words():
	player_words.append(PlayerText.text)
	DisplayText.text=""
	PlayerText.clear()
	check_player_words_length()

func is_story_done():
	return player_words.size() == current_story.prompts.size()

func check_player_words_length():
	if is_story_done():
		end_game()
	else:
		prompt_player()

func tell_story():
	DisplayText.text= current_story.story % player_words

func prompt_player():
	DisplayText.text += "May I have" +current_story.prompts[player_words.size()]+"please?"

func end_game():
	PlayerText.queue_free()
	$VBoxContainer/HBoxContainer/Label.text="Again!"
	tell_story()

在输出部分, 我们将有许多故事不断出现。我们只需要添加我们的话。

输出如下:

字典-添加故事模板
字典-添加故事模板
字典-添加故事模板
字典-添加故事模板

我们可以做许多以上的故事。

在下一个教程中, 我们将学习StoryObjects。


赞(0)
未经允许不得转载:srcmini » 字典-添加故事模板

评论 抢沙发

评论前必须登录!