gpt4 book ai didi

javascript - 淡入随机 Javascript 游戏

转载 作者:太空宇宙 更新时间:2023-11-04 06:06:58 24 4
gpt4 key购买 nike

我正在尝试使用 javascript 在我的迷你游戏中解决这个问题。游戏假设使用 randomFadeIn 和 jquery.random-fade-in.min.js 随机显示 div。它有效,但问题是我无法阻止它运行。这只是一个基本的 javascript 游戏,但很难使用 jquery 实现

这是我的完整代码

const result = document.getElementById(".box-container>div");
console.log(result);
const button = document.getElementsByTagName("div");
let sec = 0;

function gameStart(num) {
let num1 = 800;
if ($(".box-container>div>p").css('opacity') != 0) {
console.log("not yet done");
$(function() {
$('.box-container').randomFadeIn(800);
});
} else {
console.log("win");
};
}

function clickBox() {
$(".box-container>div>p").click(function() {
$(this).animate({
opacity: 0
}, 800);
})
}

function gameWins() {}

function gameStops() {
setTimeout(function() {
alert("Game Ends");
}, 60000);
}

clickBox();
//gameStops();
gameWins();
.box-container {
width: 232px;
float: left;
width: 45%;
}

.box-container div {
float: left;
height: 100px;
margin-bottom: 8px;
margin-right: 8px;
overflow: hidden;
width: 100px;
}

.box-container div p {
background: #097;
box-sizing: border-box;
color: #fff;
display: none;
font-size: 20px;
height: 100%;
margin: 0;
padding-top: 14px;
text-align: center;
width: 100%;
}

.clearfix:after {
clear: both;
content: '';
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://sutara79.github.io/jquery.random-fade-in/dist/jquery.random-fade-in.js"></script>

<h1> Click Dem Boxes</h1>
<button onclick="gameStart()"> Start game </button>
<p>Mechanics: You need to click all the boxes before the time ends</p>

> just a bunch of divs that fades in and does not stop
<div class="box-container clearfix">
<div>
<p></p>
</div>
<div>
<p></p>
</div>
<div>
<p></p>
</div>
<div>
<p></p>
</div>
<div>
<p></p>
</div>

最佳答案

通过使用 .stop()功能,你可以停止动画。请参阅下面的代码段。

let maxSeconds = 30000;
let numOfCards = $('.box').length;

function gameStart() {
console.log("Game started");
let numOfClicked = 0;

$(".box-container>div>p").click(function() {
// Increase the counter
numOfClicked++;

// Fade out
$(this).fadeOut(800);

if(numOfClicked == numOfCards){
gameWon();
}
})

$('.box-container').randomFadeIn(800);

setTimeout(
function() {
if(numOfClicked != numOfCards){
gameLost();
}
}, maxSeconds);
}

function gameWon(){
gameStop();
console.log("You won the game!");
}

function gameLost(){
gameStop();
console.log("You lost the game!");
}

function gameStop(){
$(".box-container>div>p").stop(false, false);
}
.box-container {
width: 232px;
float: left;
width: 45%;
}

.box-container div {
float: left;
height: 100px;
margin-bottom: 8px;
margin-right: 8px;
overflow: hidden;
width: 100px;
}

.box-container div p {
background: #097;
box-sizing: border-box;
color: #fff;
display: none;
font-size: 20px;
height: 100%;
margin: 0;
padding-top: 14px;
text-align: center;
width: 100%;
}

.clearfix:after {
clear: both;
content: '';
display: block;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://sutara79.github.io/jquery.random-fade-in/dist/jquery.random-fade-in.js"></script>

<h1> Click Dem Boxes</h1>
<button onclick="gameStart()"> Start game </button>
<p>Mechanics: You need to click all the boxes before the time ends</p>

> just a bunch of divs that fades in and does not stop
<div class="box-container clearfix">
<div class="box">
<p></p>
</div>
<div class="box">
<p></p>
</div>
<div class="box">
<p></p>
</div>
<div class="box">
<p></p>
</div>
<div class="box">
<p></p>
</div>
</div>

关于javascript - 淡入随机 Javascript 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56772878/

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