我一直在做剪刀石头布游戏,我希望图像在用户做出选择时显示出来。例如,如果我选择纸张,而计算机选择岩石,则我希望岩石图像与“ lose”语句一起显示
我尝试将图像放在文件夹之外,以查看是否可行,但没有成功。
的HTML
<div class="main-container">
<div class="score">
<p>You:0</p>
<p>Computer:0</p>
</div>
<div class="user-choices">
<img id='rock'class='choice' src="rock.png">
<img id='paper'class='choice' src="paper.png">
<img id='scissors'class='choice' src="scissors.png">
</div>
<div class='result'>
<div class="cpu-result"></div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
的CSS
<style>
.result img {
height: 10rem;
}
.result{
text-align: center;
display: none;
}
</style>
Run Code Online (Sandbox Code Playgroud)
Java脚本
<script>
function cpuChoice() {
const rand = Math.random()
if (rand > .34) {
return 'paper'
} else if (rand <= .67) {
return 'rock'
} else {
return 'scissors'
}
}
function showWinner(winner,computerPick) {
if …Run Code Online (Sandbox Code Playgroud)