嗨,伙计们,我正在尝试制作纸牌游戏,但在将 32 张纸牌图像分布在 32 个图片框中时遇到了一些问题,但我无法编写有关如何按名称制作图像列表的代码。我已经尝试将我的代码使用按钮作为卡片并将它们随机分发给玩家,这是成功的,但是当我尝试这样做以防图像出现时,我无法正确地在这里发布我的两个代码。
class CardsDeck
{
public static Random r = new Random();
private static List<string> cards = new List<string>{ "♣ King", "♣ Queen", "♣ Jack", " ♣", "♣ 7", "♣ 8", "♣ 9", "♣ 10",
"♦ King", "♦ Queen", "♦ Jack", " ♦", "♦ 7", "♦ 8", "♦ 9", "♦ 10",
"♥ King", "♥ Queen", "♥ Jack", " ♥", "♥ 7", "♥ 8", "♥ 9", "♥ 10",
"♠ King", "♠ Queen", "♠ Jack", " ♠", "♠ 7", "♠ 8", "♠ 9", "♠ 10" };
public string ReceiveCards()
{
if (cards.Count > 0)
{
int index = r.Next(cards.Count);
var card = cards[index];
cards.RemoveAt(index);
return card;
}
else
{
return "";
}
}
}
在主窗体中
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
CardsDeck cd = new CardsDeck();
btnA1.Text = cd.ReceiveCards();
btnA2.Text = cd.ReceiveCards();
btnA3.Text = cd.ReceiveCards();
btnA4.Text = cd.ReceiveCards();
btnB1.Text = cd.ReceiveCards();
btnB2.Text = cd.ReceiveCards();
btnB3.Text = cd.ReceiveCards();
btnB4.Text = cd.ReceiveCards();
btnC1.Text = cd.ReceiveCards();
btnC2.Text = cd.ReceiveCards();
btnC3.Text = cd.ReceiveCards();
btnC4.Text = cd.ReceiveCards();
btnD1.Text = cd.ReceiveCards();
btnD2.Text = cd.ReceiveCards();
btnD3.Text = cd.ReceiveCards();
btnD4.Text = cd.ReceiveCards();
}
private void btnTrump_Click(object sender, EventArgs e)
{
CardsDeck cd = new CardsDeck();
btnA5.Text = cd.ReceiveCards();
btnA6.Text = cd.ReceiveCards();
btnA7.Text = cd.ReceiveCards();
btnA8.Text = cd.ReceiveCards();
btnB5.Text = cd.ReceiveCards();
btnB6.Text = cd.ReceiveCards();
btnB7.Text = cd.ReceiveCards();
btnB8.Text = cd.ReceiveCards();
btnC5.Text = cd.ReceiveCards();
btnC6.Text = cd.ReceiveCards();
btnC7.Text = cd.ReceiveCards();
btnC8.Text = cd.ReceiveCards();
btnD5.Text = cd.ReceiveCards();
btnD6.Text = cd.ReceiveCards();
btnD7.Text = cd.ReceiveCards();
btnD8.Text = cd.ReceiveCards();
}
}
现在在这种情况下,我试图用图像而不是用字符串来做这件事,但我不能让我的代码正确。谁能在这方面帮助我
class Card
{
public static Random r = new Random();
PictureBox[] picBox = new PictureBox[32]
public static Bitmap[] pictures = new Bitmap[32];
// What should i write here??
bool[] usedPictures = new bool[pictures.Length];
}
public string ReceiveCards()
{
int ICount = 0;
while (iCount < pictures.Length)
{
int attempt = random.Next(0, pictures.Length);
//Ensures you will only use an available picture
if (usedPictures[attempt] == false)
{
picBox[attempt].Image= pictures[iCount];
doorUsed[attempt] = true;
iCount++;
}
}