C#在多个图片框中随机化多个图像

时间:2012-07-31 12:35:02

标签: c# image random picturebox

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

2 个答案:

答案 0 :(得分:1)

您可以使用ImageList,根据列车车辆的顺序在ImageList中添加图像

`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

了解如何使用

答案 1 :(得分:0)

似乎您的方法将从面向对象的角度受益。为什么不创建一个PlayingCard对象;一个具有'CardImage'属性的?这样,您只需维护一个PlayCards列表,然后可以访问.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以确定图像。

创建您的收藏集     列表与LT;游戏牌&GT;