C#在多个图片框中随机化多个图像
大家好,我正在尝试制作一个纸牌游戏,在32个图片框中分发32张纸牌图片时遇到了一些问题,但我无法编写如何按图片名称制作图片列表的代码。我已经尝试了我的代码使用按钮作为卡,并随机分发给玩家,这是一个成功,但当我试图这样做的情况下,图像我不能得到它的权利,我张贴我的两个代码在这里C#在多个图片框中随机化多个图像,c#,image,random,picturebox,C#,Image,Random,Picturebox,大家好,我正在尝试制作一个纸牌游戏,在32个图片框中分发32张纸牌图片时遇到了一些问题,但我无法编写如何按图片名称制作图片列表的代码。我已经尝试了我的代码使用按钮作为卡,并随机分发给玩家,这是一个成功,但当我试图这样做的情况下,图像我不能得到它的权利,我张贴我的两个代码在这里 class CardsDeck { public static Random r = new Random(); private static List<string> cards = new
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 "";
}
}
}
现在,在这种情况下,我试图用图像而不是字符串来完成它,但我无法正确地获得代码。在这方面有人能帮我吗
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++;
}
}
类卡
{
公共静态随机r=新随机();
PictureBox[]picBox=新PictureBox[32]
公共静态位图[]图片=新位图[32];
//我应该在这里写什么??
bool[]usedPictures=新bool[pictures.Length];
}
公共字符串接收卡()
{
int-ICount=0;
while(i计数<图片长度)
{
int trunt=random.Next(0,pictures.Length);
//确保您只使用可用图片
if(usedPictures[尝试]==false)
{
picBox[尝试]。图像=图片[iCount];
doorUsed[尝试]=真;
iCount++;
}
}
您可以使用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" };
`private static List cards=新列表{”♣ 国王“♣ 女王“♣ 杰克“♣", "♣ 7", "♣ 8", "♣ 9", "♣ 10",
"♦ 国王“♦ 女王“♦ 杰克“♦", "♦ 7", "♦ 8", "♦ 9", "♦ 10",
"♥ 国王“♥ 女王“♥ 杰克“♥", "♥ 7", "♥ 8", "♥ 9", "♥ 10",
"♠ 国王“♠ 女王“♠ 杰克“♠", "♠ 7", "♠ 8", "♠ 9", "♠ 10" };
然后您可以使用该卡在ImageList中获取所需索引
您可以看到如何使用at似乎您的方法将从面向对象的角度受益。为什么不创建一个PlayingCard对象;一个具有“CardImage”属性的对象?这样做,而不是维护两个单独的列表(一个卡值,一个卡图像),您只需维护一个播放卡列表,就可以访问.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;
}
然后在构造器中解析西装和等级以确定图像
将您的收藏创建为
列表是的,我想要这个,但我找不到任何这样的例子所有这些都有一张图片:(
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;
}