继前一篇"
C#玩4张纸牌小游戏
View Code
public partial class Form2 : Form { private IList<Card> cards; private IList<Card> currentCards; public Form2 ( ) { InitializeComponent ( ); cards = new List<Card> (52); currentCards = new List<Card> (4); BuildCardData ( ); } private void BuildCardData ( ) { cards.Clear ( ); for (int i = 1; i <= 13; i++) { for (int j = 1; j <= 4; j++) { cards.Add (new Card ((Point) i, (Flower) j)); } } } private void Form2_Load ( object sender, EventArgs e ) { Reflesh ( ); RePaint ( ); } private void Reflesh ( ) { IList<Card> tmp = new List<Card> (cards); Random rd = new Random (); int intTmp = 0; for (int i = 0; i < 4; i++) { intTmp = rd.Next (1,52 - i - 1); if (currentCards.Count < i + 1) { currentCards.Add (new Card (Point.A, Flower.Flower)); #region MyRegion switch (i) { case 0: currentCards[i].gpPalette = Graphics.FromHwnd (pbFirst.Handle); break; case 1: currentCards[i].gpPalette = Graphics.FromHwnd (pbSecond.Handle); break; case 2: currentCards[i].gpPalette = Graphics.FromHwnd (pbThird.Handle); break; case 3: currentCards[i].gpPalette = Graphics.FromHwnd (pbFourth.Handle); break; default: break; } #endregion } currentCards[i].flower=tmp[intTmp].flower; currentCards[i].point = tmp[intTmp].point; tmp.RemoveAt (intTmp); } } private void btnReflesh_Click ( object sender, EventArgs e ) { Reflesh ( ); RePaint ( ); } private void RePaint ( ) { currentCards[0].Paint ( ); currentCards[1].Paint ( ); currentCards[2].Paint ( ); currentCards[3].Paint ( ); } private void pbFirst_Paint ( object sender, PaintEventArgs e ) { currentCards[0].Paint ( e.Graphics); } private void pbSecond_Paint ( object sender, PaintEventArgs e ) { currentCards[1].Paint (e.Graphics); } private void pbThird_Paint ( object sender, PaintEventArgs e ) { currentCards[2].Paint (e.Graphics); } private void pbFourth_Paint ( object sender, PaintEventArgs e ) { currentCards[3].Paint (e.Graphics); } }