-
Notifications
You must be signed in to change notification settings - Fork 839
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4786 from 1911aditi/main
added a game memory flip
- Loading branch information
Showing
6 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Memory Flip Game | ||
|
||
This is a simple memory flip game created using HTML, CSS, and JavaScript. The objective of the game is to find all matching pairs of cards by flipping them two at a time. | ||
|
||
## Features | ||
|
||
- Simple and intuitive user interface | ||
- Dynamic card generation and shuffling | ||
- Smooth card flipping animations | ||
- Matching pairs detection | ||
|
||
## How to Play | ||
|
||
1. Open the game in a web browser. | ||
2. Click on any card to flip it and reveal its content. | ||
3. Click on another card to try and find its matching pair. | ||
4. If the two flipped cards match, they will remain revealed. | ||
5. If the two flipped cards do not match, they will be flipped back after a short delay. | ||
6. Continue flipping cards to find all matching pairs. | ||
|
||
## Getting Started | ||
|
||
To run the game locally, follow these steps: | ||
|
||
1. Clone the repository or download the source code. | ||
2. Open the `index.html` file in your preferred web browser. | ||
|
||
## Files | ||
|
||
- `index.html`: The main HTML file containing the structure of the game. | ||
- `styles.css`: The CSS file for styling the game board and cards. | ||
- `script.js`: The JavaScript file containing the game logic and functionality. | ||
|
||
## Customization | ||
|
||
You can customize the game by modifying the `cardsArray` in the `script.js` file to include different card values or images. | ||
|
||
## Example | ||
|
||
```javascript | ||
const cardsArray = [ | ||
{ name: 'A', img: 'A' }, | ||
{ name: 'B', img: 'B' }, | ||
{ name: 'C', img: 'C' }, | ||
{ name: 'D', img: 'D' }, | ||
{ name: 'E', img: 'E' }, | ||
{ name: 'F', img: 'F' }, | ||
{ name: 'G', img: 'G' }, | ||
{ name: 'H', img: 'H' } | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Memory Flip Game</title> | ||
<link rel="stylesheet" href="styles.css"> | ||
</head> | ||
<body> | ||
<div class="game-container"> | ||
<div class="game-board" id="game-board"> | ||
<!-- Cards will be dynamically inserted here --> | ||
</div> | ||
</div> | ||
<script src="script.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// script.js | ||
|
||
const cardsArray = [ | ||
{ name: 'A', img: 'A' }, | ||
{ name: 'B', img: 'B' }, | ||
{ name: 'C', img: 'C' }, | ||
{ name: 'D', img: 'D' }, | ||
{ name: 'E', img: 'E' }, | ||
{ name: 'F', img: 'F' }, | ||
{ name: 'G', img: 'G' }, | ||
{ name: 'H', img: 'H' } | ||
]; | ||
|
||
// Duplicate the cards array to create pairs | ||
const gameCards = cardsArray.concat(cardsArray); | ||
gameCards.sort(() => 0.5 - Math.random()); // Shuffle the cards | ||
|
||
const gameBoard = document.getElementById('game-board'); | ||
|
||
let firstCard = null; | ||
let secondCard = null; | ||
let lockBoard = false; | ||
|
||
gameCards.forEach(card => { | ||
const cardElement = document.createElement('div'); | ||
cardElement.classList.add('card'); | ||
cardElement.dataset.name = card.name; | ||
|
||
const frontFace = document.createElement('div'); | ||
frontFace.classList.add('front'); | ||
frontFace.textContent = card.img; | ||
|
||
const backFace = document.createElement('div'); | ||
backFace.classList.add('back'); | ||
backFace.textContent = '?'; | ||
|
||
cardElement.appendChild(frontFace); | ||
cardElement.appendChild(backFace); | ||
|
||
cardElement.addEventListener('click', flipCard); | ||
|
||
gameBoard.appendChild(cardElement); | ||
}); | ||
|
||
function flipCard() { | ||
if (lockBoard) return; | ||
if (this === firstCard) return; | ||
|
||
this.classList.add('flip'); | ||
|
||
if (!firstCard) { | ||
firstCard = this; | ||
return; | ||
} | ||
|
||
secondCard = this; | ||
lockBoard = true; | ||
|
||
checkForMatch(); | ||
} | ||
|
||
function checkForMatch() { | ||
const isMatch = firstCard.dataset.name === secondCard.dataset.name; | ||
|
||
isMatch ? disableCards() : unflipCards(); | ||
} | ||
|
||
function disableCards() { | ||
firstCard.removeEventListener('click', flipCard); | ||
secondCard.removeEventListener('click', flipCard); | ||
|
||
resetBoard(); | ||
} | ||
|
||
function unflipCards() { | ||
setTimeout(() => { | ||
firstCard.classList.remove('flip'); | ||
secondCard.classList.remove('flip'); | ||
|
||
resetBoard(); | ||
}, 1000); | ||
} | ||
|
||
function resetBoard() { | ||
[firstCard, secondCard] = [null, null]; | ||
lockBoard = false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* styles.css */ | ||
body { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
background-color: #f0f0f0; | ||
margin: 0; | ||
font-family: Arial, sans-serif; | ||
} | ||
|
||
.game-container { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
flex-direction: column; | ||
} | ||
|
||
.game-board { | ||
display: grid; | ||
grid-template-columns: repeat(4, 100px); | ||
grid-gap: 10px; | ||
} | ||
|
||
.card { | ||
width: 100px; | ||
height: 100px; | ||
background-color: #333; | ||
color: white; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
font-size: 24px; | ||
cursor: pointer; | ||
position: relative; | ||
transform: rotateY(0deg); | ||
transition: transform 0.5s; | ||
} | ||
|
||
.card.flip { | ||
transform: rotateY(180deg); | ||
} | ||
|
||
.card .front, | ||
.card .back { | ||
position: absolute; | ||
width: 100%; | ||
height: 100%; | ||
backface-visibility: hidden; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
|
||
.card .front { | ||
background-color: #f9f9f9; | ||
color: #333; | ||
} | ||
|
||
.card .back { | ||
background-color: #333; | ||
color: white; | ||
transform: rotateY(180deg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.