简体   繁体   English

JS石头剪刀布游戏,animation问题

[英]JS Rock paper scissors game, animation issue

I am very new to coding.我对编码很陌生。 I followed a tutorial by dev Ed.我遵循了 dev Ed 的教程。

When I click on the button 'Lets Play' It should fade out and show the game.当我单击“让我们玩”按钮时,它应该淡出并显示游戏。 It worked at first but at some point it stopped.起初它起作用,但在某些时候它停止了。

There may be more errors than this but I am not able to test it until I can get past this first one.可能有比这更多的错误,但我无法测试它,直到我能通过第一个。 I apologise if it is something simple and I am very new to this.如果这很简单,我很抱歉,我对此很陌生。 Is anyone able to help me out with this?有人能帮我解决这个问题吗?

 const game = () => { let pScore = 0; //Player score let cScore = 0; //Computer score } //Start the game const startGame = () => { const playBtn = document.querySelector('.intro button'); // Lets play button const introScreen = document.querySelector('.intro'); //This is the main screen of the game that will fade when the button is pressed. const match = document.querySelector('.match'); playBtn.addEventListener('click', () => { introScreen.classList.add('fadeOut'); match.classList.add('fadeIn'); }); }; //play match const playMatch = () => { const options = document.querySelectorAll('.options button'); //Selects all the options from the buttons const playerHand = document.querySelector('.player-hand'); const computerHand = document.querySelector('.computer-hand'); const hands = document.querySelectorAll('.hands img'); hands.forEach(hand => { hand.addEventListener('animationend', function() { this.style.animation = ''; }); }) //Computer Options const computerOptions = ['rock', 'paper', 'scissors']; options.forEach(option => { option.addEventListener('click', function() { //computer choice const computerNumber = math.floor(Math.random() * 3); const computerChoice = computerOptions[computerNumber]; //Here is the where we call compare hands everytime the user preses one of the buttons setTimeout(() => { compareHands(this.textContent, computerChoice); //Update Images playerHand.src = `./assets/${this.textContent}.png`; computerHand.src = `./assets/${computerChoice}.png`; }, 2000); //Animation playerHand.style.animation = "shakePlayer 2s ease"; computerHand.style.animation = "shakeComputer 2s ease"; }); }); }; const updateScore = () => { const playerScore = document.querySelector('.player-score p'); const computerScore = document.querySelector('.computer-score p'); playerScore.textContent = pScore; computerScore.textContent = cScore; } //The function below is getting the computers choice from above const compareHands = (playerChoice, computerChoice) => { //Update Text const winner = document.querySelector('.winner'); //Checking for a tie if (playerChoice === computerChoice) { winner.textContent = 'It is a tie'; return; //If it is a tie, this will stop the rest of the code from running. and will end the function. } } //Check for Rock if (playerChoice === 'rock') { if (computerChoice === 'scissors') { winner.textContent = 'Player Wins'; pScore++; updateScore(); return; } else { winner.textContent = 'Computer Wins'; cScore++; updateScore(); return; } } //Check for paper if (playerChoice === 'paper') { if (computerChoice === 'scissors') { winner.textContent = 'Computer Wins'; cScore++; updateScore(); return; } else { winner.textContent = 'Player Wins'; pScore++; updateScore(); } } //Check for Scissors if (playerChoice === 'scissors') { if (computerChoice === 'rock') { winner.textContent = 'Computer Wins'; cScore++; return; } else { winner.textContent = 'Player Wins'; pScore++; updateScore(); return; } //Is call all the the inner functions. startGame(); playMatch(); }; //start the game function game();
 * { margin: 0; padding: 0; box-sizing: border-box; } section { height: 100vh; background-color: rgb(39, 41, 68); font-family: sans-serif; }.score { color: rgb(216, 214, 214); height: 20vh; display: flex; justify-content: space-around; align-items: center; }.score h2 { font-size: 30px; }.score p { text-align: center; padding: 10px; font-size: 25px; }.intro { color: rgb(216, 214, 214); height: 50vh; display: flex; flex-direction: column; align-items: center; justify-content: space-around; transition: opacity 0.5s ease; }.intro h1 { font-size: 50px; }.intro button, .match button /*This is styling the buttons */ { width: 150px; height: 50px; background: none; border: none; color: rgb(216, 214, 214); font-size: 20px; background: rgb(45, 117, 96); border-radius: 3px; cursor: pointer; }.match { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); transition: opacity 0.5s ease 0.5; }.winner { color: rgb(216, 214, 214); text-align: center; font-size: 50px; }.hands, .options /* This is styling the hands section*/ { display: flex; justify-content: space-around; align-items: center; }.player-hand { transform: rotateY(180deg); } div.fadeOut /* This will make the fade out */ { opacity: 0; pointer-events: none; } div.fadeIn { opacity: 1; pointer-events: all; } @keyframes shakePlayer { 0% { transform: rotateY(180deg) translateY(0px); } 15% { transform: rotateY(180deg) translateY(-50px); } 25% { transform: rotateY(180deg) translateY(0px); } 35% { transform: rotateY(180deg) translateY(-50px); } 50% { transform: rotateY(180deg) translateY(0px); } 65% { transform: rotateY(180deg) translateY(-50px); } 75% { transform: rotateY(180deg) translateY(0px); } 85% { transform: rotateY(180deg) translateY(-50px); } 100% { transform: rotateY(180deg) translateY(0px); } } @keyframes shakeComputer { 0% { transform: translateY(0px); } 15% { transform: translateY(-50px); } 25% { transform: translateY(0px); } 35% { transform: translateY(-50px); } 50% { transform: translateY(0px); } 65% { transform: translateY(-50px); } 75% { transform: translateY(0px); } 85% { transform: translateY(-50px); } 100% { transform: translateY(0px); } }
 <section class="game"> <.-- This is the score section--> <div class="score"> <div class="player-score"> <.-- This will show the player score--> <h2>Player</h2> <p>0</p> </div> <div class="computer-score"> <.--This will show the computers score--> <h2>Computer</h2> <p>0</p> </div> </div> <div class="intro"> <.-- this is the main part of the gamne--> <h1>Rock Paper and Scissors</h1> <button>Let's play</button> </div> <div class="match fadeOut"> <h2 class="winner">Choose an option</h2> <div class="hands"> <img class="player-hand" src="./assets/rock.png" alt="rock"> <!-- This is the players hand that is on the left hand side. --> <img class="computer-hand" src="./assets/rock.png" alt="rock"> <!--This is the computers hand --> </div> <!-- This will show the hands--> <div class="options"> <!--This will be where the player can select their options--> <button class="rock">rock</button> <button class="paper">paper</button> <button class="scissors">scissors</button> </div> <!--This will be where the player can select their options--> </div> </section>

It looks like there's a mistake in compareHands with closing the curly brackets too early.看起来在compareHands中过早关闭大括号是错误的。 The startGame() and playMatch() are being called in that last if statement if you follow the brackets, so the event listener in startGame() is not being added to the button.如果您遵循括号,则在最后一个if语句中调用startGame()playMatch() ,因此startGame()中的事件侦听器不会添加到按钮中。

Fix the brackets issue in compareHands (look at line 66 and see the brackets are closing before it gets to the if statements) and move startGame() and playMatch() outside of that function, like game() , so that they are called and the event listener is added to the button.修复compareHands中的括号问题(查看第 66 行,看到括号在到达if语句之前关闭)并将startGame()playMatch()移到 function 之外,如game() ,以便调用它们事件侦听器被添加到按钮。

https://jsfiddle.net/53Lsbyxu/20/ https://jsfiddle.net/53Lsbyxu/20/

 let scores = { player: 0, computer: 0 }; let enemies = { rock: "scissors", paper: "rock", scissors: "paper" }; const playBtn = document.querySelector(".intro button"); const introScreen = document.querySelector(".intro"); const match = document.querySelector(".match"); const winner = document.querySelector(".winner"); const options = document.querySelectorAll(".options"); const playerHand = document.querySelector(".player-hand"); const computerHand = document.querySelector(".computer-hand"); const hands = document.querySelectorAll(".hands img"); const playerScore = document.querySelector(".player-score p"); const computerScore = document.querySelector(".computer-score p"); const startGame = () => { playBtn.onclick = () => { introScreen.classList.add("fadeOut"); match.classList.add("fadeIn"); }; }; const playMatch = () => { hands.forEach((hand) => (hand.onanimationend = (e) => (e.target.style.animation = ""))); options.forEach((option) => { option.onclick = (e) => { const computerChoice = Object.keys(enemies)[Math.floor(Math.random() * 3)]; setTimeout(() => { compareHands(e.target.textContent, computerChoice); playerHand.src = `./assets/${e.target.textContent}.png`; computerHand.src = `./assets/${computerChoice}.png`; }, 2000); playerHand.style.animation = "shakePlayer 2s ease"; computerHand.style.animation = "shakeComputer 2s ease"; }; }); }; const updateMatch = () => { playerScore.textContent = scores.player; computerScore.textContent = scores.computer; }; const compareHands = (playerChoice, computerChoice) => { if (playerChoice === computerChoice) return (winner.textContent = "It is a tie"); if (enemies[playerChoice] === computerChoice) { scores.computer++; winner.textContent = "Computer Wins"; } else { scores.player++; winner.textContent = "Player Wins"; } updateMatch(); }; startGame(); playMatch();
 * { margin: 0; padding: 0; box-sizing: border-box; } section { height: 100vh; background-color: rgb(39, 41, 68); font-family: sans-serif; }.score { color: rgb(216, 214, 214); height: 20vh; display: flex; justify-content: space-around; align-items: center; }.score h2 { font-size: 30px; }.score p { text-align: center; padding: 10px; font-size: 25px; }.intro { color: rgb(216, 214, 214); height: 50vh; display: flex; flex-direction: column; align-items: center; justify-content: space-around; transition: opacity 0.5s ease; }.intro h1 { font-size: 50px; }.intro button, .match button /*This is styling the buttons */ { width: 150px; height: 50px; background: none; border: none; color: rgb(216, 214, 214); font-size: 20px; background: rgb(45, 117, 96); border-radius: 3px; cursor: pointer; }.match { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); transition: opacity 0.5s ease 0.5; }.winner { color: rgb(216, 214, 214); text-align: center; font-size: 50px; }.hands, .options /* This is styling the hands section*/ { display: flex; justify-content: space-around; align-items: center; }.player-hand { transform: rotateY(180deg); } div.fadeOut /* This will make the fade out */ { opacity: 0; pointer-events: none; } div.fadeIn { opacity: 1; pointer-events: all; } @keyframes shakePlayer { 0% { transform: rotateY(180deg) translateY(0px); } 15% { transform: rotateY(180deg) translateY(-50px); } 25% { transform: rotateY(180deg) translateY(0px); } 35% { transform: rotateY(180deg) translateY(-50px); } 50% { transform: rotateY(180deg) translateY(0px); } 65% { transform: rotateY(180deg) translateY(-50px); } 75% { transform: rotateY(180deg) translateY(0px); } 85% { transform: rotateY(180deg) translateY(-50px); } 100% { transform: rotateY(180deg) translateY(0px); } } @keyframes shakeComputer { 0% { transform: translateY(0px); } 15% { transform: translateY(-50px); } 25% { transform: translateY(0px); } 35% { transform: translateY(-50px); } 50% { transform: translateY(0px); } 65% { transform: translateY(-50px); } 75% { transform: translateY(0px); } 85% { transform: translateY(-50px); } 100% { transform: translateY(0px); } }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <link rel="stylesheet" href="./style.css" /> <title>Rock Paper and Scissors</title> </head> <body> <section class="game"> <.-- This is the score section--> <div class="score"> <div class="player-score"> <.-- This will show the player score--> <h2>Player</h2> <p>0</p> </div> <div class="computer-score"> <.--This will show the computers score--> <h2>Computer</h2> <p>0</p> </div> </div> <div class="intro"> <.-- this is the main part of the gamne--> <h1>Rock Paper and Scissors</h1> <button>Let's play</button> </div> <div class="match fadeOut"> <h2 class="winner">Choose an option</h2> <div class="hands"> <img class="player-hand" src="./assets/rock.png" alt="rock"> <!-- This is the players hand that is on the left hand side. --> <img class="computer-hand" src="./assets/rock.png" alt="rock"> <!--This is the computers hand --> </div> <!-- This will show the hands--> <div class="options"> <!--This will be where the player can select their options--> <button class="rock">rock</button> <button class="paper">paper</button> <button class="scissors">scissors</button> </div> <!--This will be where the player can select their options--> </div> </section> <script src="./main.js"></script> </body> </html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM