处理Winforms中卡片组计数的问题

时间:2014-04-01 09:29:10

标签: c# winforms forms picturebox

我正在处理一副牌,如果用户点击牌组(pictureBox1),牌组的大小应减少1,并且所绘牌的图像应显示在另一张PictureBox上。但是,当我运行调试器时,我看到尽管甲板大小为4,这是正确的,但在Form()函数中,一旦我点击甲板(pic​​tureBox1),甲板大小就会变为0。当我点击牌组时,如何更改代码以使牌组大小为4,然后它减少到3,依此类推?

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Controls.Add(pictureBox1);

        Card Drill = new Card();
        Card Barrel = new Card();
        Card Cyber = new Card();
        Card Mobius = new Card();
        var KaibaDeck = new List<Card>() {Drill,Barrel,Cyber,Mobius};
        var KaibaHand = new List<Card>() { };
        Drill.Pic = Image.FromFile("C:/wamp/www/drillroid.jpg");
        Barrel.Pic = Image.FromFile("C:/wamp/www/barreldragon.jpg");
        Cyber.Pic = Image.FromFile("C:/wamp/www/cyberphoenix.jpg");
        Mobius.Pic = Image.FromFile("C:/wamp/www/Mobius.jpg");           
    }

    Card Drill = new Card();
    Card Barrel = new Card();
    Card Cyber = new Card();
    Card Mobius = new Card();
    List<Card> KaibaDeck = new List<Card>();

    static Card DrawForKaiba(List<Card> KaibaDeck)
    {
        int KaibaDeckSize = KaibaDeck.Count;
        KaibaDeckSize = KaibaDeck.Count;       
        Random random = new Random();
        int DrawKaiba = random.Next(0, 100) % KaibaDeckSize + 1;
        Card Drew = new Card();
        Drew = KaibaDeck[DrawKaiba - 1];
        KaibaDeck.Remove(KaibaDeck[DrawKaiba - 1]);
        return Drew;
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        Card Drew = new Card();
        Drew = DrawForKaiba(KaibaDeck);
        Image HandCard = Drew.Pic;
        PictureBox pb4 = new PictureBox();
        pb4.Location = new Point(22, 144);
        pb4.Image = HandCard;
        this.Controls.Add(pb4);
        pb4.BringToFront();
    }
}

class Card
{
    int attack;
    string name;
    Image HandCard;
    Image pic;

    public int Attack
    {
        get { return attack; }
        set { attack = value; }
    }
    public string Name
    {
        get { return name; }
        set { name = value; }
    }
    public Image Pic
    {
        set{

            Bitmap pic = new Bitmap(68, 106);
            using (Graphics gr = Graphics.FromImage(pic))
            {
            gr.SmoothingMode = SmoothingMode.HighQuality;
            gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
            gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
            gr.DrawImage(value, new Rectangle(0, 0, 68, 106));
            }
        }
        get
        {
            return pic;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

可能更改此行可以解决您的问题

  var KaibaDeck = new List<Card>() {Drill,Barrel,Cyber,Mobius};

将其更改为

  KaibaDeck = new List<Card>() {Drill,Barrel,Cyber,Mobius};

您正在初始化本地卡片列表,而不是在表单级别声明的全局卡片 当然,您可以删除全局初始化并仅保留变量声明

List<Card> KaibaDeck = null;