累了的时候看看这个 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>贪吃蛇</title>
<style>
canvas {
display: block;
margin: 0 auto;
background-color: #f0f0f0;
border: 1px solid #c0c0c0;
}
</style>
</head>
<body>
<canvas id="game" width="300" height="300"></canvas>
<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
const gridSize = 15;
const tileSize = canvas.width / gridSize;
let snake = [{x: 7, y: 7}];
let food = {x: 3, y: 3};
let dx = 0;
let dy = 0;
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (const part of snake) {
ctx.fillStyle = 'green';
ctx.fillRect(part.x * tileSize, part.y * tileSize, tileSize, tileSize);
ctx.strokeStyle = 'white';
ctx.strokeRect(part.x * tileSize, part.y * tileSize, tileSize, tileSize);
}
ctx.fillStyle = 'red';
ctx.fillRect(food.x * tileSize, food.y * tileSize, tileSize, tileSize);
snake.unshift({x: snake[0].x + dx, y: snake[0].y + dy});
snake.pop();
if (snake[0].x === food.x && snake[0].y === food.y) {
snake.push({});
food.x = Math.floor(Math.random() * gridSize);
food.y = Math.floor(Math.random() * gridSize);
}
for (let i = 1; i < snake.length; i++) {
if (snake[i].x === snake[0].x && snake[i].y === snake[0].y) {
snake = [{x: 7, y: 7}];
dx = 0;
dy = 0;
}
}
if (snake[0].x < 0 || snake[0].x >= gridSize || snake[0].y < 0 || snake[0].y >= gridSize) {
snake = [{x: 7, y: 7}];
dx = 0;
dy = 0;
}
}
function handleKeydown(event) {
if (event.key === 'ArrowUp' && dy === 0) {
dx = 0;
dy = -1;
} else if (event.key === 'ArrowDown' && dy === 0) {
dx = 0;
dy = 1;
} else if (event.key === 'ArrowLeft' && dx === 0) {
dx = -1;
dy = 0;
} else if (event.key === 'ArrowRight' && dx === 0) {
dx = 1;
dy = 0;
}
}
document.addEventListener('keydown', handleKeydown);
setInterval(draw, 100);
</script>
</body>
</html>
@Maboroshii 可以的,可以持续优化
下面是一个简单的用JavaScript实现扫雷的例子:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>扫雷</title>
<style>
.cell {
width: 20px;
height: 20px;
border: 1px solid #ccc;
display: inline-block;
}
.cell.clicked {
background-color: #ddd;
}
.cell.mine {
background-color: red;
}
</style>
</head>
<body>
<div id="board"></div>
<script>
var board = document.getElementById("board");
var rows = 10;
var cols = 10;
var mines = 10;
var cells = [];
var mineCells = [];
function createBoard() {
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
var cell = document.createElement("div");
cell.classList.add("cell");
cell.dataset.row = i;
cell.dataset.col = j;
cell.addEventListener("click", handleClick);
board.appendChild(cell);
cells.push(cell);
}
board.appendChild(document.createElement("br"));
}
}
function placeMines() {
for (var i = 0; i < mines; i++) {
var index = Math.floor(Math.random() * cells.length);
var cell = cells[index];
if (mineCells.indexOf(cell) === -1) {
cell.classList.add("mine");
mineCells.push(cell);
} else {
i--;
}
}
}
function handleClick(event) {
var cell = event.target;
if (cell.classList.contains("clicked")) {
return;
}
if (cell.classList.contains("mine")) {
cell.classList.add("clicked");
alert("你输了!");
resetGame();
return;
}
var row = parseInt(cell.dataset.row);
var col = parseInt(cell.dataset.col);
var adjacentMines = getAdjacentMines(row, col);
cell.classList.add("clicked");
if (adjacentMines) {
cell.innerHTML = adjacentMines;
} else {
var adjacents = getAdjacentCells(row, col);
adjacents.forEach(function (adjacent) {
handleClick({target: adjacent});
});
}
if (cells.filter(function (cell) {
return !cell.classList.contains("clicked");
}).length === mineCells.length) {
alert("你赢了!");
resetGame();
return;
}
}
function getAdjacentCells(row, col) {
var adjacents = [];
for (var i = row - 1; i <= row + 1; i++) {
if (i < 0 || i >= rows) {
continue;
}
for (var j = col - 1; j <= col + 1; j++) {
if (j < 0 || j >= cols) {
continue;
}
if (i === row && j === col) {
continue;
}
adjacents.push(cells[i * cols + j]);
}
}
return adjacents;
}
function getAdjacentMines(row, col) {
var adjacents = getAdjacentCells(row, col);
return adjacents.filter(function (adjacent) {
return adjacent.classList.contains("mine");
}).length;
}
function resetGame() {
board.innerHTML = "";
cells = [];
mineCells = [];
createBoard();
placeMines();
}
createBoard();
placeMines();
</script>
</body>
</html>
这是一个基于 DOM 的实现,没有使用 Canvas,每个扫雷单元都是一个 div
元素,点击单元格时可能会出现以下三种情况之一:
玩家赢得游戏的条件是所有没有被揭开的单元格都是雷,即游戏中雷的数量等于游戏面板上没有被揭开的单元格数。
另外,为了确保游戏的可玩性,我们在随机放置雷时,如果随机到了已经有雷的单元格,会忽略这个雷,重新随机放置。
过早客微信公众号:guozaoke • 过早客新浪微博:@过早客 • 广告投放合作微信:fullygroup50 鄂ICP备2021016276号-2 • 鄂公网安备42018502001446号