- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我正在尝试将计算机中的图像放入我的剪刀石头布游戏中,但我遇到了麻烦,而且它不起作用我不知道我哪里出了问题。而且,当我放入图像时,我必须从代码中删除警报才能使其正常工作,我将包含我的 JavaScript、HTML 以及我的 fiddle 。我遇到了很多麻烦,因为我真的陷入困境并且真的遇到了麻烦。
JavaScript
var differentOptions = ["rock", "paper", "scissors"];
var scores = [0, 0, 0, 0];
function Picks(id) {
var choice1 = id;
var imagePlayer = "<img='rps-paper" + "<img='rsp-rock" + "<img='rps-scissors" + choice1 + ".gif' />";
var choice2 = computerGenerate();
var imageComputer = "<img='rps-paper" + "<img='rsp-rock" + "<img='rps-scissors" + choice2 + ".gif' />";
alert ("You chose " + choice1 + ". The computer chose " + choice2 + "! " + compare(choice1, choice2));
document.getElementById('player').innerHTML = imagePlayer;
document.getElementById('computer').innerHTML = imageComputer;
displayScores();
}
function computerGenerate() {
var num = [Math.floor(Math.random() * 3)]
var computerChoice = differentOptions[num];
return computerChoice;
}
function compare(choice1, choice2) {
if (choice1 == choice2) {
scores[0]++;
scores[3]++;
return (" Draw You both picked the same! Try again!");
} else if (choice1 == "rock") {
if (choice2 == "scissors") {
scores[1]++;
scores[3]++;
return ("You Win!");
} else {
scores[2]++;
scores[3]++;
return ("You Lose!");
}
} else if (choice1 == "paper") {
if (choice2 == "rock") {
scores[1]++;
scores[3]++;
return ("You Win!");
} else if (choice2 == "scissors") {
scores[2]++;
scores[3]++;
return ("You Lose!");
}
} else if (choice1 == "scissors") {
if (choice2 == "rock") {
scores[2]++;
scores[3]++;
return ("You Lose!");
} else if (choice2 == "paper") {
scores[1]++;
scores[3]++;
return ("You Win!");
}
}
}
function displayScores() {
document.getElementById("wins").innerHTML = "Wins: " + scores[1];
document.getElementById("loss").innerHTML = "Losses: " + scores[2];
document.getElementById("ties").innerHTML = "Ties: " + scores[0];
document.getElementById("games").innerHTML = "Games Played: " + scores[3];
}
function resetScores() {
scores = [0, 0, 0, 0];
displayScores();
}
这是我的 HTML。
<div style="text-align: center;">
<div id="optionsPanel">
<h1 class="title">Let's Play Rock / Paper / Scissors!</h1>
<div id="computer"></div>
<br/>
<div id="player"></div>
<button id="rockButton" class="button" onclick='userPicks("rock")'>Rock</button>
<button id="paperButton" class="button" onclick='userPicks("paper")'>Paper</button>
<button id="scissorsButton" class="button" onclick='userPicks("scissors")'>Scissors</button </div>
<div id="scoresPanel">
<h2 class="title">Scores:</h2>
<div id="wins" class="scores">Wins:</div>
<div id="loss" class="scores">Losses:</div>
<div id="ties" class="scores">Ties:</div>
<div id="games" class="scores">Games Played:</div>
<h2 class="button" onclick='resetScores();'>Reset</h2>
</div>
这是我的 CSS。
#player, #computer {
border: 1px solid black;
height: 100px;
width: 150px;
margin: 10px;
}
最佳答案
这是我的版本
function userPicks(id) {
var choice1 = differentOptions[id];
var choice2 = computerGenerate();
var imagePlayer = "<img src='rps-"+ choice1 + ".gif' title='"+choice1+"' />";
var imageComputer = "<img src='rps-"+choice2 + ".gif' title='"+choice2+"' />";
document.getElementById("message").innerHTML="You chose " + choice1 + ". The computer chose " + choice2 + "! " + compare(choice1, choice2);
添加<div id="message"></div>
后到页面
我将 Picks 重命名为 userPics,并将执行方式从 userPicks("rock") 更改为 userPick(0)/(1) 和 (2),因此我们只需要一个数组
<小时/>这是一个扩展版本,您可以在其中决定关联数组中的图像。
var differentOptions = {
"rock": "rock.jpg",
"paper": "paper.jpg",
"scissors":"http://upload.wikimedia.org/wikipedia/commons/1/18/Skalm_2.JPG"
}
注意将 userPicks(1) 更改回 userPics("rock")
function userPicks(choice1) {
var choice2 = computerGenerate();
var imagePlayer = "<img width='90%' height='90%' src='"+ differentOptions[choice1] + "' title='"+choice1+"' />";
var imageComputer = "<img width='90%' height='90%' src='"+differentOptions[choice2] + "' title='"+choice2+"' />";
使用
function computerGenerate() {
var choices = ["rock","paper","scissors"]; // now you can add "spock" if you want
var num = [Math.floor(Math.random() * choices.length)]
return choices[num];
}
关于javascript - 剪刀石头布。图像不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26199852/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!