我需要协助基于文本的游戏,以便在某个级别按下按钮时指定输出代码。
如何编写代码,以便当我在不同级别按下相同的按钮时,它将执行不同的代码行?
这是我的代码:
<!DOCTYPE html>
<html>
<body>
<head>
<script>
let level = 1
if (level === 1) {
alert(level + '. You are walking down a path and see a shadowy figure emerge from the woods, what do you do?')
var button = document.createElement("button");
button.innerHTML = "Threaten";
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);
button.addEventListener("click", function() {
alert("something happens");
});
var button = document.createElement("button");
button.innerHTML = "Flee";
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);
button.addEventListener("click", function() {
alert("something happens");
});
var button = document.createElement("button");
button.innerHTML = "Attack";
var body = document.getElementsByTagName("body")[0];
body.appendChild(button);
button.addEventListener("click", function() {
alert("something happens");
});
};
</script>
</head>
</body>
</html>
请告诉我我做错了什么,因为我无法找到答案。