简体   繁体   English

C#随机图像在图片框中并分配值

[英]C# random image in picturebox and assign value

I'm creating a card game using C#. 我正在使用C#创建一个纸牌游戏。 I want to assign a value to my card example: Ace(image) = 1; 我想为我的卡片例子分配一个值:Ace(image)= 1; and I want to random it. 而我想随便它。 Here's my code: 这是我的代码:

 private void button1_Click(object sender, EventArgs e)
        {
            Random cards = new Random();
            card = cards.Next(0, 9);
            switch (card)
            {
                case 0:
                    pictureBox1.Image = Properties.Resources.king_d;
                    pictureBox2.Image = Properties.Resources.jack_s;

                    break;

                case 1:
                    pictureBox1.Image = Properties.Resources.ace_c;
                    pictureBox2.Image = Properties.Resources.ten_d;

                    break;
            }
        }
    }

new the random out of the method. 新的随机出方法。 you can take it from a singleton class ( read this ) or for simplicity if you are in windows app make it static like this: 你可以从一个单独的类中读取它( 阅读本文 )或者为了简单起见,如果你在Windows应用程序中使它像这样静态:

static Random cards = new Random();
private void button1_Click(object sender, EventArgs e)

    {

        card = cards.Next(0, 9);
        switch (card)
        {
            case 0:
                pictureBox1.Image = Properties.Resources.king_d;
                pictureBox2.Image = Properties.Resources.jack_s;

                break;

            case 1:
                pictureBox1.Image = Properties.Resources.ace_c;
                pictureBox2.Image = Properties.Resources.ten_d;

                break;
        }
    }
}

Update The best way to have a card that contains a value, picture, etc. is to have a new class for that. 更新使用包含值,图片等的卡片的最佳方法是为其创建一个新类。 Since PictureBox already has most properties and behaviors that you need, I recommend using it. 由于PictureBox已经拥有您需要的大多数属性和行为,我建议使用它。

The code has to be something like this: 代码必须是这样的:

   Public Class MyCard:PictureBox
    {
      public int GamePoint {get;set;}
    }

Then instead of using PictureBox in your code, use this. 然后使用它来代替在代码中使用PictureBox。

To be honest I like to encapsulate the code a bit more so I prefer this: 说实话,我喜欢将代码封装得更多,所以我更喜欢这个:

   Public Class MyCard:PictureBox
    {
      public CardType CardType {set;get;}
      public int GamePoint {get{ return (int)this.CardType;  }}
      public MyCard(CardType _cardType)
      {
       CardType = _cardType;
        }
    }

    enum CardType
    { Ace=1,
    King=2,
    ...
    }

Although I don't see an actual question in your question, I think you want to do this in a simpler way. 虽然我在你的问题中没有看到实际问题,但我认为你想以更简单的方式做到这一点。

Well first of all, don't create a Random every time the method is called, make it a class-level variable and initialize it: 首先,每次调用方法时都不要创建Random ,使其成为类级变量并初始化它:

private static Random cards = new Random();

Currently, you're using a switch to decide what to show in the two picture boxes. 目前,您正在使用switch来决定在两个图片框中显示的内容。 If the random number is 0, put these two cards, if the number is 1, put those two cards... This means that each number from 0 to 9 corresponds to two Bitmap s. 如果随机数为0,则将这两个卡放入,如果数字为1,则将这两个卡放入...这意味着0到9之间的每个数字对应于两个Bitmap

You can use a dictionary to map 0 to 9 to Tuple<Bitmap, Bitmap> , but I think it's better to use an array. 您可以使用字典将0到9映射到Tuple<Bitmap, Bitmap> ,但我认为最好使用数组。

What you basically need to do is to declare an array that stores those Tuple<Bitmap, Bitmap> . 你基本上需要做的是声明一个存储那些Tuple<Bitmap, Bitmap>的数组。 Let's call it CardCombinations . 我们称之为CardCombinations I recommend you to put this array in a utility class called CardUtility or something. 我建议你把这个数组放在名为CardUtility的实用程序类中。 Then, you can just do: 然后,你可以这样做:

card = cards.Next(0, 9);
pictureBox1.Image = CardUtility.CardCombinations[card].Item1;
pictureBox2.Image = CardUtility.CardCombinations[card].Item2;

As you can see, this greatly reduced the code in the button1_Click method. 如您所见,这大大减少了button1_Click方法中的代码。 Now we can declare the array that I was talking about. 现在我们可以声明我正在谈论的数组。

It's pretty simple: 这很简单:

public static Tuple<Bitmap, Bitmap>[] CardCombinations => new[] {
    new Tuple<Bitmap, Bitmap>(Properties.Resources.king_d, Properties.Resources.jack_s),
    ...
};

"But that's still verbose!" “但那仍然很冗长!” you cried. 你哭了 Protip: you can use the using static directive to shorten the bitmap names to king_d and jack_s ! Protip:您可以使用using static指令将位图名称缩短为king_djack_s

using static SomeNamespace.Properties.Resources;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM