右键单击标志扫雷器
我正在建造一个扫雷游戏。我遵循了一个教程,以获取基础知识。然后在这里还使用一些帮助来交替颜色。我的下一个问题是我被卡在右键单击以获取标志。我尝试使用“上下文菜单”,然后使用e.preventdefault()。但是,它仍然行不通。作为参考,这是代码。
var board = [];
var rows = 10;
var columns = 10;
var minesCount = 10;
var minesLocation = [];
var tilesClicked = 0;
var flagEnabled = false;
var gameOver = false;
window.onload = function() {
startGame();
}
function setMines() {
let minesLeft = minesCount;
while (minesLeft > 0) {
let r = Math.floor(Math.random() * rows);
let c = Math.floor(Math.random() * columns);
let id = r.toString() + "-" + c.toString();
if (!minesLocation.includes(id)) {
minesLocation.push(id);
minesLeft -= 1;
}
}
}
function startGame() {
document.getElementById("mines-count").innerText = minesCount;
document.getElementById("board").addEventListener("contextmenu", e=> {
e.preventDefault();
setFlag();
});
setMines();
for (let r = 0; r < rows; r++) {
let row = [];
for (let c = 0; c < columns; c++) {
let tile = document.createElement("div");
tile.id = r.toString() + "-" + c.toString();
tile.addEventListener("click", clickTile);
document.getElementById("board").append(tile);
row.push(tile);
}
board.push(row);
}
console.log(board);
}
function setFlag() {
if (flagEnabled) {
flagEnabled = false;
document.getElementById("flag-button").style.backgroundColor = "lightgray";
}
else {
flagEnabled = true;
document.getElementById("flag-button").style.backgroundColor = "darkgray";
}
}
function clickTile() {
if (gameOver || this.classList.contains("tile-clicked")) {
return;
}
let tile = this;
if (flagEnabled) {
if (tile.innerText == "") {
tile.innerText = "
I am building a minesweeper game. I followed a tutorial to get the basics. Then also used some help here for alternating colors. My next problem is that I am stuck on the right click for flag. I have tried using "context menu" then using e.preventDefault(). However, it still does not work. For reference, here is the code.
var board = [];
var rows = 10;
var columns = 10;
var minesCount = 10;
var minesLocation = [];
var tilesClicked = 0;
var flagEnabled = false;
var gameOver = false;
window.onload = function() {
startGame();
}
function setMines() {
let minesLeft = minesCount;
while (minesLeft > 0) {
let r = Math.floor(Math.random() * rows);
let c = Math.floor(Math.random() * columns);
let id = r.toString() + "-" + c.toString();
if (!minesLocation.includes(id)) {
minesLocation.push(id);
minesLeft -= 1;
}
}
}
function startGame() {
document.getElementById("mines-count").innerText = minesCount;
document.getElementById("board").addEventListener("contextmenu", e=> {
e.preventDefault();
setFlag();
});
setMines();
for (let r = 0; r < rows; r++) {
let row = [];
for (let c = 0; c < columns; c++) {
let tile = document.createElement("div");
tile.id = r.toString() + "-" + c.toString();
tile.addEventListener("click", clickTile);
document.getElementById("board").append(tile);
row.push(tile);
}
board.push(row);
}
console.log(board);
}
function setFlag() {
if (flagEnabled) {
flagEnabled = false;
document.getElementById("flag-button").style.backgroundColor = "lightgray";
}
else {
flagEnabled = true;
document.getElementById("flag-button").style.backgroundColor = "darkgray";
}
}
function clickTile() {
if (gameOver || this.classList.contains("tile-clicked")) {
return;
}
let tile = this;
if (flagEnabled) {
if (tile.innerText == "") {
tile.innerText = "????";
}
else if (tile.innerText == "????") {
tile.innerText = "";
}
return;
}
if (minesLocation.includes(tile.id)) {
gameOver = true;
revealMines();
return;
}
let coords = tile.id.split("-");
let r = parseInt(coords[0]);
let c = parseInt(coords[1]);
checkMine(r, c);
}
function revealMines() {
for (let r= 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
let tile = board[r][c];
if (minesLocation.includes(tile.id)) {
tile.innerText = "????";
tile.style.backgroundColor = "red";
}
}
}
}
function checkMine(r, c) {
if (r < 0 || r >= rows || c < 0 || c >= columns) {
return;
}
if (board[r][c].classList.contains("tile-clicked")) {
return;
}
board[r][c].classList.add("tile-clicked");
tilesClicked += 1;
let minesFound = 0;
minesFound += checkTile(r-1, c-1); //top left
minesFound += checkTile(r-1, c); //top
minesFound += checkTile(r-1, c+1); //top right
minesFound += checkTile(r, c-1); //left
minesFound += checkTile(r, c+1); //right
minesFound += checkTile(r+1, c-1); //bottom left
minesFound += checkTile(r+1, c); //bottom
minesFound += checkTile(r+1, c+1); //bottom right
if (minesFound > 0) {
board[r][c].innerText = minesFound;
board[r][c].classList.add("x" + minesFound.toString());
}
else {
checkMine(r-1, c-1); //top left
checkMine(r-1, c); //top
checkMine(r-1, c+1); //top right
checkMine(r, c-1); //left
checkMine(r, c+1); //right
checkMine(r+1, c-1); //bottom left
checkMine(r+1, c); //bottom
checkMine(r+1, c+1); //bottom right
}
if (tilesClicked == rows * columns - minesCount) {
document.getElementById("mines-count").innerText = "Cleared";
gameOver = true;
}
}
function checkTile(r, c) {
if (r < 0 || r >= rows || c < 0 || c >= columns) {
return 0;
}
if (minesLocation.includes(r.toString() + "-" + c.toString())) {
return 1;
}
return 0;
}
body {
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
text-align: center;
}
#board {
width: 500px;
height: 500px;
border: 10px solid darkgray;
margin: 0 auto;
display: flex;
flex-wrap: wrap;
}
#board div {
--primary-lightness: 58%;
--secondary-lightness: 55%;
--hue: 80deg;
--sat: 63%;
--lightness: var(--primary-lightness);
background-color: hsl(var(--hue) var(--sat) var(--lightness));
width: 50px;
height: 50px;
font-size: 30px;
display: flex;
justify-content: center;
align-items: center;
}
#board div:nth-child(odd) {
--lightness: var(--secondary-lightness);
}
#board div:is(:nth-child(20n+2), :nth-child(20n+4), :nth-child(20n+6), :nth-child(20n+8), :nth-child(20n+10)) {
--lightness: var(--secondary-lightness);
}
#board div:is(:nth-child(20n+1), :nth-child(20n+3), :nth-child(20n+5), :nth-child(20n+7), :nth-child(20n+9)) {
--lightness: var(--primary-lightness);
}
#board .tile-clicked {
--primary-lightness: 76%;
--secondary-lightness: 72%;
--hue: 30deg;
--sat: 57%;
--lightness: var(--primary-lightness);
}
.x1 {
color: blue;
}
.x2 {
color: green;
}
.x3 {
color: red;
}
.x4 {
color: navy;
}
.x5 {
color: brown;
}
.x6 {
color: teal;
}
.x7 {
color: black;
}
.x8 {
color: gray;
}
#flag-button {
width: 100px;
height: 50px;
font-size: 30px;
background-color: lightgray;
border: none;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Minesweeper</title>
<link rel="stylesheet" href="minesweeper.css">
<script src="minesweeper.js"></script>
</head>
<body>
<h1>Mines: <span id="mines-count">0</span></h1>
<div id="board"></div>
<br>
<button id="flag-button">????</button>
</body>
</html>
Sorry if this is a rookie mistake. I am still pretty new to the language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论