gpt4 book ai didi

javascript - Whack-A-Mole JavaScript返回和循环不起作用

转载 作者:行者123 更新时间:2023-11-28 02:04:59 28 4
gpt4 key购买 nike

我一直在尝试编码Wack-A-Mole,但是有一些问题。我目前有两个return语句,但是当我将stopper3和stopper4设置为true并在Trigger中重新运行该函数时。这是行不通的。另外,我编写了diwn两个函数来重复检查stopper1和stopper2是否为true,然后再次运行,但我不知道如何循环使用它们。我不希望有一天,setTimeout无法正常工作,并且我不了解回调。

我要问的是,如何循环2个Cycle函数,以及为什么在函数的停止器再次设置为true之后,return语句不会取消返回。

    <!DOCTYPE html>
<html>
<head>
<title>Whack-A-Mole</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<center>
<p>Try to hit the Wack-a-moles!</p>
<button id="start">Click here to start!</button>
<button id="restart">Restart</button>
<button id="pause">Pause</button>
<button id="resume">Resume</button>
<p id= highscore>Highscore: 0 points!</p>
<p id="time"></p>
<div id="game">
<br>
<img src="moleman.png" alt="Please refresh" id="b1">
<img src="moleman2.png" alt="Please refresh" id="bb1">
<img src="moleman.png" alt="Please refresh" id="b2">
<img src="moleman2.png" alt="Please refresh" id="bb2">
<img src="moleman.png" alt="Please refresh" id="b3">
<img src="moleman2.png" alt="Please refresh" id="bb3">
<br>
<img src="moleman.png" alt="Please refresh" id="b4">
<img src="moleman2.png" alt="Please refresh" id="bb4">
<img src="moleman.png" alt="Please refresh" id="b5">
<img src="moleman2.png" alt="Please refresh" id="bb5">
<img src="moleman.png" alt="Please refresh" id="b6">
<img src="moleman2.png" alt="Please refresh" id="bb6">
<br>
<img src="moleman.png" alt="Please refresh" id="b7">
<img src="moleman2.png" alt="Please refresh" id="bb7">
<img src="moleman.png" alt="Please refresh" id="b8">
<img src="moleman2.png" alt="Please refresh" id="bb8">
<img src="moleman.png" alt="Please refresh" id="b9">
<img src="moleman2.png" alt="Please refresh" id="bb9">
</div>
<p>Your score is:</p>
<p id="T">0 points!</p>

<script type= "text/javascript">
document.getElementById("b1").addEventListener("click", Score);
document.getElementById("b2").addEventListener("click", Score);
document.getElementById("b3").addEventListener("click", Score);
document.getElementById("b4").addEventListener("click", Score);
document.getElementById("b5").addEventListener("click", Score);
document.getElementById("b6").addEventListener("click", Score);
document.getElementById("b7").addEventListener("click", Score);
document.getElementById("b8").addEventListener("click", Score);
document.getElementById("b9").addEventListener("click", Score);

document.getElementById("start").addEventListener("click", Start);
document.getElementById("pause").addEventListener("click", Pause);
document.getElementById("resume").addEventListener("click", Resume);
document.getElementById("restart").addEventListener("click", Restart);

var score = 0;

function Score() {
score++;
if(score == 1){
document.getElementById("T").innerHTML = score + ' point!';
} else {
document.getElementById("T").innerHTML = score + ' points!';
}

document.getElementById('b1').style.display = "none";
document.getElementById('bb1').style.display = "inline";
document.getElementById('b2').style.display = "none";
document.getElementById('bb2').style.display = "inline";
document.getElementById('b3').style.display = "none";
document.getElementById('bb3').style.display = "inline";
document.getElementById('b4').style.display = "none";
document.getElementById('bb4').style.display = "inline";
document.getElementById('b5').style.display = "none";
document.getElementById('bb5').style.display = "inline";
document.getElementById('b6').style.display = "none";
document.getElementById('bb6').style.display = "inline";
document.getElementById('b7').style.display = "none";
document.getElementById('bb7').style.display = "inline";
document.getElementById('b8').style.display = "none";
document.getElementById('bb8').style.display = "inline";
document.getElementById('b9').style.display = "none";
document.getElementById('bb9').style.display = "inline";

Highscore();
}

function Score0() {
score = 0;
document.getElementById("T").innerHTML = '0 points!';
}

var highscore = 0;

function Highscore() {
if(score > highscore) {
highscore = score;
}
if(highscore == 1){
document.getElementById("highscore").innerHTML = 'Highscore: ' + highscore + ' point!';
} else {
document.getElementById("highscore").innerHTML = 'Highscore: ' + highscore + ' points!';
}
}

var stopper1;
var stopper3;
var stopper4;

function Trigger() {

document.getElementById('b1').style.display = "inline";
document.getElementById('bb1').style.display = "none";
document.getElementById('b2').style.display = "inline";
document.getElementById('bb2').style.display = "none";
document.getElementById('b3').style.display = "inline";
document.getElementById('bb3').style.display = "none";
document.getElementById('b4').style.display = "inline";
document.getElementById('bb4').style.display = "none";
document.getElementById('b5').style.display = "inline";
document.getElementById('bb5').style.display = "none";
document.getElementById('b6').style.display = "inline";
document.getElementById('bb6').style.display = "none";
document.getElementById('b7').style.display = "inline";
document.getElementById('bb7').style.display = "none";
document.getElementById('b8').style.display = "inline";
document.getElementById('bb8').style.display = "none";
document.getElementById('b9').style.display = "inline";
document.getElementById('bb9').style.display = "none";

if(stopper3 === false) {
return;
}

document.getElementById("b1").style.visibility = "hidden";
document.getElementById("b2").style.visibility = "hidden";
document.getElementById("b3").style.visibility = "hidden";
document.getElementById("b4").style.visibility = "hidden";
document.getElementById("b5").style.visibility = "hidden";
document.getElementById("b6").style.visibility = "hidden";
document.getElementById("b7").style.visibility = "hidden";
document.getElementById("b8").style.visibility = "hidden";
document.getElementById("b9").style.visibility = "hidden";
document.getElementById("bb1").style.visibility = "hidden";
document.getElementById("bb2").style.visibility = "hidden";
document.getElementById("bb3").style.visibility = "hidden";
document.getElementById("bb4").style.visibility = "hidden";
document.getElementById("bb5").style.visibility = "hidden";
document.getElementById("bb6").style.visibility = "hidden";
document.getElementById("bb7").style.visibility = "hidden";
document.getElementById("bb8").style.visibility = "hidden";
document.getElementById("bb9").style.visibility = "hidden";

if(stopper4 === false) {
return;
}

var x = Math.floor((Math.random() * 9) + 1);
if (x == 1) {
document.getElementById("b1").style.visibility = "visible";
document.getElementById("bb1").style.visibility = "visible";
} else if (x == 2){
document.getElementById("b2").style.visibility = "visible";
document.getElementById("bb2").style.visibility = "visible";
} else if (x == 3){
document.getElementById("b3").style.visibility = "visible";
document.getElementById("bb3").style.visibility = "visible";
} else if (x == 4){
document.getElementById("b4").style.visibility = "visible";
document.getElementById("bb4").style.visibility = "visible";
} else if (x == 5){
document.getElementById("b5").style.visibility = "visible";
document.getElementById("bb5").style.visibility = "visible";
} else if (x == 6){
document.getElementById("b6").style.visibility = "visible";
document.getElementById("bb6").style.visibility = "visible";
} else if (x == 7){
document.getElementById("b7").style.visibility = "visible";
document.getElementById("bb7").style.visibility = "visible";
} else if (x == 8){
document.getElementById("b8").style.visibility = "visible";
document.getElementById("bb8").style.visibility = "visible";
} else if (x == 9){
document.getElementById("b9").style.visibility = "visible";
document.getElementById("bb9").style.visibility = "visible";
}

function Cycle1() {

if (stopper1) {
setTimeout(Trigger,1000);
}
}
}

var stopper2;
var showtime = 61;

function Time() {
showtime--;
if(showtime == 1){
document.getElementById('time').innerHTML = showtime + " second left!";
} else {
document.getElementById('time').innerHTML = showtime + " seconds left!";
}

if (showtime < 0) {
document.getElementById('time').innerHTML = "Time's up!";
stopper1 = false;
stopper2 = false;
document.getElementById('pause').style.display = "none";
document.getElementById('resume').style.display = "none";
}
function Cycle2() {

if (stopper2) {
setTimeout(Time,1000);
}
}
}

function Time61() {
showtime = 61;
}

function Start() {
stopper1 = true;
stopper3 = true;
stopper4 = true;
Trigger();
stopper2 = true;
Time();
document.getElementById("start").style.display = "none";
document.getElementById("pause").style.display = "inline";
document.getElementById("restart").style.display = "inline";
document.getElementById("time").style.display = "inline";

}

function Pause() {
document.getElementById('resume').style.display = "inline";
document.getElementById('pause').style.display = "none";
stopper1 = false;
stopper2 = false;
stopper3 = false;
}

function Resume() {
document.getElementById('pause').style.display = "inline";
document.getElementById('resume').style.display = "none";
stopper1 = true;
stopper2 = true;
stopper3 = true;
}

function Restart() {
document.getElementById("start").style.display = "inline";
document.getElementById("pause").style.display = "none";
document.getElementById("resume").style.display = "none";
document.getElementById("restart").style.display = "none";
document.getElementById("time").style.display = "none";
Score0();
Time61();
stopper1 = false;
stopper2 = false;
stopper4 = false;
}



</script>

</center>
</body>
</html>


的CSS

:html {
font-size:40px;
}




#time {

}

button#start {
margin-top:-100px;
font-size:24px;
width:300px;
height:100px;
}

button#pause {
display:none;
}

button#resume {
display: none;
}

button#restart {
display: none;
}

img {
width:100px;
height:65px;
}

#b1 {
visibility: hidden;
}

#b2 {
visibility: hidden;
}

#b3 {
visibility: hidden;
}

#b4 {
visibility: hidden;
}

#b5 {
visibility: hidden;
}

#b6 {
visibility: hidden;
}

#b7 {
margin-left:-12px;
visibility: hidden;
}

#b8 {
visibility: hidden;
}

#b9 {
visibility: hidden;
}

#bb1 {
display: none;
visibility: hidden;
}

#bb2 {
display: none;
visibility: hidden;
}

#bb3 {
display: none;
visibility: hidden;
}

#bb4 {
display: none;
visibility: hidden;
}

#bb5 {
display: none;
visibility: hidden;
}

#bb6 {
display: none;
visibility: hidden;
}

#bb7 {
margin-left:-12px;
display: none;
visibility: hidden;
}

#bb8 {
display: none;
visibility: hidden;
}

#bb9 {
display: none;
visibility: hidden;
}

#T {
margin-top:-50px;
text-decoration: underline;
}

最佳答案

回油塞

我不清楚返回值,但是有没有缘由从未将stopper4设置为true?您将stopper4设置为false两次或三次,但从未设置为true

循环功能

这是循环两个循环功能的方式:

  (function Cycle1() {
if (stopper1) {
setTimeout(Trigger, 1000);
}
})();


请注意此语法与您所拥有的语法之间的区别。此语法将立即执行该功能。该模式如下所示:

(function(){

})();


(请注意,您甚至可以根据需要删除函数名称-代码中未使用或不需要它们)

您可以研究立即调用函数表达式(IIFE)。请在此处找到一个代码笔: https://codepen.io/anon/pen/ZrZBLx

您可能会发现这并不能解决所有问题,但是您非常非常亲密。做得好!

关于javascript - Whack-A-Mole JavaScript返回和循环不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49080038/

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