C#翻牌遊戲

資管二甲 U1035128 郭璟塘

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 ForgetGame
{
    public partial class MainForm : Form
    {
        int[] arrPic = new int[16];
        int time = 0;//遊戲所用時間
        int returnNum = 0;//當前翻轉圖片的張數
        int picHideNum = 0;//隱藏的張數
        int wordRecord = 0;

        int first = -1, sencend = -1;//第一,二次當前點擊圖片的編號
        public MainForm()
        {
            InitializeComponent();
        }

        private void tspRule_Click(object sender, EventArgs e)
        {
            GameRule rule = new GameRule();
            rule.ShowDialog();
        }

        private void tsnGameOver_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void tsmNewGame_Click(object sender, EventArgs e)
        {
            foreach (Control item in this.Controls)
            {
                if (item is PictureBox)
                {
                    ((PictureBox)item).Visible = true;
                }
            }
            for (int i = 0; i < arrPic.Length; i++)
            {
                arrPic[i] = -1;
            }
            for (int i = 0; i <= 7; i++)
            {
                PutImageToArrary(i);
                PutImageToArrary(i);
            }
            ShowImage();
            this.timer1.Enabled = true;


        }

        private void PutImageToArrary(int index)
        {
            Random r = new Random();
            int rand = r.Next(16);
            while (arrPic[rand] != -1)
            {
                rand = r.Next(16);
            }
            arrPic[rand] = index;

        }
        //顯示圖片
        private void ShowImage()
        {
            foreach (Control item in this.Controls)
            {
                if (item is PictureBox)
                {

                    int picindex = arrPic[Convert.ToInt32(((PictureBox)item).Tag)];
                    ((PictureBox)item).Image = imageList1.Images[picindex];
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (Control item in this.Controls)
            {
                if (item is PictureBox)
                {
                    ((PictureBox)item).Image = imageList1.Images[8];
                }
            }
            this.timer1.Enabled = false;
            MessageBox.Show("遊戲開始,即將計時!");
            time = 0;
            this.timer2.Enabled = true;
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.label1.Text = string.Format("遊戲所用時間:{0}秒", time++);

        }

        private void PB0_Click(object sender, EventArgs e)
        {
            if (returnNum == 0)//第一次點擊圖片
            {

                first = Convert.ToInt32(((PictureBox)sender).Tag);
                ((PictureBox)sender).Image = this.imageList1.Images[arrPic[first]];
                returnNum++;
            }
            else if (returnNum == 1)
            {
                sencend = Convert.ToInt32(((PictureBox)sender).Tag);
                if (sencend == first)
                {
                    this.timer3.Enabled = true;
                }
                else
                {
                    ((PictureBox)sender).Image = this.imageList1.Images[arrPic[sencend]];
                    returnNum++;
                    if (arrPic[first] ==​​ arrPic[sencend])
                    {
                        foreach (Control item in this.Controls)
                        {

                            if (item is PictureBox)
                            {

                                int current = Convert.ToInt32(((PictureBox)item).Tag);
                                //找到第一次點擊或第二次的圖片
                                if (current == first || current == sencend)
                                {
                                    ((PictureBox)item).Visible = false;
                                    returnNum = 0;
                                    picHideNum = picHideNum + 1;
                                    while (picHideNum == 16)
                                    {
                                        DialogResult rs = MessageBox.Show("遊戲結束", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                                        if (wordRecord < time)
                                        {
                                            this.label2.Text = string.Format("當前最高紀錄:{0}秒", time);
                                            this.label1.Text = string.Format("遊戲所用時間:{0}秒", time);
                                        }
                                        else
                                        {
                                            this.label2.Text = string.Format("當前最高紀錄:{0}秒", wordRecord);
                                            this.label1.Text = string.Format("遊戲所用時間:{0}秒", time);
                                        }
                                        this.timer2.Enabled = false;
                                        break;

                                    }

                                }
                            }

                        }

                    }
                    else
                    {
                        this.timer3.Enabled = true;

                    }
                }

            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void 遊戲ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            foreach (Control item in this.Controls)
            {

                if (item is PictureBox)
                {

                    int current = Convert.ToInt32(((PictureBox)item).Tag);

                    if (current == first || current == sencend)
                    {
                        ((PictureBox)item).Image = imageList1.Images[8];
                        returnNum = 0;
                        this.timer3.Enabled = false;

                    }
                }
            }
        }



    }
}

results matching ""

    No results matching ""