【问题标题】:dice game is calculating the correct score骰子游戏正在计算正确的分数
【发布时间】:2018-05-12 22:25:51
【问题描述】:

在 udemy 课程中使用 javascript。我做了一个骰子游戏。游戏有效,但分数不正确。分数是骰子数字的增量。例如第一个是 3。所以分数是 3。但它不是我掷三分,分数是 9。

var scores,roundScore,activePlayer,dice;
 scores       = [0,0];
 roundScore   = 0;
 activePlayer = 0;


 
  document.getElementById('score-0').textContent ='0';
  document.getElementById('score-1').textContent ='0';
  document.getElementById('current-0').textContent ='0';
  document.getElementById('current-1').textContent ='0';
  document.querySelector('.dice').style.display = 'none';



 document.querySelector('.btn-roll').addEventListener('click', function() {
 	var dice = (Math.random() * Math.floor(6)) + 1;
 	var diceDom  = document.querySelector('.dice');

 	diceDom.style.display = 'block';
 	diceDom.src = 'dice-' + dice + '.png';

 	if(dice !== 1){
 		//add score
 		roundScore += dice;
 		document.querySelector('#current-' + activePlayer).textContent = roundScore;
 	}else{ 
 		//nextplayer
 				activePlayer === 0 ? activePlayer = 1 : activePlayer = 0;
 				roundScore = 0;

 				document.getElementById('current-0').textContent = '0'; 
 				document.getElementById('current-1').textContent = '0';

 				document.querySelector('.player-0-panel').classList.toggle('active');
 				document.querySelector('.player-1-panel').classList.toggle('active');
 				document.querySelector('.dice').style.display = 'none';

 				}
 	



 });
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    
}

.clearfix::after {
    content: "";
    display: table;
    clear: both;
}

body {
    background-image: linear-gradient(rgba(62, 20, 20, 0.4), rgba(62, 20, 20, 0.4)), url(back.jpg);
    background-size: cover;
    background-position: center;
    font-family: Lato;
    font-weight: 300;
    position: relative;
    height: 100vh;
    color: #555;
}

.wrapper {
    width: 1000px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    box-shadow: 0px 10px 50px rgba(0, 0, 0, 0.3);
    overflow: hidden;
}

.player-0-panel,
.player-1-panel {
    width: 50%;
    float: left;
    height: 600px;
    padding: 100px;
}



/**********************************************
*** PLAYERS
**********************************************/

.player-name {
    font-size: 40px;
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 2px;
    font-weight: 100;
    margin-top: 20px;
    margin-bottom: 10px;
    position: relative;
}

.player-score {
    text-align: center;
    font-size: 80px;
    font-weight: 100;
    color: #EB4D4D;
    margin-bottom: 130px;
}

.active { background-color: #f7f7f7; }
.active .player-name { font-weight: 300; }

.active .player-name::after {
    content: "\2022";
    font-size: 47px;
    position: absolute;
    color: #EB4D4D;
    top: -7px;
    right: 10px;
    
}

.player-current-box {
    background-color: #EB4D4D;
    color: #fff;
    width: 40%;
    margin: 0 auto;
    padding: 12px;
    text-align: center;
}

.player-current-label {
    text-transform: uppercase;
    margin-bottom: 10px;
    font-size: 12px;
    color: #222;
}

.player-current-score {
    font-size: 30px;
}

button {
    position: absolute;
    width: 200px;
    left: 50%;
    transform: translateX(-50%);
    color: #555;
    background: none;
    border: none;
    font-family: Lato;
    font-size: 20px;
    text-transform: uppercase;
    cursor: pointer;
    font-weight: 300;
    transition: background-color 0.3s, color 0.3s;
}

button:hover { font-weight: 600; }
button:hover i { margin-right: 20px; }

button:focus {
    outline: none;
}

i {
    color: #EB4D4D;
    display: inline-block;
    margin-right: 15px;
    font-size: 32px;
    line-height: 1;
    vertical-align: text-top;
    margin-top: -4px;
    transition: margin 0.3s;
}

.btn-new { top: 45px;}
.btn-roll { top: 403px;}
.btn-hold { top: 467px;}

.dice {
    position: absolute;
    left: 50%;
    top: 178px;
    transform: translateX(-50%);
    height: 100px;
    box-shadow: 0px 10px 60px rgba(0, 0, 0, 0.10);
}

.winner { background-color: #f7f7f7; }
.winner .player-name { font-weight: 300; color: #EB4D4D; }
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        
        <link href="https://fonts.googleapis.com/css?family=Lato:100,300,600" rel="stylesheet" type="text/css">
        <link href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" rel="stylesheet" type="text/css">
        <link type="text/css" rel="stylesheet" href="style.css">

        
        <title>Pig Game</title>
    </head>

    <body>
        <div class="wrapper clearfix">
            <div class="player-0-panel active">
                <div class="player-name" id="name-0">Player 1</div>
                <div class="player-score" id="score-0">43</div>
                <div class="player-current-box">
                    <div class="player-current-label">Current</div>
                    <div class="player-current-score" id="current-0">11</div>
                </div>
            </div>
            
            <div class="player-1-panel">
                <div class="player-name" id="name-1">Player 2</div>
                <div class="player-score" id="score-1">72</div>
                <div class="player-current-box">
                    <div class="player-current-label">Current</div>
                    <div class="player-current-score" id="current-1">0</div>
                </div>
            </div>
            
            <button class="btn-new"><i class="ion-ios-plus-outline"></i>New game</button>
            <button class="btn-roll"><i class="ion-ios-loop"></i>Roll dice</button>
            <button class="btn-hold"><i class="ion-ios-download-outline"></i>Hold</button>
            
            <img src="dice-5.png" alt="Dice" class="dice">
        </div>
        
        <script src="app.js"></script>
    </body>




     <script type="text/javascript" src="app.js"></script> -->
</html>

score 0i rolled a 4 but the score is 10 when it should be what the dice rolled

【问题讨论】:

  • Math.floor(6) 只是6,你可能想把整个表达都打底
  • var dice = Math.floor(Math.random() * 6) + 1;

标签: javascript


【解决方案1】:

应该是 var dice = Math.floor(Math.random()*6) + 1; 问题是您添加了 2 次相同的脚本,这意味着您添加了 2 次点击事件,这反过来意味着您将骰子值添加到分数中两次。

删除最后一个脚本标记,即 index.html 中的第 47 行。就在html标签上方。

【讨论】:

  • 这就是我原本拥有的。这就是教程中的情况,但结果是相同的错误分数。我尝试了几个声明,但都是一样的。比如 var dice = Math.floor(Math.random()*6) + 1; vvar dice = Math.floor(Math.random() * Math.floor(6) + 1;
【解决方案2】:

问题是您添加了 2 次相同的脚本,这意味着您添加了 2 次点击事件,这反过来意味着您将骰子值添加到分数中两次。

删除最后一个脚本标记,即 index.html 中的第 47 行。就在html标签上方。

【讨论】:

  • 天才!太感谢了。我确定错误出现在 js 文件中,我从未仔细检查过 html 代码。
  • 我支持你,但我的次数少于 15 次
  • 酷,没明白吗?您可以将我的第一条评论作为解决方案,因为在那里添加了相同的解决方案。
  • 我如何选择您的评论作为解决方案?
猜你喜欢
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 2016-08-04
  • 2023-04-10
  • 2023-01-26
  • 2012-02-29
  • 2016-12-26
相关资源
最近更新 更多