Skip to content

Commit

Permalink
Merge pull request #4786 from 1911aditi/main
Browse files Browse the repository at this point in the history
added a game memory flip
  • Loading branch information
kunjgit authored Jul 15, 2024
2 parents f03b14f + a6121f6 commit e7a5a4a
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Games/Memory Flip/README.md
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' }
];
17 changes: 17 additions & 0 deletions Games/Memory Flip/index.html
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>
87 changes: 87 additions & 0 deletions Games/Memory Flip/script.js
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;
}
64 changes: 64 additions & 0 deletions Games/Memory Flip/styles.css
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);
}
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,9 @@ This repository also provides one such platforms where contributers come over an
| [Alien_Invasion](https://github.com/kunjgit/GameZone/tree/main/Games/Alien_Invasion) |
| [Drawing_App](https://github.com/kunjgit/GameZone/tree/main/Games/Drawing_app) |
| [Dodge_the_Blocks](https://github.com/kunjgit/GameZone/tree/main/Games/Dodge_the_Blocks) |

| [Memory_Flip](https://github.com/kunjgit/GameZone/tree/main/Games/Memory_Flip) |

|[Town-Rise](https://github.com/kunjgit/GameZone/tree/main/Games/Town_Rise_Game)|
| [IKnowYou-Mind-Reading-Game](https://github.com/kunjgit/GameZone/tree/main/Games/IKnowYou-Mind-Reading-Game) |
|[Color Swap](https://github.com/kunjgit/GameZone/tree/main/Games/Color_Swap)|
Expand Down
Binary file added assets/images/Memory_Flip.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e7a5a4a

Please sign in to comment.