主要思路:利用button的特性,及巧妙使用tag属性。
设计:一个窗体,一个随机数数组类。
随机数数组类:(RandomNumber)可以得到任意0——X直接的随机整数排列数组
class RandomNumber { private int[] random; ////// 随机数数组类 /// public RandomNumber(int number) { random = new int[number]; } ////// 任意随机数数组 /// public int[] GetRandom() { Random r = new Random(); for (int i = 0; i < random.Length; ) { int temp = r.Next(0, random.Length); bool op = true; for (int j = 0; j < i; j++) { if (random[j] == temp) { op = false; break; } } if (op) { random[i] = temp; i++; } } return random; } }
界面:三个计时器timer,imageList控件一个
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace 记忆纸牌{ public partial class MemoryCards : Form { public MemoryCards() { InitializeComponent(); } private void MemoryCards_Load(object sender, EventArgs e) { Inint(); } ////// 初始化 /// private void Inint() { DealCard();//发牌 } //变量 private Button[] buttons = new Button[16];//按钮数组 private int[] rand;//随机数组 private int time = 10;//倒计时计时器默认时间 private int gameTime = 0;//游戏时间 private Button btn1, btn2;//临时btn变量 private bool bl = true;//按钮用到的变量 ////// 发牌 /// private void DealCard() { rand = new RandomNumber(16).GetRandom();//得到随机数 int i = 0; foreach (Button item in flowLayoutPanel1.Controls)//容器中的button赋给button数组 { item.Enabled = true; buttons[i] = item; i++; } //给button设置图片 for (i = 0; i < rand.Length / 2; i++) { buttons[rand[i]].ImageList = imageList1; buttons[rand[i]].ImageIndex = i; buttons[rand[i]].Tag = i; } for (i = rand.Length / 2; i < rand.Length; i++) { buttons[rand[i]].ImageList = imageList1; buttons[rand[i]].ImageIndex = i - 8; buttons[rand[i]].Tag = i - 8; } } ////// 翻转牌 /// private void TurnCard() { for (int i = 0; i < rand.Length; i++) { buttons[i].ImageList = imageList1; buttons[i].ImageIndex = 8; } } ////// 完成 /// ///private bool Success() { int i = 0; foreach (Button item in flowLayoutPanel1.Controls) { if (item.Enabled == false) { i++; } } return i == 16 ? true : false; ; } /// /// 倒计时计时器事件 /// /// /// private void RTimer_Tick(object sender, EventArgs e) { if (time > 0) { time--; lb_ShowTime.Text = "倒计时:" + time.ToString(); } else { TurnCard(); lb_ShowTime.Text = "计时:" + gameTime.ToString(); RTimer.Stop();//停止倒计时 GameTimer.Start(); //启动游戏计时器 } } ////// 按钮事件 /// /// /// private void btn_Click(object sender, EventArgs e) { if (GameTimer.Enabled) { //翻转button后,显示图片 Button btn = sender as Button; btn.ImageList = imageList1; btn.ImageIndex = Convert.ToInt32(btn.Tag.ToString()); if (bl) { btn1 = btn; bl = false; return; } else { btn2 = btn; bl = true; } //判断是否一样 if (btn1.ImageIndex == btn2.ImageIndex && btn1 != btn2) { btn1.Enabled = false; btn2.Enabled = false; } else { ClickCardTimer.Start();//启动button翻转后显示时间 } } } ////// 开始,重新开始 /// /// /// private void btn_StartORRestart_Click(object sender, EventArgs e) { if (btn_StartORRestart.Text == "重新开始") { DealCard();//发牌 //初始变量为默认 time = 10; gameTime = 0; GameTimer.Stop();//游戏时间计时器停止 lb_ShowTime.Text = "倒计时:" + time.ToString(); RTimer.Start();//倒计时开始 } else { btn_StartORRestart.Text = "重新开始"; lb_ShowTime.Text = "倒计时:" + time.ToString(); DealCard();//发牌 RTimer.Start();//倒计时开始 } } ////// 点击翻转后显示的时间 /// /// /// private void ClickCardTimer_Tick(object sender, EventArgs e) { btn1.ImageIndex = 8; btn2.ImageIndex = 8; ClickCardTimer.Stop(); } ////// 游戏时间计时 /// /// /// private void GameTimer_Tick(object sender, EventArgs e) { gameTime++; lb_ShowTime.Text = "计时:" + gameTime.ToString(); //判断是否完成 if (Success()) { GameTimer.Stop(); MessageBox.Show("完成!,耗时:" + gameTime.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }}
实现有点不合理,时间仓促,就到这里了