随机数字消消乐游戏JavaScript
时间: 2023-08-15 15:05:57 浏览: 38
好,以下是一个简单的随机数字消消乐游戏的 JavaScript 代码示例:
```javascript
// 定义一些常量
const ROWS = 8; // 行数
const COLS = 8; // 列数
const NUM_COLORS = 5; // 可用颜色数量
// 定义一些变量
let gameBoard = []; // 存储游戏面板的二维数组
let selectedPiece = null; // 存储选中的方块
let score = 0; // 分数
// 初始化游戏面板
function initGameBoard() {
for (let i = 0; i < ROWS; i++) {
gameBoard[i] = [];
for (let j = 0; j < COLS; j++) {
gameBoard[i][j] = Math.floor(Math.random() * NUM_COLORS);
}
}
}
// 渲染游戏面板
function renderGameBoard() {
let board = document.getElementById("board");
board.innerHTML = "";
for (let i = 0; i < ROWS; i++) {
for (let j = 0; j < COLS; j++) {
let piece = document.createElement("div");
piece.classList.add("piece");
piece.style.backgroundColor = getColor(gameBoard[i][j]);
piece.addEventListener("click", () => {
selectPiece(i, j);
});
board.appendChild(piece);
}
}
}
// 获取指定颜色代码
function getColor(colorCode) {
switch (colorCode) {
case 0:
return "red";
case 1:
return "blue";
case 2:
return "green";
case 3:
return "yellow";
case 4:
return "purple";
default:
return "black";
}
}
// 选中指定方块
function selectPiece(row, col) {
// 如果已经选中方块,则尝试交换两个方块
if (selectedPiece !== null) {
let rowDiff = Math.abs(selectedPiece.row - row);
let colDiff = Math.abs(selectedPiece.col - col);
if ((rowDiff === 1 && colDiff === 0) || (rowDiff === 0 && colDiff === 1)) {
swapPieces(selectedPiece, { row, col });
selectedPiece = null;
checkMatches();
}
} else {
selectedPiece = { row, col };
}
}
// 交换两个方块
function swapPieces(piece1, piece2) {
let temp = gameBoard[piece1.row][piece1.col];
gameBoard[piece1.row][piece1.col] = gameBoard[piece2.row][piece2.col];
gameBoard[piece2.row][piece2.col] = temp;
renderGameBoard();
}
// 检查是否有匹配的方块
function checkMatches() {
let matches = [];
for (let i = 0; i < ROWS; i++) {
for (let j = 0; j < COLS; j++) {
let color = gameBoard[i][j];
if (color !== -1) {
let match = findMatch(i, j, color);
if (match.length > 2) {
matches.push(match);
}
}
}
}
if (matches.length > 0) {
removeMatches(matches);
}
}
// 查找匹配的方块
function findMatch(row, col, color) {
let match = [{ row, col }];
let visited = [];
visited[row * COLS + col] = true;
let queue = [{ row, col }];
while (queue.length > 0) {
let { row, col } = queue.shift();
let neighbors = getNeighbors(row, col);
for (let neighbor of neighbors) {
let { row: r, col: c } = neighbor;
if (!visited[r * COLS + c] && gameBoard[r][c] === color) {
match.push(neighbor);
visited[r * COLS + c] = true;
queue.push(neighbor);
}
}
}
return match;
}
// 获取相邻的方块
function getNeighbors(row, col) {
let neighbors = [];
if (row > 0) {
neighbors.push({ row: row - 1, col });
}
if (row < ROWS - 1) {
neighbors.push({ row: row + 1, col });
}
if (col > 0) {
neighbors.push({ row, col: col - 1 });
}
if (col < COLS - 1) {
neighbors.push({ row, col: col + 1 });
}
return neighbors;
}
// 移除匹配的方块
function removeMatches(matches) {
for (let match of matches) {
for (let piece of match) {
gameBoard[piece.row][piece.col] = -1;
}
score += match.length;
}
renderGameBoard();
}
// 初始化游戏
function initGame() {
initGameBoard();
renderGameBoard();
}
// 开始游戏
initGame();
```
这个代码示例实现了一个基本的随机数字消消乐游戏,玩家可以通过点击相邻的方块来交换它们的位置,如果有三个或以上相同颜色的方块连成一排或一列,则这些方块将被消除并得分。