【发布时间】:2014-06-07 17:16:17
【问题描述】:
我对这个网站很陌生 xD
我正在尝试用 Java 创建一个纸牌游戏,但在创建带有纸牌图像的牌组时遇到了一些问题。我创建了 2 个类:Deck 和 Card。在 Card 类中,我输入以下代码
public static final int SPADE = 4;
public static final int HEART = 3;
public static final int CLUB = 2;
public static final int DIAMOND = 1;
private static final String[] Suit = { "*", "d", "c", "h", "s"};
private static final String[] Rank = { "*", "A", "2", "3", "4",
"5", "6", "7", "8", "9", "10", "J", "Q", "K", "A"};
private int cardValue;
public Card( int suit, int rank)
{
if ( rank == 1 )
cardValue = 14*10 + suit; // Give Ace the rank 14
else
cardValue = rank*10 + suit;
}
在 Deck 类中,我使用构造函数来创建一副 52 张卡片:
private Card[] deckOfCards;
public static final int NCARDS = 52;
public Deck( )
{
deckOfCards = new Card[NCARDS];
int i=0;
for ( int suit = Card.DIAMOND; suit <= Card.SPADE; suit++ ) {
for ( int rank = 1; rank <= 13; rank++,i++ ){
deckOfCards[i] = new Card(suit, rank);
}
}
}
我还创建了洗牌和发牌的方法,但我无法将每张牌的相关图像与 deckOfCards 相关联。有人可以帮我吗? :)
提前致谢!
【问题讨论】:
-
在您的对象中添加一个字符串变量,例如 strImage,其中存储图像名称,例如
queen_club.png,并且在调用图像时,只需从对象的变量中调用。 -
你能解释一下吗? :) 你的意思是在我的 Card 方法中创建一个新的字符串变量?以及如何将图像存储在字符串中?
-
添加一个属性来存储图片的名称,不能将图片存储为字符串。当您准备好添加图像时,只需从对象的属性中调用。请参阅下面的解决方案。