gpt4 book ai didi

javascript - 在 javascript 中创建游戏 - 分配正确答案

转载 作者:行者123 更新时间:2023-11-28 07:55:40 26 4
gpt4 key购买 nike

我正在制作一个非常简单的游戏,您必须选择正确的动物来匹配显示的动物图案。

我需要用正确的动物分配正确的打印品,这样分数才能正确加起来。不确定如何着手进行此操作,如能提供任何帮助,我们将不胜感激。

这是代码。

<html>
<head>
<script>
function randSort (a,b) {return Math.random() - 0.5}

var questions = [
{text: " What animal is this?", img: "AnimalPrints/1.jpg", answers: ["Cheetah", "Tiger", "Ladybird"], ans: },
{text: " What animal is this one?", img: "AnimalPrints/2.jpg", answers: ["Elephant", "Giraffe", "Snake"], ans: "Giraffe"},
{text: "What animal is this one please?", img: "AnimalPrints/3.jpg", answers: ["Bumblebee", "Tiger", "Lady bird"], ans: "Bumblebee"}
];

var correctCount = 0;
var currentQ = 0;

function select(nr) {
if (nr == questions[currentQ].ans)
{
correctCount++;
document.getElementById('display').innerHTML= "You win"
}
else
{
document.getElementById('display').innerHTML= "You lose"
}
document.getElementById('display').innerHTML += "<p>Score: "+ correctCount;

// if its the last one
nextQ();
}

function showQ() {
document.getElementById('questionText').innerHTML = questions[currentQ].text;

document.getElementById('animalPrint').src = questions[currentQ].img;

newhtml = "";
for (var i = 0; i< questions[currentQ].answers.length; i++)
{
newhtml+= "<button onclick = 'select(" + i + ")'>"+ questions[currentQ].answers[i] + "</button>";
}
document.getElementById('alloptions').innerHTML = newhtml;
}

function nextQ(){
if (currentQ < questions.length-1)
{
currentQ++;
showQ();
}
}

window.onload =init;

function init()
{
correctCount = 0;
questions.sort(randSort);
currentQ = 0;
showQ();

}

</script>
<title> Game_page</title>

<link rel="stylesheet" type="text/css" href="gamepage_css.css">
<script type="text/javascript">
/*
document.write("<img src = \ "" + Math.floor 1 + Math.random() * 10) +
".jpg\" />");

document.write("<img src = \"" + Math.floor 1 + Math.random() * )
*/
</script>
</head>

<body>
<div id=main>
<button id="nextbutton" onclick="nextQ();"></button></a>
<div id="questionText"> Testing</div>
<div id="animal">
<img id="animalPrint" src="AnimalPrints/1.jpg">
</div>
<div id="alloptions">
</div>
</div>
<div id="display">Output</div>
</body>
</html>

最佳答案

此脚本还有更多问题,但希望这些小更新能有所帮助。添加了一些调整以显示和选择功能

 function select(e) {
if (e.target.textContent == questions[currentQ].ans)
{
correctCount++;
document.getElementById('display').innerHTML= "You win"
}
else
{
document.getElementById('display').innerHTML= "You lose"
}
document.getElementById('display').innerHTML += "<p>Score: "+ correctCount;

// if its the last one
nextQ();
}

function showQ() {
document.getElementById('questionText').innerHTML = questions[currentQ].text;

document.getElementById('animalPrint').src = questions[currentQ].img;

newhtml = "";
for (var i = 0; i< questions[currentQ].answers.length; i++)
{
newhtml+= "<button>"+ questions[currentQ].answers[i] + "</button>";
}
document.getElementById('alloptions').innerHTML = newhtml;
document.getElementById('alloptions').addEventListener('click', select)
}

关于javascript - 在 javascript 中创建游戏 - 分配正确答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30128980/

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