Here's a simple Simon game implementation using Vue.js 3 with Composition API. This example includes the game logic, button colors, and sound effects. You can further style and customize the game as needed.
First, make sure to install the necessary dependencies:
npm install vue@next
npm install --save-dev @vitejs/plugin-vue
Then, create a SimonGame.vue file with the following content:
<template>
<div id="simon-game">
<h1>Simon Game</h1>
<div id="game-buttons">
<button
v-for="(color, index) in colors"
:key="index"
:style="{ backgroundColor: color }"
@click="handleButtonClick(index)"
></button>
</div>
<button id="start-over" @click="startGame">Start Over</button>
<audio ref="audio" />
</div>
</template>
<script setup>
import { ref, reactive } from "vue";
const colors = ["red", "blue", "green", "yellow"];
const gameButtons = document.getElementById("game-buttons");
const audio = ref(null);
const gameSequence = reactive([]);
const playerSequence = reactive([]);
const level = ref(0);
const isGameActive = ref(false);
function playSound(color) {
const audioElement = document.createElement("audio");
audioElement.src = `sounds/${color}.mp3`;
audioElement.play();
audio.value = audioElement;
}
function generateSequence() {
const randomColor = colors[Math.floor(Math.random() * 4)];
gameSequence.push(randomColor);
playSound(randomColor);
lightUpButton(randomColor);
}
function lightUpButton(color) {
const button = gameButtons.querySelector(`button[style*="${color}"]`);
button.classList.add("light-up");
setTimeout(() => button.classList.remove("light-up"), 200);
}
function handleButtonClick(index) {
if (isGameActive.value) {
const color = colors[index];
playerSequence.push(color);
playSound(color);
checkPlayerSequence();
}
}
function checkPlayerSequence() {
if (playerSequence[playerSequence.length - 1] !== gameSequence[playerSequence.length - 1]) {
alert("Game Over! Press 'Start Over' to try again.");
resetGame();
} else if (playerSequence.length === gameSequence.length) {
setTimeout(generateSequence, 1000);
playerSequence.length = 0;
level.value++;
}
}
function startGame() {
isGameActive.value = true;
level.value = 0;
gameSequence.length = 0;
playerSequence.length = 0;
generateSequence();
}
function resetGame() {
isGameActive.value = false;
level.value = 0;
gameSequence.length = 0;
playerSequence.length = 0;
}
startGame();
</script>
<style scoped>
#game-buttons {
display: flex;
justify-content: space-around;
margin-bottom: 20px;
}
button {
width: 100px;
height: 100px;
border: none;
border-radius: 5px;
cursor: pointer;
}
button.light-up {
animation: light-up 200ms ease-in-out;
}
@keyframes light-up {
0% {
opacity: 1;
}
50% {
opacity: 0.5;
}
100% {
opacity: 1;
}
}
</style>
This example assumes you have the sound files (red.mp3, blue.mp3, green.mp3, yellow.mp3) in a sounds folder in the same directory as your Vue component.
To run the game, create a main.js file with the following content:
import { createApp } from "vue";
import SimonGame from "./SimonGame.vue";
createApp(SimonGame).mount("#app");
Then, create an index.html file with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simon Game</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
Finally, run the game using Vite:
npm init @vitejs/config
npm run dev
Open your browser and navigate to http://localhost:3000 to play the Simon game.