0

嗨,伙计们,我正在尝试制作纸牌游戏,但在将 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++;
      }
 } 
4

2 回答 2

1

可以使用ImageList,按照List cars的顺序在ImageList中添加Images

`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" };

比您可以使用卡片在 ImageList 中获取需要的索引。

您可以在http://msdn.microsoft.com/ru-ru/library/system.windows.forms.imagelist.aspx查看如何使用

于 2012-07-31T13:44:11.493 回答
0

似乎您的方法将从面向对象的角度受益。为什么不创建一个 PlayingCard 对象;一个具有“CardImage”属性的?这样,您无需维护两个单独的列表(一个卡片值,一个卡片图像),只需维护一个 PlayingCards 列表,您就可以访问 .CardImage 属性?

public class PlayingCard
{
    public enum Suit { Spades, Hearts, Diamonds, Clubs }
    public enum Rank {King =13, Queen = 12, Jack = 11, Seven = 7, Eight = 8, Nine = 9 }

    public Suit CardSuit;
    public Rank CardRank;

    public Bitmap CardImage;
}

然后在您的构造函数中解析 Suit 和 Rank 以确定图像。

将您的收藏创建为 List<PlayingCard>

于 2012-07-31T15:19:07.720 回答