我对你的描述做了一个小模型。由于 Code.org 有设计和半码,请注意我的设计。你可以和我的设计互动sample hangman program。
我做了一个对应的列表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);
});
希望这会有所帮助。