3.配对游戏

3.配对游戏

  新建 匹配游戏的 Windows窗体应用程序  属性 Text-匹配游戏  size 550*550
  
  添加TabeLayoutPanel控件   BackColor-CornflowerBlue(Web)        Dock-Fill

  添加行和列,编辑行和列(百分比为25%)
 
  将窗体布局为4*4 平均分布各行各列  (16个大小相等的方块单元格)

  选择TabelLayoutPanel 将Label添加到左上方的方块中
 
   Label属性 BackColor-CornflowerBlue AutoSize-False  Dock-Fill   TextAlign-MiddleCenter

   设置Font属性,字体为Webdings72 point Bold   Text-c  GdiCharSet-2

    !  是一个蜘蛛
    N  是一个眼球
    ,  是一个辣椒

  复制粘贴标签

//创建列表
   public partial class Form2 : Form
   {
    Random random = new Random();

    List<string> icons = new List<string>()
    {
     "!","!","N","N",",",",","K","K",
     "b","b","v","v","w","w","z","z"
    };
   }
 
 //向每个标签分配随机图标
 private void AssignIconsToSquares()
{
  foreach(Control control in tableLayoutPanel1.Controls)
   {
    //The statements you want to execute
    //for each label go here
    //The statements use iconLabel to access
    //each label's properties and methosd

    Label iconLabel = control as Label;
    if(iconLabel != null)
     {
      int randomNumber = random.Next(icons.Count);
      iconLabel.Text = icons [randomNumber];
      icons.RemoveAt(randomNumber);
     }
   }
}

  //调用方法
  public Form2()
 {
   InitializeComponent();

   AssignIconsToSquares();
 }
 
 //取消注释 在foreach循环(if里面)中取消被注释的代码行
 iconLabel.ForeColor = iconLabel.BackColor;


 //添加Click事件处理程序
 private void labe_Click(object sender,EventArgs e)
 {
  Label clickedLabel = sender as Label;

  if(clickedLabel != null)
   {
     //If the clicked label is black,the player clicked
     //an icon that's already been revealed --
     //ignore the click
     if(clickedLabel.ForeColor == Color.Black)
        return;     
 
     clickedLabel.ForeColor = Color.Black;
   }
 }

选中所有标签控件,在属性中找到Click 事件处理程序 label_Click

//添加标签引用

public partial class Form2 : Form
  {
  //firstClicked points to the first Label control
  //that the player clicks, but it will be null
  //if the player hasn't clicked a label yet

        Label firstClicked = null;

  //secondClicked points to the second Label control
  //that the player clicks

          Label secondClicked = null;
 
  }

  //修改Click事件处理程序
     
      if(firstClicked == null)
      {
         firstClicked = clickedLabel;
         firstClicked.ForeColor = Color.Black;

         return;
      }

--------------------------------------------------------------------------------------------------------
private void labe_Click(object sender, EventArgs e)
        {
            Label clickedLabel = sender as Label;

            if (clickedLabel != null)
            {
               
                if (clickedLabel.ForeColor == Color.Black)
                    return;
              
                if (firstClicked == null)
                {
                    firstClicked = clickedLabel;
                    firstClicked.ForeColor = Color.Black;

                    return;
                }
            }
        }
-----------------------------------------------------------------------------------------------------------

添加计时器

转到Windows窗体设计器       工具箱,组件类别        双击Timer
 
            单击timer1设置属性 Interval-750  Enabled-false
            双击timer1添加Tick事件处理程序
        private void timer1_Tick(object sender, EventArgs e)
        {
            //Stop the timer
            timer1.Stop();

            //Hide both icons
            firstClicked.ForeColor = firstClicked.BackColor;
            secondClicked.ForeColor = secondClicked.BackColor;

            //Reset firstClicked and secondClicked
            //so the next time a label is
            //clicked,the program knows it's the first click
            firstClicked = null;
            secondClicked = null;
        }
//修改
在private void label_Click()刚开始添加
           if(timer1.Enabled ==true)
            {
                return;
            }
  在第二个if中的最后加:
                secondClicked = clickedLabel;
                secondClicked.ForeColor = Color.Green;                                      timer1.Start();
  保持匹配对可见
将下面的IF语句添加到lablel_Click()事件处理程序方法的尾部附近,紧靠启动计时器的语句的上方
                if (firstClicked.Text == secondClicked.Text)
                {
                    firstClicked = null;
                    secondClicked = null;
                    return;
                }
              


  //验证玩家是否获胜
  private void CheckForWinner()
        {
              foreach (Control control in tableLayoutPanel1.Controls)
              {
                 Label iconLabel = control as Label;

                if (iconLabel != null)
                 {
                    if (iconLabel.ForeColor == iconLabel.BackColor)
                    return;
                 }
             }
        
        MessageBox.Show(@"You matched all the icons!",@"Congratulations");
        Close();
       }

//验证玩家是否获胜
                secondClicked = clickedLabel;
                secondClicked.ForeColor = Color.Black;

               //Check to see if the player won
                CheckForWinner();

                if (firstClicked.Text == secondClicked.Text)
                {
                    firstClicked = null;
                    secondClicked = null;
                    return;
                }

                timer1.Start();

posted @ 2012-04-03 21:05  珍爱贝贝1314  阅读(296)  评论(1编辑  收藏  举报