Sure! Below is a complete, self-contained HTML file that implements a Wordle clone with modern design, smooth animations, and all the core functionalities you'd expect from the original game. You can save this code as an .html file and open it in your browser to play.
Features:
- Random Word Selection: The game selects a random five-letter word from a predefined list every time you load the page.
- Interactive Grid: A 6x5 grid where each row represents an attempt to guess the word.
- On-Screen Keyboard: An onscreen keyboard that reflects correct, present, and absent letters.
- Animations: Smooth animations for letter flips and keyboard interactions.
- Responsive Design: Looks good on both desktop and mobile devices.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Wordle Clone</title>
<style>
body {
background-color: #121213;
color: #FFFFFF;
font-family: 'Helvetica Neue', Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
padding: 20px;
}
h1 {
margin-bottom: 20px;
font-size: 2em;
}
.grid {
display: grid;
grid-template-rows: repeat(6, 1fr);
grid-gap: 5px;
margin-bottom: 30px;
}
.row {
display: grid;
grid-template-columns: repeat(5, 1fr);
grid-gap: 5px;
}
.tile {
width: 60px;
height: 60px;
border: 2px solid #3a3a3c;
display: flex;
align-items: center;
justify-content: center;
font-size: 2em;
text-transform: uppercase;
font-weight: bold;
background-color: #121213;
transition: background-color 0.3s, border-color 0.3s;
}
.tile.correct {
background-color: #538D4E;
border-color: #538D4E;
color: white;
animation: flip 0.5s forwards;
}
.tile.present {
background-color: #B59F3B;
border-color: #B59F3B;
color: white;
animation: flip 0.5s forwards;
}
.tile.absent {
background-color: #3A3A3C;
border-color: #3A3A3C;
color: white;
animation: flip 0.5s forwards;
}
@keyframes flip {
0% { transform: rotateY(0); }
50% { transform: rotateY(90deg); }
100% { transform: rotateY(0); }
}
.keyboard {
display: flex;
flex-direction: column;
align-items: center;
max-width: 600px;
width: 100%;
}
.keyboard-row {
display: flex;
margin-bottom: 8px;
}
.key {
margin: 0 4px;
padding: 10px;
min-width: 40px;
height: 58px;
background-color: #818384;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
text-transform: uppercase;
font-size: 0.9em;
transition: background-color 0.3s;
}
.key.correct {
background-color: #538D4E;
}
.key.present {
background-color: #B59F3B;
}
.key.absent {
background-color: #3A3A3C;
}
.key:active {
transform: scale(0.95);
}
.modal {
position: fixed;
top: 0;
left: 0;
width:100%;
height:100%;
background: rgba(0,0,0,0.8);
display: flex;
align-items: center;
justify-content: center;
visibility: hidden;
opacity: 0;
transition: visibility 0s, opacity 0.3s;
}
.modal.show {
visibility: visible;
opacity: 1;
}
.modal-content {
background: #1E1E1E;
padding: 30px;
border-radius: 8px;
text-align: center;
}
.close-btn {
margin-top: 20px;
padding: 10px 20px;
background: #818384;
border: none;
border-radius: 4px;
color: white;
cursor: pointer;
transition: background-color 0.3s;
}
.close-btn:hover {
background: #6c6d70;
}
@media (max-width: 600px) {
.tile {
width: 40px;
height: 40px;
font-size: 1.2em;
}
.key {
padding: 8px;
min-width: 30px;
height: 40px;
font-size: 0.8em;
}
}
</style>
</head>
<body>
<h1>Wordle Clone</h1>
<div class="grid" id="grid">
<!-- 6 Rows of 5 Tiles -->
<!-- Rows will be generated by JavaScript -->
</div>
<div class="keyboard" id="keyboard">
<!-- Keyboard Rows -->
<div class="keyboard-row">
<button class="key">Q</button>
<button class="key">W</button>
<button class="key">E</button>
<button class="key">R</button>
<button class="key">T</button>
<button class="key">Y</button>
<button class="key">U</button>
<button class="key">I</button>
<button class="key">O</button>
<button class="key">P</button>
</div>
<div class="keyboard-row">
<button class="key">A</button>
<button class="key">S</button>
<button class="key">D</button>
<button class="key">F</button>
<button class="key">G</button>
<button class="key">H</button>
<button class="key">J</button>
<button class="key">K</button>
<button class="key">L</button>
</div>
<div class="keyboard-row">
<button class="key">Enter</button>
<button class="key">Z</button>
<button class="key">X</button>
<button class="key">C</button>
<button class="key">V</button>
<button class="key">B</button>
<button class="key">N</button>
<button class="key">M</button>
<button class="key">←</button>
</div>
</div>
<!-- Modal for Messages -->
<div class="modal" id="modal">
<div class="modal-content" id="modal-content">
<h2 id="modal-title">Congratulations!</h2>
<p id="modal-text">You guessed the word correctly.</p>
<button class="close-btn" onclick="closeModal()">Close</button>
</div>
</div>
<script>
const WORD_LIST = [
"APPLE", "BRAVE", "CRANE", "DREAM", "EVOKE",
"FLAME", "GRACE", "HOUSE", "INDEX", "JUDGE",
"KNACK", "LARGE", "MIGHT", "NERVE", "OCEAN",
"PLACE", "QUERY", "ROBIN", "STONE", "TRUST",
"UNITY", "VALUE", "WORRY", "XENON", "YOUNG",
"ZEBRA"
];
const grid = document.getElementById('grid');
const keyboard = document.getElementById('keyboard');
const modal = document.getElementById('modal');
const modalTitle = document.getElementById('modal-title');
const modalText = document.getElementById('modal-text');
let currentRow = 0;
let currentTile = 0;
let isGameOver = false;
const solution = WORD_LIST[Math.floor(Math.random() * WORD_LIST.length)];
console.log("Solution:", solution); // For debugging
// Initialize grid
for (let i = 0; i < 6; i++) {
const row = document.createElement('div');
row.classList.add('row');
for (let j = 0; j < 5; j++) {
const tile = document.createElement('div');
tile.classList.add('tile');
row.appendChild(tile);
}
grid.appendChild(row);
}
// Handle Keyboard Clicks
keyboard.addEventListener('click', (e) => {
if (isGameOver) return;
if (e.target.classList.contains('key')) {
const key = e.target.textContent;
handleKey(key);
}
});
// Handle Physical Keyboard
document.addEventListener('keydown', (e) => {
if (isGameOver) return;
const key = e.key.toUpperCase();
if (key === 'ENTER') {
handleKey('Enter');
} else if (key === 'BACKSPACE') {
handleKey('←');
} else if (/^[A-Z]$/.test(key)) {
handleKey(key);
}
});
function handleKey(key) {
if (key === 'Enter') {
submitWord();
return;
}
if (key === '←') {
deleteLetter();
return;
}
if (currentTile < 5 && currentRow < 6) {
const row = grid.children[currentRow];
const tile = row.children[currentTile];
tile.textContent = key;
tile.classList.remove('incorrect', 'correct', 'present');
currentTile++;
}
}
function deleteLetter() {
if (currentTile > 0) {
currentTile--;
const row = grid.children[currentRow];
const tile = row.children[currentTile];
tile.textContent = '';
tile.classList.remove('incorrect', 'correct', 'present');
}
}
function submitWord() {
const row = grid.children[currentRow];
let word = '';
for (let i = 0; i < 5; i++) {
const tile = row.children[i];
word += tile.textContent;
}
if (word.length !== 5) {
showMessage("Not enough letters");
return;
}
if (!WORD_LIST.includes(word)) {
showMessage("Word not in list");
return;
}
// Check letters
const solutionArr = solution.split('');
const guessArr = word.split('');
const letterStatus = Array(5).fill('absent');
// First pass for correct letters
for (let i = 0; i < 5; i++) {
if (guessArr[i] === solutionArr[i]) {
letterStatus[i] = 'correct';
solutionArr[i] = null;
}
}
// Second pass for present letters
for (let i = 0; i < 5; i++) {
if (letterStatus[i] === 'correct') continue;
const index = solutionArr.indexOf(guessArr[i]);
if (index !== -1) {
letterStatus[i] = 'present';
solutionArr[index] = null;
}
}
// Update tiles and keyboard
for (let i = 0; i < 5; i++) {
const tile = row.children[i];
tile.classList.add(letterStatus[i]);
flipTile(tile, letterStatus[i]);
updateKeyboard(guessArr[i], letterStatus[i]);
}
if (word === solution) {
isGameOver = true;
showModal("Congratulations!", "You guessed the word correctly.");
return;
}
currentRow++;
currentTile = 0;
if (currentRow === 6) {
isGameOver = true;
showModal("Game Over", `The word was ${solution}.`);
}
}
function flipTile(tile, status) {
tile.style.animation = "flip 0.5s forwards";
}
function updateKeyboard(letter, status) {
const keys = Array.from(document.querySelectorAll('.key'));
for (let key of keys) {
if (key.textContent === letter) {
if (status === 'correct') {
key.classList.remove('present', 'absent');
key.classList.add('correct');
} else if (status === 'present') {
if (!key.classList.contains('correct')) {
key.classList.remove('absent');
key.classList.add('present');
}
} else {
if (!key.classList.contains('correct') && !key.classList.contains('present')) {
key.classList.add('absent');
}
}
break;
}
}
}
function showMessage(message) {
modalTitle.textContent = "Error";
modalText.textContent = message;
modal.classList.add('show');
}
function showModal(title, text) {
modalTitle.textContent = title;
modalText.textContent = text;
modal.classList.add('show');
}
function closeModal() {
modal.classList.remove('show');
}
</script>
</body>
</html>
How It Works:
-
HTML Structure:
- The
<div class="grid" id="grid"> contains the 6x5 grid for guesses.
- The
<div class="keyboard" id="keyboard"> contains an on-screen keyboard for input.
- The
<div class="modal" id="modal"> is used to display messages like errors or game over.
-
CSS Styling:
- Dark-themed background with colorful tiles similar to Wordle.
- Smooth flip animations when revealing tile statuses.
- Responsive design adjustments for smaller screens.
-
JavaScript Functionality:
- Word Selection: A random word is selected from the
WORD_LIST.
- Grid Initialization: Dynamically creates 6 rows with 5 tiles each.
- Input Handling: Supports both on-screen and physical keyboard inputs.
- Word Submission:
- Checks if the word has 5 letters and is in the
WORD_LIST.
- Compares the guess to the solution and updates tile colors accordingly.
- Updates the on-screen keyboard to reflect the status of each letter.
- End Game Conditions:
- Displays a modal upon winning or exhausting all attempts.
Customization:
- Word List: You can expand the
WORD_LIST array with more five-letter words to make the game more diverse.
- Styling: Feel free to adjust the CSS to better match your design preferences or to add more animations.
- Features: Additional features like sharing results, streak tracking, or persistent storage can be added with further JavaScript enhancements.
Enjoy your Wordle clone!