如何在code.org中为我的刽子手游戏创建函数?
我需要制作一个“提示”按钮,这样当我按下按钮时,指向您要猜测的单词的提示会为您提供线索。
例子
我的话是abandon
,当我点击“提示”按钮时,线索会说如下:
停止支持或照顾(某人);沙漠
我已经为刽子手游戏中的猜测词设置了变量,例如:
var words= ["abandon", "above", "about"];
var word = "";
如何在code.org中为我的刽子手游戏创建函数?
我需要制作一个“提示”按钮,这样当我按下按钮时,指向您要猜测的单词的提示会为您提供线索。
我的话是abandon
,当我点击“提示”按钮时,线索会说如下:
停止支持或照顾(某人);沙漠
我已经为刽子手游戏中的猜测词设置了变量,例如:
var words= ["abandon", "above", "about"];
var word = "";
我对你的描述做了一个小模型。由于 Code.org 有设计和半码,请注意我的设计。您可以与我的设计示例 hangman 程序进行交互。
我做了一个相应的清单hints
。然后我选择一个随机数并存储在index
. 这告诉我使用哪个words
位置hints
。
然后我实现了我的三个按钮。第一个,hintBtn
设置标签的文本,并取消隐藏它(它作为DesignMode的一部分被隐藏)。
第二个按钮 ,newWordBtn
选择一个不同index
的用作答案。它还使提示再次隐藏(我们在用户单击时更改提示标签的文本hintBtn
)。
我留给guessBtn
你完全实施。
//Code.org Project: https://studio.code.org/projects/applab/38gmbu9k7LCiyPIRALOnHVQgEKVQ1PuPkC7hXmOGc3k
var words= ["abandon", "above", "apple"];
var hints = ["to be left without warning", "not below", "Washington's fruit"];
var index = randomNumber(0, words.length - 1);
var word = words[index];
onEvent("hintBtn", "click", function( ) {
//this code will run when "Hint" clicked
//will grab the hint from the hints list
// based on what spot using right now ('index')
setProperty("hintText", "text", hints[index]);
//reveal the hint (want to change the text first)
setProperty("hintText", "hidden", false);
});
onEvent("newWordBtn", "click", function( ) {
//change which word spot using
index = randomNumber(0, words.length - 1);
//rehide the hint text
setProperty("hintText", "hidden", true);
});
onEvent("guessBtn", "click", function( ) {
//to implement
console.log(word);
});
希望这可以帮助。