Conversation
…ce image animation
src/App.js
Outdated
| const { name, value } = event.target; | ||
| event.preventDefault(); | ||
| this.state.audio.play(); // start playing background music | ||
| this.state.audio.loop = true; |
There was a problem hiding this comment.
We never want to reassign state like so, but always use the setState function. By doing this here, you will run into trouble with missing rerenders and possibly out-of-sync state.
src/App.js
Outdated
| this.state.diceOrder === 1 | ||
| ? this.state.playerDiceRolls[0] * 10 + this.state.playerDiceRolls[1] | ||
| : this.state.playerDiceRolls[1] * 10 + this.state.playerDiceRolls[0]; | ||
| const newScores = this.state.userScores.slice(); //copy the array |
There was a problem hiding this comment.
We could just do this
| const newScores = this.state.userScores.slice(); //copy the array | |
| const newScores = [...this.state.userScores] |
src/App.js
Outdated
|
|
||
| resetGame = () => { | ||
| this.setState({ | ||
| // Reset the whole game to let user play again |
There was a problem hiding this comment.
I think ideally we define the defaultValues from 36 - 48 here. We could create a defaultValues object, and use it in line 36, and here.
There was a problem hiding this comment.
| // Reset the whole game to let user play again | |
| this.setState(defaultState) |
src/App.js
Outdated
|
|
||
| toggleMusic = (event) => { | ||
| event.preventDefault(); | ||
| let isMusicOn = this.state.isMusicOn; |
There was a problem hiding this comment.
I think no need to define variable for state, which is a variable already.
src/App.js
Outdated
| const gameStarted = this.state.gameStarted; | ||
| const userRollDice = this.state.userRollDice; | ||
| const userOrderDice = this.state.userOrderDice; | ||
| const playerDiceRolls = this.state.playerDiceRolls; | ||
| const userScores = this.state.userScores; |
There was a problem hiding this comment.
Same here, no need to create new variables. If you absolutely want to, use destructuring for nicer syntax!
| const gameStarted = this.state.gameStarted; | |
| const userRollDice = this.state.userRollDice; | |
| const userOrderDice = this.state.userOrderDice; | |
| const playerDiceRolls = this.state.playerDiceRolls; | |
| const userScores = this.state.userScores; | |
| const { gameStatrted, userRollDice, userOrderDice, playerDiceRolls, userScores } = this.state; |
src/App.js
Outdated
| this.state.currentPlayer, | ||
| this.state.numberOfRounds, | ||
| this.state.numberOfPlayers, | ||
| this.state.userOrderDice |
There was a problem hiding this comment.
since you defined userOrderDice right above, let's use that variable at least
| import five from "./assets/five.png"; | ||
| import six from "./assets/six.png"; | ||
|
|
||
| export const DiceImage = ({ roll }) => { |
src/DiceImage.js
Outdated
| switch (roll) { | ||
| case 1: | ||
| return <img className="dice-image" src={one} alt="1" />; | ||
| case 2: | ||
| return <img className="dice-image" src={two} alt="2" />; | ||
| case 3: | ||
| return <img className="dice-image" src={three} alt="3" />; | ||
| case 4: | ||
| return <img className="dice-image" src={four} alt="4" />; | ||
| case 5: | ||
| return <img className="dice-image" src={five} alt="5" />; | ||
| default: | ||
| return <img className="dice-image" src={six} alt="6" />; |
There was a problem hiding this comment.
We could improve this like so:
First name your images as numbers, instead of letters.
"./assets/1.png"; etc.
then like so:
| switch (roll) { | |
| case 1: | |
| return <img className="dice-image" src={one} alt="1" />; | |
| case 2: | |
| return <img className="dice-image" src={two} alt="2" />; | |
| case 3: | |
| return <img className="dice-image" src={three} alt="3" />; | |
| case 4: | |
| return <img className="dice-image" src={four} alt="4" />; | |
| case 5: | |
| return <img className="dice-image" src={five} alt="5" />; | |
| default: | |
| return <img className="dice-image" src={six} alt="6" />; | |
| return <img className="dice-image" src={`./assets/${roll}.png`} alt={roll} />; |
| if (userRollDice) { | ||
| return ( | ||
| <img | ||
| src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif" | ||
| border="0" | ||
| alt="animated-dice-image-0085" | ||
| className="animated-gif" | ||
| /> | ||
| ); | ||
| } else { | ||
| return <p>Click [Continue] button...</p>; | ||
| } |
There was a problem hiding this comment.
| if (userRollDice) { | |
| return ( | |
| <img | |
| src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif" | |
| border="0" | |
| alt="animated-dice-image-0085" | |
| className="animated-gif" | |
| /> | |
| ); | |
| } else { | |
| return <p>Click [Continue] button...</p>; | |
| } | |
| return userRollDice ? <img | |
| src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif" | |
| border="0" | |
| alt="animated-dice-image-0085" | |
| className="animated-gif" | |
| /> : <p>Click [Continue] button...</p> |
There was a problem hiding this comment.
or get rid of the else statement for better readability
| if (userRollDice) { | |
| return ( | |
| <img | |
| src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif" | |
| border="0" | |
| alt="animated-dice-image-0085" | |
| className="animated-gif" | |
| /> | |
| ); | |
| } else { | |
| return <p>Click [Continue] button...</p>; | |
| } | |
| if (userRollDice) return ( | |
| <img | |
| src="https://www.animatedimages.org/data/media/710/animated-dice-image-0085.gif" | |
| border="0" | |
| alt="animated-dice-image-0085" | |
| className="animated-gif" | |
| /> | |
| ); | |
| return <p>Click [Continue] button...</p>; |
src/PlayForm.js
Outdated
| isLastPlayer, | ||
| isMusicOn, | ||
| }) => { | ||
| const captionMusic = isMusicOn ? "Music OFF" : "Music ON"; |
There was a problem hiding this comment.
captionMusic sounds like an action, a function. musicCaption might be better
|
@yomansg please see these comments |
| <div className="Main-body height-fixed"> | ||
| {/* Constantly show the players' scores on a scoreboard*/} | ||
| {gameStarted && <h4>Leaderboard 🏆</h4>} | ||
| {gameStarted && <hr />} | ||
| {gameStarted && <LeaderBoard userScores={userScores} />} | ||
| </div> |
There was a problem hiding this comment.
| <div className="Main-body height-fixed"> | |
| {/* Constantly show the players' scores on a scoreboard*/} | |
| {gameStarted && <h4>Leaderboard 🏆</h4>} | |
| {gameStarted && <hr />} | |
| {gameStarted && <LeaderBoard userScores={userScores} />} | |
| </div> | |
| { gameStarted && (<div className="Main-body height-fixed"> | |
| {/* Constantly show the players' scores on a scoreboard*/} | |
| <h4>Leaderboard 🏆</h4> | |
| <hr /> | |
| <LeaderBoard userScores={userScores} /> | |
| </div>)} | |
src/App.js
Outdated
| this.state.audio.play(); // start playing background music | ||
| this.state.audio.loop = true; |
There was a problem hiding this comment.
| this.state.audio.play(); // start playing background music | |
| this.state.audio.loop = true; | |
| const audio = this.state.audio; | |
| audio.play(); | |
| audio.loop = true; |
| [name]: value, | ||
| userScores: Array(this.state.numberOfPlayers).fill(0), | ||
| gameStarted: true, | ||
| isMusicOn: true, |
There was a problem hiding this comment.
| isMusicOn: true, | |
| isMusicOn: true, | |
| audio: { | |
| ...this.state.audio, | |
| loop: true | |
| } |
| label: "Game music", | ||
| }, | ||
| ]; | ||
|
|
There was a problem hiding this comment.
| const defaultState = { | |
| gameStarted: false, | |
| numberOfPlayers: 2, // form input state | |
| numberOfRounds: 3, | |
| userRollDice: false, | |
| userOrderDice: false, | |
| userScores: [], | |
| currentPlayer: 0, | |
| currentRound: 0, | |
| playerDiceRolls: [0, 0], | |
| currentPlayerScore: 0, | |
| diceOrder: 1, | |
| audio: new Audio(audioClips[2].sound), // set background music | |
| isMusicOn: false, // background music is initially OFF | |
| } |
src/App.js
Outdated
| this.state = { | ||
| gameStarted: false, | ||
| numberOfPlayers: 2, // form input state | ||
| numberOfRounds: 3, | ||
| userRollDice: false, | ||
| userOrderDice: false, | ||
| userScores: [], | ||
| currentPlayer: 0, | ||
| currentRound: 0, | ||
| playerDiceRolls: [0, 0], | ||
| currentPlayerScore: 0, | ||
| diceOrder: 1, | ||
| audio: new Audio(audioClips[2].sound), // set background music | ||
| isMusicOn: false, // background music is initially OFF | ||
| }; |
There was a problem hiding this comment.
| this.state = { | |
| gameStarted: false, | |
| numberOfPlayers: 2, // form input state | |
| numberOfRounds: 3, | |
| userRollDice: false, | |
| userOrderDice: false, | |
| userScores: [], | |
| currentPlayer: 0, | |
| currentRound: 0, | |
| playerDiceRolls: [0, 0], | |
| currentPlayerScore: 0, | |
| diceOrder: 1, | |
| audio: new Audio(audioClips[2].sound), // set background music | |
| isMusicOn: false, // background music is initially OFF | |
| }; | |
| this.state = defaultState; |
src/App.js
Outdated
| if (isMusicOn) { | ||
| this.state.audio.pause(); // Pause music if it is playing | ||
| } else { | ||
| this.state.audio.play(); // Play music if it is paused | ||
| } |
There was a problem hiding this comment.
| if (isMusicOn) { | |
| this.state.audio.pause(); // Pause music if it is playing | |
| } else { | |
| this.state.audio.play(); // Play music if it is paused | |
| } | |
| isMusicOn ? this.state.audio.pause() : this.state.audio.play() |
No description provided.