抱歉,这听起来完全是愚蠢的,但是在编码方面我真是笨拙!我正在尝试使用一副纸牌制作更高或更低的猜谜游戏(即玩家猜测下一张纸牌是否高于或低于所示的纸牌),但我真的不明白如何给每张纸牌一张值。
例如,我想给Ace卡一个值“1”,这样人们就可以猜测下一张卡是高于还是低于1。
我知道涉及数组和类,但是我似乎找不到用简单的英语解释它的教程。到目前为止,这是我的代码(是的,我知道这很可怕!)
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled</title>
<script language="javascript" type="text/javascript">
var ask=window.confirm("Do you understand the rules of Higher or Lower?")
if (ask)
window.alert("Lets go!")
else
window.alert("Guess if the next card shown will be higher or lower than the current card.")
function rollDice()
{
var diceArray = ["card0.jpg","card1.jpg","card2.jpg","card3.jpg","card4.jpg","card5.jpg"];
var pickCard = diceArray[Math.floor(Math.random() * diceArray.length)];
document.getElementById("cards").src = pickCard;
}
</script>
<link rel="stylesheet" href="teststyle.css" type="text/css">
</head>
<body style="text-align: center;">
<br>
<big style="font-weight: bold; font-family: Helvetica,Arial,sans-serif;"><big><big>HIGHER
OR LOWER?</big></big></big><br>
<h3>Use your knowledge of probability to guess if the next card will be
higher or lower!</h3>
<br>
<br>
<img src="card5.jpg" onload="pickCard()" id="cards"><br>
<br>
<span
style="font-weight: bold; font-family: Helvetica,Arial,sans-serif;">VALUES</span><span
style="font-family: Helvetica,Arial,sans-serif;">: ACE, TWO, THREE,
FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING</span><br>
<br>
<br>
</body>
</html>
最佳答案
我认为将每张卡表示为一个对象可能会很有用:
{
imagePath: "card0.jpg",
value: 1,
name: "Ace"
}
您可以将它们放在数组中:
var diceArray = [
{
imagePath: "card0.jpg",
value: 1,
name: "Ace"
},
{
imagePath: "card1.jpg",
value: 2,
name: "Two"
},
// and so on...
];
var pickCard = diceArray[Math.floor(Math.random() * diceArray.length)];
document.getElementById("cards").src = pickCard.imagePath;
var pickedValue = pickCard.value;
可以看到,存储在
pickCard
中的结果现在是一个具有name
,value
和imagePath
属性的对象。您以后可以根据需要添加其他属性(suit
等)。关于javascript - 制作简单的纸牌游戏时如何给纸牌赋予数值(JavaScript),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19935941/