js实现记忆翻牌小游戏
时间: 2023-08-10 19:05:55 浏览: 193
js记忆翻牌连连看游戏代码
好的,我可以给你提供一个简单的记忆翻牌小游戏的实现,使用JavaScript和HTML/CSS。
HTML部分:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>记忆翻牌小游戏</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>记忆翻牌小游戏</h1>
<div class="game">
<div class="cards">
<div class="card" data-value="1"></div>
<div class="card" data-value="2"></div>
<div class="card" data-value="3"></div>
<div class="card" data-value="4"></div>
<div class="card" data-value="1"></div>
<div class="card" data-value="2"></div>
<div class="card" data-value="3"></div>
<div class="card" data-value="4"></div>
</div>
<button class="start-btn">开始游戏</button>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS部分:
```css
.game {
margin: 0 auto;
text-align: center;
}
.cards {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.card {
width: 100px;
height: 100px;
margin: 10px;
background-color: #ccc;
border-radius: 5px;
cursor: pointer;
font-size: 50px;
display: flex;
justify-content: center;
align-items: center;
color: #fff;
font-family: Arial, sans-serif;
transition: all 0.2s ease-in-out;
transform-style: preserve-3d;
backface-visibility: hidden;
}
.card.flipped {
transform: rotateY(180deg);
}
.start-btn {
margin-top: 20px;
padding: 10px 20px;
font-size: 20px;
background-color: #3498db;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
transition: all 0.2s ease-in-out;
}
.start-btn:hover {
background-color: #2980b9;
}
```
JavaScript部分:
```javascript
const cards = document.querySelectorAll('.card');
const startBtn = document.querySelector('.start-btn');
let firstCard, secondCard;
let hasFlippedCard = false;
let lockBoard = false;
let score = 0;
function flipCard() {
if (lockBoard) return;
if (this === firstCard) return;
this.classList.add('flipped');
if (!hasFlippedCard) {
hasFlippedCard = true;
firstCard = this;
return;
}
secondCard = this;
checkForMatch();
}
function checkForMatch() {
let isMatch = firstCard.dataset.value === secondCard.dataset.value;
isMatch ? disableCards() : unflipCards();
}
function disableCards() {
firstCard.removeEventListener('click', flipCard);
secondCard.removeEventListener('click', flipCard);
resetBoard();
score++;
if (score === cards.length / 2) {
endGame();
}
}
function unflipCards() {
lockBoard = true;
setTimeout(() => {
firstCard.classList.remove('flipped');
secondCard.classList.remove('flipped');
resetBoard();
}, 1000);
}
function resetBoard() {
[hasFlippedCard, lockBoard] = [false, false];
[firstCard, secondCard] = [null, null];
}
function shuffle() {
cards.forEach(card => {
let randomPos = Math.floor(Math.random() * cards.length);
card.style.order = randomPos;
});
}
function startGame() {
shuffle();
cards.forEach(card => {
card.classList.add('flipped');
setTimeout(() => {
card.classList.remove('flipped');
card.addEventListener('click', flipCard);
}, 2000);
});
}
function endGame() {
document.querySelector('.game').innerHTML = '<h1>恭喜你通关了!</h1>';
}
startBtn.addEventListener('click', startGame);
```
这个小游戏的实现思路是:先将卡片随机打乱顺序并翻过来,等待一段时间后再翻回去,等待玩家开始游戏。当玩家点击一张卡片时,如果已经有一张卡片被翻开,则比较两张卡片是否匹配,如果匹配则禁用这两张卡片,如果不匹配则翻回去。当所有的卡片都被禁用后,游戏结束。
阅读全文