Below are some mini-game ideas suitable for development with VueJS 3. These projects range from simple to more complex, allowing you to pick one that matches your skill level and interests. Each idea includes a brief description, core features to implement, and some tips to get you started:
1. Vue Rock, Paper, Scissors
Description:
A classic game of chance where players choose one of rock, paper, or scissors, and the computer generates a random choice. The winner is determined by game rules (rock beats scissors, scissors beats paper, paper beats rock).
Core Features:
- User input (rock, paper, scissors)
- Computer's random choice generation
- Result determination and display
- Score tracking
Tip:
Use Vue's reactivity for updating scores and results in real-time. Utilize CSS for a visually appealing choice representation. For example, you can use the Composition API with script setup to manage game logic:
<template>
<div>
<button @click="play('rock')">Rock</button>
<button @click="play('paper')">Paper</button>
<button @click="play('scissors')">Scissors</button>
<p>Result: {{ result }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const result = ref('')
function play(userChoice) {
const choices = ['rock', 'paper', 'scissors']
const computerChoice = choices[Math.floor(Math.random() * choices.length)]
if (userChoice === computerChoice) {
result.value = 'Tie!'
} else if ((userChoice === 'rock' && computerChoice === 'scissors') ||
(userChoice === 'scissors' && computerChoice === 'paper') ||
(userChoice === 'paper' && computerChoice === 'rock')) {
result.value = 'You win!'
} else {
result.value = 'Computer wins!'
}
}
</script>
2. Quiz App with Timer
Description:
A timed quiz app that presents users with a series of questions. Users must answer before the time runs out.
Core Features:
- Question and answer data storage (possibly in a JSON file)
- Timer implementation with countdown
- Question navigation (next, previous)
- Answer submission and scoring
- Final score display after quiz completion or time expiration
Tip:
Leverage Vue's lifecycle hooks for managing the timer. Consider using a library like Vuex or Pinia for state management, especially if your quiz data is extensive. Here’s a simple timer example using script setup:
<template>
<div>
<p>Time left: {{ timeLeft }}</p>
<button @click="startTimer">Start</button>
</div>
</template>
<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
const timeLeft = ref(60) // 60 seconds
let intervalId
function startTimer() {
intervalId = setInterval(() => {
timeLeft.value--
if (timeLeft.value === 0) {
clearInterval(intervalId)
alert('Time\'s up!')
}
}, 1000)
}
onUnmounted(() => {
clearInterval(intervalId)
})
</script>
3. Memory Match Game
Description:
A memory game where users are presented with a grid of face-down cards. Users flip two cards at a time to try and find matches.
Core Features:
- Grid generation with card components
- Card flip animation and state management
- Match detection and feedback
- Game over condition (all matches found) with final time/score
Tip:
Utilize Vue's transitions for smooth card flip animations. Manage card states (flipped/unflipped, matched) with Vue's reactive properties. For example, you can create a CardComponent.vue:
<template>
<div @click="flipCard" class="card" :class="{ flipped: isFlipped }">
<div class="front"></div>
<div class="back">{{ value }}</div>
</div>
</template>
<script setup>
import { ref } from 'vue'
const props = defineProps({
value: String,
index: Number
})
const isFlipped = ref(false)
function flipCard() {
isFlipped.value = !isFlipped.value
// Emit event to parent to handle match logic
emit('flip', { index: props.index, value: props.value })
}
</script>
<style>
.card {
position: relative;
width: 100px;
height: 100px;
border: 1px solid black;
display: inline-block;
margin: 10px;
transition: transform 0.5s;
}
.flipped {
transform: rotateY(180deg);
}
.front,
.back {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
backface-visibility: hidden;
border-radius: 10px;
display: flex;
justify-content: center;
align-items: center;
font-size: 36px;
}
.back {
transform: rotateY(180deg);
}
</style>
Then, manage the game logic in the parent component using an array of cards and handling flips:
<template>
<div>
<CardComponent
v-for="(card, index) in cards"
:key="index"
:value="card.value"
:index="index"
@flip="handleFlip" />
</div>
</template>
<script setup>
import { ref } from 'vue'
import CardComponent from './CardComponent.vue'
const cards = ref([
{ value: 'A', matched: false },
{ value: 'A', matched: false },
{ value: 'B', matched: false },
{ value: 'B', matched: false }
])
const flippedCards = ref([])
function handleFlip({ index, value }) {
flippedCards.value.push({ index, value })
if (flippedCards.value.length === 2) {
const [card1, card2] = flippedCards.value
if (card1.value === card2.value) {
// Match found, update cards
cards.value[card1.index].matched = true
cards.value[card2.index].matched = true
} else {
// No match, flip back after a delay
setTimeout(() => {
flippedCards.value = []
}, 1000)
}
}
}
</script>
4. Vue Snake Game
Description:
A classic snake game where the player controls a snake that grows as it consumes food pellets. The game ends if the snake collides with itself or the boundary.
Core Features:
- Game board rendering
- Snake movement controls (keyboard or touch)
- Food pellet random generation
- Collision detection (snake with itself, boundary, food)
- Score tracking and display
Tip:
For smooth animations, consider using requestAnimationFrame within a Vue component. Use a 2D array to represent the game board for efficient collision checks. Here's a simplified example of rendering the game board and handling snake movement:
<template>
<div>
<canvas ref="gameCanvas" width="400" height="400"></canvas>
<p>Score: {{ score }}</p>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
const gameCanvas = ref(null)
const score = ref(0)
const snake = ref([{x: 10, y: 10}, {x: 11, y: 10}, {x: 12, y: 10}])
const direction = ref('right')
const food = ref({ x: 15, y: 15 })
let animationId
function draw() {
const ctx = gameCanvas.value.getContext('2d')
ctx.clearRect(0, 0, 400, 400)
snake.value.forEach(({ x, y }) => {
ctx.fillStyle = 'green'
ctx.fillRect(x * 20, y * 20, 20, 20)
})
ctx.fillStyle = 'red'
ctx.fillRect(food.value.x * 20, food.value.y * 20, 20, 20)
}
function update() {
for (let i = snake.value.length - 1; i > 0; i--) {
snake.value[i] = { ...snake.value[i - 1] }
}
if (direction.value === 'right') {
snake.value[0].x += 1
} else if (direction.value === 'left') {
snake.value[0].x -= 1
} else if (direction.value === 'up') {
snake.value[0].y -= 1
} else if (direction.value === 'down') {
snake.value[0].y += 1
}
// Collision detection and food consumption logic
if (snake.value[0].x === food.value.x && snake.value[0].y === food.value.y) {
score.value++
food.value = {
x: Math.floor(Math.random() * 20),
y: Math.floor(Math.random() * 20)
}
snake.value.push({ x: snake.value[snake.value.length - 1].x, y: snake.value[snake.value.length - 1].y })
}
if (snake.value[0].x < 0 || snake.value[