gpt4 book ai didi

javascript - 如何让我的游戏发挥作用? ELI5

转载 作者:行者123 更新时间:2023-11-28 07:32:12 24 4
gpt4 key购买 nike

我正在尝试制作一个游戏,根据用户在输入框中输入的数字来显示图像序列。我希望它显示图像数组中的随机图像,无论用户设置它显示多少次。

这是我的 JavaScript:

var numLeaves = 0;
var numSeq = ["1.jpg","2.png","3.jpg","4.jpg","5.png","6.jpg","7.jpg","8.jpg","9.jpg","0.jpg"];

function startGame() {

while (1 < 10) {
randomNum = Math.floor((Math.random() * numSeq.length));

if (randomNum === 0) {
numLeaves = 1;
} else if (randomNum == 1) {
numLeaves = 2;
} else if (randomNum == 2) {
numLeaves = 3;
} else if (randomNum == 3) {
numLeaves = 4;
} else if (randomNum == 4) {
numLeaves = 5;
} else if (randomNum == 5) {
numLeaves = 6;
} else if (randomNum == 6) {
numLeaves = 7;
} else if (randomNum == 7) {
numLeaves = 8;
} else if (randomNum == 8) {
numLeaves = 9;
} else if (randomNum == 9) {
numLeaves = 0;
}

document.getElementById("picture").src = numSeq[randomNum];
setInterval(function(){document.getElementById("picture").src = ""}, 1500);
i++;
}


}

function submitInput() {
if (document.getElementById("leaveGuess").value == numLeaves) {
document.getElementById("result").innerHTML = "Correct!";
}
else
{
document.getElementById("result").innerHTML = "Incorrect... There were " + numLeaves + " leaves on the shamrock";
}

}

这是我的 HTML:

<!DOCTYPE html>

<html>

<head>
<title></title>
<link rel="stylesheet" type="text/css" href="rep.css">
<script type="text/javascript" src="rep.js"></script>
</head>


<body>
<div id="test"></div>

<div id="content">

<div id="title" class="center">
<h1>Repetitive</h1>
<p>Created by Daniel Hancock</p>
<h2>Instructions:</h2>
<p>In under one second memorize the amount of leaves on the shamrock. <br> After one second, you will be asked to enter the number of leaves that you saw on the shamrock.</p>
<label for="numSequence"><strong>Length:</strong></label>
<input id="numSequence"> <br>
<button onclick="startGame()" id="startButton">New</button>
</div>

<div id="display" class="center">
<img src="" id="picture" width="215" height="215">
<div id="guessing" class="center">
<input id="leaveGuess">
<button onclick="submitInput()">Submit</button>
<p id="result"></p>
</div>
</div>

</div>
</body>

</html>

像我五岁 child 一样向我解释。

最佳答案

我不明白你的问题,但我发现代码中存在一些问题:

1)你没有将 numLeaves 声明为全局变量(也许)并且你在没有使用他的情况下更新了他 10 次。

2) 设置变量 i = 0;在 startGame 函数开始时或者更好的解决方案是使用 for 代替 while

3)为什么你使用 randomNum === 0 而不是 randomNum == 0?

提示:你可以写叶子数 = (随机数 + 1) % 10;而不是 10 个 if 命令

关于javascript - 如何让我的游戏发挥作用? ELI5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29014562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com