console
const gameConfig = {
level: 5,
hasAutoHidden: false,
}
let totalScore = 0
const mouses = document.querySelectorAll(".mouse")
let timeout
const hit = (e) => {
score.innerText = `得分:${++totalScore}`
const mouse = e.target
mouse.disabled = true
if (!gameConfig.hasAutoHidden) hidden(mouse)
clearTimeout(timeout)
gameConfig.hasAutoHidden = false
}
mouses.forEach((mouse) => mouse.addEventListener("click", hit))
const show = (mouse) => {
mouse.style.transform = "translateY(-40px)"
timeout = setTimeout(() => {
gameConfig.hasAutoHidden = true
hidden(mouse)
}, 2 * (1000 - gameConfig.level * 100))
}
const hidden = (mouse) => {
mouse.style.transform = "translateY(0)"
setTimeout(() => {
mouse.disabled = false
show(getRandomMouse())
}, 1 * (1000 - gameConfig.level * 100))
}
const getRandomMouse = () => mouses[~~(Math.random() * 9)]
const restart = () => {
score.innerText = "得分:0"
hiddenAll()
clearTimeout(timeout)
setTimeout(
() => show(getRandomMouse()),
1 * (1000 - gameConfig.level * 100)
)
}
const hiddenAll = () =>
mouses.forEach((mouse) => {
mouse.style.transform = "translateY(0)"
mouse.style.transition = `transform ${
1 - gameConfig.level / 10
}s linear`
clearTimeout(timeout)
})
const setLevel = () => {
const value = prompt("设置游戏难度,请输入1-9。别手贱,会鬼畜。。。")
const warning = () => {
alert("请输入有效值")
setLevel()
}
switch (value) {
case null:
console.log("点击了取消")
break
case "":
warning()
break
default: {
if (Number(value)) {
gameConfig.level = Number(value)
hiddenAll()
setTimeout(restart, 1000)
} else warning()
}
}
}
restart()
<header>
<span id="score">得分:0</span>
<button onclick="setLevel()">难度</button>
<button onclick="restart()">重玩</button>
</header>
<main>
<section class="mouse-ground">
<div class="hole"></div>
<button class="mouse"></button>
<div class="grass"></div>
</section>
<div class="mouse-ground">
<div class="hole"></div>
<button class="mouse"></button>
<div class="grass"></div>
</div>
<div class="mouse-ground">
<div class="hole"></div>
<button class="mouse"></button>
<div class="grass"></div>
</div>
<div class="mouse-ground">
<div class="hole"></div>
<button class="mouse"></button>
<div class="grass"></div>
</div>
<div class="mouse-ground">
<div class="hole"></div>
<button class="mouse"></button>
<div class="grass"></div>
</div>
<div class="mouse-ground">
<div class="hole"></div>
<button class="mouse"></button>
<div class="grass"></div>
</div>
<div class="mouse-ground">
<div class="hole"></div>
<button class="mouse"></button>
<div class="grass"></div>
</div>
<div class="mouse-ground">
<div class="hole"></div>
<button class="mouse"></button>
<div class="grass"></div>
</div>
<div class="mouse-ground">
<div class="hole"></div>
<button class="mouse"></button>
<div class="grass"></div>
</div>
</main>
<footer>一些说明</footer>
html,
body {
height: 100%;
margin: 0;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
body {
width: 320px;
}
header,
main,
footer {
display: flex;
width: 100%;
height: 60px;
}
header {
justify-content: space-between;
align-items: center;
background: yellow;
}
main {
flex-wrap: wrap;
justify-content: space-around;
align-items: center;
height: 320px;
background: green;
}
footer {
justify-content: center;
align-items: center;
background: red;
}
.mouse-ground {
position: relative;
display: flex;
justify-content: center;
align-items: center;
width: 84px;
height: 84px;
background: blue;
}
.hole {
width: 60px;
height: 60px;
border-radius: 30px;
background: black;
}
.mouse {
position: absolute;
left: 22px;
top: 44px;
width: 40px;
height: 40px;
background: purple;
transition: transform 1s linear;
transform: translateY(0);
cursor: pointer;
}
.grass {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 40px;
background: greenyellow;
}