这是一个宾果(bingo)游戏的代码 后附github地址有完整代码 喜欢的话留个star哦
<template>
<div class="bingo-container">
<h3>Bingo Game</h3>
<div class="bingo-board">
<div
v-for="(cell, index) in boardFlat"
:key="index"
:class="['bingo-cell', { selected: cell.selected, bingo: cell.isBingo }]"
:style="{ backgroundColor: cell.isBingo ? 'rgba(124, 76, 220, 1)' : cell.selected ? 'rgba(109, 45, 8, 1)' : 'lightgray' }"
@click="markCell(index)"
>
{{ cell.number }}
</div>
<!-- 绘制所有胜利的线 -->
<div
v-for="(style, index) in bingoLines"
:key="index"
class="bingo-line"
:style="style"
></div>
</div>
<button @click="resetGame">Reset Game</button>
</div>
</template>
<script setup>
import { ref, onMounted } from 'vue';
const size = 5; // 5x5 Bingo board
const boardFlat = ref([]); // One-dimensional array to store board cells
const bingo = ref(false); // Bingo flag
const bingoLines = ref([]); // Array to store styles for each winning line
// Initialize the Bingo board
const initBoard = () => {
let numbers = Array.from({ length: size * size }, (_, i) => i + 1);
numbers = shuffleArray(numbers);
boardFlat.value = numbers.map(number => ({ number, selected: false, isBingo: false }));
};
// Shuffle the array of numbers
const shuffleArray = (array) => {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
};
// Calculate the winning line style dynamically
const calculateLineStyle = (type, index = 0) => {
const cellSize = 49; // Each cell's width/height
const gap = 11; // Gap between cells
const padding = 27; // Board padding
if (type === 'row') {
const topPosition = padding + index * (cellSize + gap) + cellSize / 2 - 1.5;
return {
top: `${topPosition}px`,
left: `${padding}px`,
width: `${size * (cellSize + gap) - gap}px`, // Width covers 5 cells minus gaps
height: '3px',
position: 'absolute',
backgroundColor: 'rgba(82, 33, 156, 1)',
};
} else if (type === 'col') {
const leftPosition = padding + index * (cellSize + gap) + cellSize / 2 - 1.5;
return {
top: `${padding}px`,
left: `${leftPosition}px`,
height: `${size * (cellSize + gap) - gap}px`, // Height covers 5 cells minus gaps
width: '3px',
position: 'absolute',
backgroundColor: 'rgba(82, 33, 156, 1)',
};
} else if (type === 'diag1') {
return {
top: `${padding}px`,
left: `${padding}px`,
width: `${Math.sqrt(2) * (size * cellSize + (size - 1) * gap)}px`,
height: '3px',
position: 'absolute',
backgroundColor: 'rgba(82, 33, 156, 1)',
transform: 'rotate(45deg)',
transformOrigin: '0 0',
};
} else if (type === 'diag2') {
return {
top: `${padding}px`,
right: `${padding}px`,
width: `${Math.sqrt(2) * (size * cellSize + (size - 1) * gap)}px`,
height: '3px',
position: 'absolute',
backgroundColor: 'rgba(82, 33, 156, 1)',
transform: 'rotate(-45deg)',
transformOrigin: '100% 0',
};
}
};
// Reset the game
const resetGame = () => {
bingo.value = false;
bingoLines.value = [];
initBoard(); // Reinitialize the board
};
// Initialize the Bingo board on mount
onMounted(() => {
initBoard();
});
</script>
效果图:
以上是宾果的vue3代码 实现基础的选中变色,连线变色+bingo成线,同时检测多条的情况。
https://github.com/gaoxiaolei1/bingo