我正在开发一个21点游戏。到目前为止,除了这个小细节之外,一切都正常。基本上,开始游戏按钮只应在游戏之前和游戏之后可见。我没有问题,可以将其设置为隐藏,但是当我完成一局游戏时,它不起作用。
function startGame() { startGameButton.style.visibility = "hidden"; if (!player.playerName) { player.playerName = prompt("你叫什么名字?") player.chips = 100 playerEl.textContent = "玩家:" + player.playerName; } if (cards.length < 2) { let bet = prompt('你想下注多少(筹码:' + player.chips + ')') player.chips = player.chips - bet; chipsEl.textContent = "筹码:" + player.chips; } let firstCard = randomCard(); let secondCard = randomCard(); cards = [firstCard, secondCard]; let tableFirstCard = randomCard(); let tableSecondCard = randomCard(); tableCards = [tableFirstCard, tableSecondCard]; let message = ""; let hasBlackJack = false; let isAlive = true; sum = firstCard + secondCard; newCardButton.style.visibility = "visible"; startGameButton.textContent = "新游戏"; if (sum < 21) { message = "你想要抽一张新牌吗?"; } else if (sum === 21) { message = "你得到了21点!"; newCardButton.style.visibility = "hidden"; startGameButton.style.visibility = "visible"; player.chips = player.chips + bet * 2 hasBlackJack = true; } else { startGameButton.style.visibility = "visible"; isAlive = false; message = "运气不好!你已经离开游戏了!"; } messageEl.textContent = message; cardsEl.textContent = "牌:" + firstCard + " " + secondCard; tableCardsEl.textContent = "桌面牌:" + tableSecondCard + " " + tableSecondCard; sumEl.textContent = "总和:" + sum; console.log("21点:" + hasBlackJack); console.log("存活:" + isAlive); }
我什至尝试交换顺序,看看是否有任何区别,但是发生的一切都是:
if (sum < 21) { message = "你想要抽一张新牌吗?"; } else if (sum === 21) { message = "你得到了21点!"; newCardButton.style.visibility = "hidden"; startGameButton.style.visibility = "visible"; player.chips = player.chips + bet * 2 hasBlackJack = true; } else { startGameButton.style.visibility = "visible"; isAlive = false; message = "运气不好!你已经离开游戏了!"; }
if语句中的所有内容都在发生,除了可见性部分。
如果需要更多上下文,请参考代码链接:https://github.com/pedrosilva410/blackjack-game
我已经在你的Github上查看了整个代码,问题在于你的
startGame
函数只在游戏刚开始时调用(这是有道理的,显然)。在游戏进行中它什么也不做。你关于使“开始游戏”按钮可见的指示是正确的,只是把它们放错了位置。也把它们添加到你的
drawCard
函数中。