code.club

 找回密碼
 立即註冊
搜索
查看: 3176|回復: 1
打印 上一主題 下一主題

多選翻翻樂memory match game

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2021-11-28 11:14:13 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最後由 csharp 於 2021-12-27 13:03 編輯
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Media;

  11. namespace memory_match_game
  12. {
  13.     //This program is made by Sun Ying-Chi 2021/07/30
  14.     public partial class Form1 : Form
  15.     {
  16.         Rectangle currentScreen = Screen.PrimaryScreen.WorkingArea;//得出所使用螢幕之長寬值
  17.         PictureBox play = new PictureBox();
  18.         //底下有static的變數都是為了讓Form2能直接使用的
  19.         public static PictureBox[] pNumberChoiceShow = new PictureBox[5];
  20.         PictureBox[] pNumberChoice = new PictureBox[5];
  21.         public static PictureBox[] pNumberChoiceGo = new PictureBox[5];
  22.         PictureBox[] pImg = new PictureBox[3];
  23.         PictureBox mySound = new PictureBox();
  24.         public static bool soundON = true;
  25.         public static PictureBox[] pThemeShow = new PictureBox[3];
  26.         PictureBox[] pThemeChoice = new PictureBox[3];
  27.         public static PictureBox[] pThemeChoiceGo = new PictureBox[3];
  28.         public static bool hasClicked2;
  29.         public static bool hasClicked1;
  30.         public static int pChoiceNumber;
  31.         public static int pThemeNumber;
  32.         int w, h;
  33.         public Form1()
  34.         {
  35.             this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);
  36.             InitializeComponent();
  37.             this.WindowState = FormWindowState.Maximized;
  38.             f1AllSetting();
  39.         }

  40.         void Form_FormClosing(object sender, FormClosingEventArgs e)
  41.         {
  42.             Application.Exit();
  43.         }

  44.         void f1AllSetting()
  45.         {
  46.             w = currentScreen.Width / 12;//寬度是目前螢幕的十二分之一
  47.             h = currentScreen.Width / 12;
  48.             this.BackgroundImage = Properties.Resources.form_bg_12;
  49.             this.BackgroundImageLayout = ImageLayout.Stretch;
  50.             //從Form2傳過來的值,以免轉回來時這裡的sound圖案不同步
  51.             if (soundON)
  52.             {
  53.                 mySound.Image = Properties.Resources.sound_on;
  54.             }
  55.             else
  56.             {
  57.                 mySound.Image = Properties.Resources.sound_off;
  58.             }

  59.             soundON = true;//預設都是開的
  60.             mySound.Size = new Size(currentScreen.Width / 25, currentScreen.Width / 25);
  61.             mySound.SizeMode = PictureBoxSizeMode.StretchImage;
  62.             mySound.BackColor = Color.Transparent;
  63.             mySound.Location = new Point(currentScreen.Width / 2 + w / 2, w * 5 + w / 40 * 6);
  64.             mySound.Click += new EventHandler(mySound_click);
  65.             Controls.Add(mySound);

  66.             play.Image = Properties.Resources.start_blue;
  67.             play.Size = new Size(currentScreen.Width / 25, currentScreen.Width / 25);
  68.             play.SizeMode = PictureBoxSizeMode.StretchImage;
  69.             play.BackColor = Color.Transparent;
  70.             play.Location = new Point(currentScreen.Width / 2 - w * 2, w * 5 + w / 40 * 6);
  71.             Controls.Add(play);
  72.             play.Click += new EventHandler(play_click);

  73.             for (int i = 0; i < pNumberChoiceShow.Length; i++)
  74.             {
  75.                 pNumberChoiceShow[i] = new PictureBox();
  76.                 pNumberChoice[i] = new PictureBox();
  77.                 pNumberChoiceGo[i] = new PictureBox();
  78.                 pNumberChoice[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("c" + i);
  79.                 pNumberChoiceGo[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("g" + i);
  80.                 pNumberChoiceShow[i].Image = pNumberChoice[i].Image;
  81.                 pNumberChoiceShow[i].Size = new Size(currentScreen.Width / 8, currentScreen.Width / 16);
  82.                 pNumberChoiceShow[i].SizeMode = PictureBoxSizeMode.StretchImage;
  83.                 pNumberChoiceShow[i].Location = new Point(currentScreen.Width / 2, currentScreen.Height / 8 + i * currentScreen.Width / 15);
  84.                 pNumberChoiceShow[i].BackColor = Color.Transparent;
  85.                 Controls.Add(pNumberChoiceShow[i]);
  86.                 pNumberChoiceShow[i].Click += new EventHandler(pNumberChoiceShow_click);
  87.             }

  88.             for (int i = 0; i < pThemeShow.Length; i++)
  89.             {
  90.                 pThemeChoice[i] = new PictureBox();
  91.                 pThemeChoiceGo[i] = new PictureBox();
  92.                 pThemeShow[i] = new PictureBox();
  93.                 pThemeShow[i].Size = new Size(currentScreen.Width / 8, currentScreen.Width / 16);
  94.                 pThemeShow[i].SizeMode = PictureBoxSizeMode.StretchImage;
  95.                 pThemeShow[i].Location = new Point(currentScreen.Width / 2 - w * 2, currentScreen.Height / 8 + i * currentScreen.Width / 8);
  96.                 pThemeShow[i].BackColor = Color.Transparent;
  97.                 pThemeShow[i].Click += new EventHandler(pThemeShow_click);
  98.                 Controls.Add(pThemeShow[i]);
  99.             }

  100.             pThemeChoice[0].Image = Properties.Resources.xmas_theme;
  101.             pThemeChoice[1].Image = Properties.Resources.numbers_theme;
  102.             pThemeChoice[2].Image = Properties.Resources.food_theme;

  103.             pThemeChoiceGo[0].Image = Properties.Resources.xmas_theme_go;
  104.             pThemeChoiceGo[1].Image = Properties.Resources.numbers_theme_go;
  105.             pThemeChoiceGo[2].Image = Properties.Resources.food_theme_go;

  106.             //先給pThemeShow圖案
  107.             for (int i = 0; i < pThemeShow.Length; i++)
  108.                 pThemeShow[i].Image = pThemeChoice[i].Image;

  109.             void play_click(object sender, EventArgs e)
  110.             {
  111.                 Form2 f2 = new Form2();
  112.                 f2.Show();
  113.                 this.Hide();
  114.             }

  115.             //sound圖案的選擇方式
  116.             void mySound_click(Object sender, EventArgs e)
  117.             {
  118.                 if (soundON == false)
  119.                 {
  120.                     soundON = true;
  121.                     mySound.Image = Properties.Resources.sound_on;
  122.                     Form2.soundOn = soundON;
  123.                 }
  124.                 else if (soundON == true)
  125.                 {
  126.                     soundON = false;
  127.                     mySound.Image = Properties.Resources.sound_off;
  128.                     Form2.soundOn = soundON;
  129.                 }
  130.             }
  131.             //sound圖案的選擇方式end

  132.         }
  133.         //----f1AllSetting---end


  134.         void pThemeShow_click(Object sender, EventArgs e)
  135.         {
  136.             PictureBox pb = (PictureBox)sender;
  137.             hasClicked1 = true;//如果玩家曾經按過這裡的選項就變成true
  138.             for (int i = 0; i < pThemeShow.Length; i++)
  139.             {
  140.                 if (pb.Image == pThemeShow[i].Image) pThemeNumber = i;//取得被選的號碼是哪個
  141.                 pThemeShow[i].Image = pThemeChoice[i].Image;//先全都歸成原狀
  142.             }
  143.             pb.Image = pThemeChoiceGo[pThemeNumber].Image;//再將被選定者給予新值
  144.             if (pThemeNumber == 0) Form2.myImageChoice = 1;
  145.             else if (pThemeNumber == 1) Form2.myImageChoice = 2;
  146.             else if (pThemeNumber == 2) Form2.myImageChoice = 3;
  147.         }



  148.         void pNumberChoiceShow_click(Object sender, EventArgs e)
  149.         {
  150.             PictureBox pb = (PictureBox)sender;
  151.             hasClicked2 = true;//如果玩家曾經按過這裡的選項就變成true
  152.             for (int i = 0; i < pNumberChoiceShow.Length; i++)
  153.             {
  154.                 if (pb.Image == pNumberChoice[i].Image) pChoiceNumber = i;//取得被選的號碼是哪個
  155.                 pNumberChoiceShow[i].Image = pNumberChoice[i].Image;//先全都歸成原狀
  156.             }
  157.             pb.Image = pNumberChoiceGo[pChoiceNumber].Image;//再將被選定者給予新值
  158.             if (pChoiceNumber == 0) Form2.cardNumber = 4;
  159.             else if (pChoiceNumber == 1) Form2.cardNumber = 8;
  160.             else if (pChoiceNumber == 2) Form2.cardNumber = 12;
  161.             else if (pChoiceNumber == 3) Form2.cardNumber = 18;
  162.             else if (pChoiceNumber == 4) Form2.cardNumber = 24;
  163.         }

  164.     }
  165. }
複製代碼



回復

使用道具 舉報

沙發
 樓主| 發表於 2021-11-28 11:14:48 | 只看該作者
本帖最後由 csharp 於 2021-11-28 11:32 編輯
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Media;

  11. namespace memory_match_game
  12. {
  13.     //This program is made by Sun Ying-Chi 2021/07/30
  14.     public partial class Form2 : Form
  15.     {
  16.         //先給預設值,以免直接按入時出錯
  17.         //底下有static的變數都是為了讓Form1能直接使用的
  18.         public static int cardNumber = 8;//真正不重覆的牌數
  19.         public static int myImageChoice = 3;//選擇哪個牌色
  20.         public static bool musicOn = true;//設定音樂開關
  21.         public static bool soundOn = true;//設定音效開關

  22.         public Form2()
  23.         {
  24.             InitializeComponent();

  25.             //表單關閉全程式
  26.             this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing);

  27.             //主體程式               
  28.             f2allSetting();

  29.         }

  30.         //設定表單關閉全程式的方式
  31.         void Form_FormClosing(object sender, FormClosingEventArgs e)
  32.         {
  33.             Application.Exit();
  34.         }

  35.         void f2allSetting()
  36.         {
  37.             this.WindowState = FormWindowState.Maximized;//每次都將螢幕開最大
  38.             this.BackgroundImage = Properties.Resources.form_bg_12;
  39.             this.BackgroundImageLayout = ImageLayout.Stretch;
  40.             //this.BackgroundImageLayout = ImageLayout.Tile;
  41.             Rectangle currentScreen = Screen.PrimaryScreen.WorkingArea;//得出所使用螢幕之長寬值
  42.             int w, h;//卡片的長和寬,本例二者是一樣高的,其實可以只用一個變數

  43.             PictureBox[] pImage = new PictureBox[cardNumber];//真正的牌的數目
  44.             PictureBox[] pShow = new PictureBox[cardNumber * 2];//真正有位置而會顯示的圖陣列
  45.             PictureBox[] pRandom = new PictureBox[cardNumber * 2];//存放已亂數之後的牌卡
  46.             bool[] matched = new bool[cardNumber * 2];//記錄是否已翻之牌已和另一張相符
  47.             bool[] isClicked = new bool[cardNumber * 2];//記錄是否已翻之牌
  48.             Random rnd = new Random();
  49.             int[] array4random = new int[cardNumber];//用於亂數之用

  50.             int whichOne = 0;//選擇到哪個數字
  51.             PictureBox temp1 = new PictureBox();//可以不要有其他設定,但一定要有實體化設定
  52.             PictureBox temp2 = new PictureBox();//可以不要有其他設定,但一定要有實體化設定
  53.             PictureBox pBack = new PictureBox();//存放背卡圖,若要給不同花色之牌不同背卡,就需要pBack陣列
  54.             int tempN1 = 0;//用於每輪翻牌時存入該二張牌的格數
  55.             int tempN2 = 0;//用於每輪翻牌時存入該二張牌的格數

  56.             SoundPlayer flip = new SoundPlayer(Properties.Resources.flip_0);//翻牌音效
  57.             SoundPlayer error = new SoundPlayer(Properties.Resources.error);//配對不成功
  58.             SoundPlayer gamewin = new SoundPlayer(Properties.Resources.applaud_0);//全部成功之結尾
  59.             SoundPlayer correct = new SoundPlayer(Properties.Resources.correct);//配對成功
  60.             PictureBox reStart = new PictureBox();
  61.             PictureBox home = new PictureBox();
  62.             PictureBox youWin = new PictureBox();//當靜音時才會出現awesome字樣的圖案表示贏了
  63.             Timer mytimer = new Timer();//翻牌不配對之後回復背卡之計時器
  64.             int time = 0;//計算時間//回復計時
  65.             int flipCount = 0;//計算每次一輪翻牌次數,最多二次
  66.             int row = 2;//多少張牌一列

  67.             //主要是setAll和亂排牌卡

  68.             setAll();
  69.             randomPicture();

  70.             //-----------主要牌卡設定以及被執行的功能------------
  71.             void setAll()
  72.             {
  73.                 tempN1 = 0;
  74.                 tempN2 = 0;
  75.                 w = currentScreen.Width / 12;//寬度是目前螢幕的十二分之一
  76.                 h = currentScreen.Width / 12;

  77.                 //you win shows
  78.                 youWin.Size = new Size(currentScreen.Width / 2, currentScreen.Width / 8);
  79.                 youWin.Location = new System.Drawing.Point(currentScreen.Width / 4, currentScreen.Height / 3);
  80.                 youWin.Image = Properties.Resources.awesome;
  81.                 youWin.SizeMode = PictureBoxSizeMode.StretchImage;
  82.                 youWin.BackColor = Color.Transparent;
  83.                
  84.                 //you win end

  85.                 //重新按鈕
  86.                 reStart.Size = new Size(currentScreen.Width / 25, currentScreen.Width / 25);
  87.                 reStart.Location = new System.Drawing.Point(currentScreen.Width / 2 + w / 2, w * 5 + w / 40 * 6);
  88.                 reStart.Image = Properties.Resources.restart;
  89.                 reStart.SizeMode = PictureBoxSizeMode.StretchImage;
  90.                 reStart.BackColor = Color.Transparent;
  91.                 reStart.Click += new EventHandler(restart_click);
  92.                 Controls.Add(reStart);
  93.                 //回首頁按鈕

  94.                 home.Size = new Size(currentScreen.Width / 25, currentScreen.Width / 25);
  95.                 home.SizeMode = PictureBoxSizeMode.StretchImage;
  96.                 home.BackColor = Color.Transparent;
  97.                 home.Image = Properties.Resources.home;
  98.                 home.Location = new System.Drawing.Point(currentScreen.Width / 2 - w * 2, w * 5 + w / 40 * 6);
  99.                 home.Click += new EventHandler(home_click);
  100.                 Controls.Add(home);

  101.                 void home_click(Object sender, EventArgs e)
  102.                 {
  103.                     gamewin.Stop();
  104.                     Form1 f1 = new Form1();//這個要放前面,不然下一行的功效會慢一拍
  105.                     Form1.soundON = soundOn;//讓Form1的音效選擇圖案同步
  106.                     f1.Show();
  107.                     if (Form1.hasClicked2)//如果Form1已經有點選過張數,這裡再把傳過來的結果再傳回去,好讓那裡的張數選擇圖案繼續保持
  108.                     {
  109.                         if (cardNumber == 4) Form1.pNumberChoiceShow[0].Image = Form1.pNumberChoiceGo[0].Image;
  110.                         else if (cardNumber == 8) Form1.pNumberChoiceShow[1].Image = Form1.pNumberChoiceGo[1].Image;
  111.                         else if (cardNumber == 12) Form1.pNumberChoiceShow[2].Image = Form1.pNumberChoiceGo[2].Image;
  112.                         else if (cardNumber == 18) Form1.pNumberChoiceShow[3].Image = Form1.pNumberChoiceGo[3].Image;
  113.                         else if (cardNumber == 24) Form1.pNumberChoiceShow[4].Image = Form1.pNumberChoiceGo[4].Image;
  114.                     }
  115.                     if (Form1.hasClicked1)//如果Form1已經有點選過牌色,這裡再把傳過來的結果再傳回去,好讓那裡的牌色選擇圖案繼續保持
  116.                     {
  117.                         if (myImageChoice == 1) Form1.pThemeShow[0].Image = Form1.pThemeChoiceGo[0].Image;
  118.                         else if (myImageChoice == 2) Form1.pThemeShow[1].Image = Form1.pThemeChoiceGo[1].Image;
  119.                         else if (myImageChoice == 3) Form1.pThemeShow[2].Image = Form1.pThemeChoiceGo[2].Image;
  120.                     }

  121.                     Hide();
  122.                 }
  123.                 //--------回首頁按鈕end-----

  124.                 rowSetting();
  125.                 //---------設置牌尺寸及每列牌數------------
  126.                 void rowSetting()
  127.                 {
  128.                     if (cardNumber == 4)
  129.                     {
  130.                         w = currentScreen.Width / 8;
  131.                         h = currentScreen.Width / 8;
  132.                         row = 4;//每4張牌換列
  133.                     }
  134.                     else if (cardNumber == 8)
  135.                     {
  136.                         w = currentScreen.Width / 10;
  137.                         h = currentScreen.Width / 10;
  138.                         row = 8;//每8張牌換列
  139.                     }
  140.                     else if (cardNumber == 12)
  141.                     {
  142.                         w = currentScreen.Width / 10;
  143.                         h = currentScreen.Width / 10;
  144.                         row = 8;//每8張牌換列
  145.                     }
  146.                     else if (cardNumber == 18)
  147.                     {
  148.                         w = currentScreen.Width / 11;
  149.                         h = currentScreen.Width / 11;
  150.                         row = 9;//每9張牌換列
  151.                     }
  152.                     else if (cardNumber == 24)
  153.                     {
  154.                         w = currentScreen.Width / 14;
  155.                         h = currentScreen.Width / 14;
  156.                         row = 12;
  157.                     }
  158.                 }
  159.                 //---------設置牌尺寸及每列牌數 end------------


  160.                 //-------選擇牌色以及背卡的圖案------
  161.                 void imageChoice()
  162.                 {
  163.                     if (myImageChoice == 1)
  164.                     {
  165.                         pBack.Image = Properties.Resources.xmas_back;
  166.                         for (int i = 0; i < cardNumber; i++)
  167.                         {
  168.                             pImage[i] = new PictureBox();
  169.                             pImage[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("x" + i);
  170.                         }
  171.                     }
  172.                     else if (myImageChoice == 2)
  173.                     {
  174.                         pBack.Image = Properties.Resources.number_back;
  175.                         for (int i = 0; i < cardNumber; i++)
  176.                         {
  177.                             pImage[i] = new PictureBox();
  178.                             pImage[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("n" + i);
  179.                         }
  180.                     }

  181.                     else if (myImageChoice == 3)
  182.                     {
  183.                         pBack.Image = Properties.Resources.food_back;
  184.                         for (int i = 0; i < cardNumber; i++)
  185.                         {
  186.                             pImage[i] = new PictureBox();
  187.                             pImage[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("f" + i);
  188.                         }
  189.                     }
  190.                 }
  191.                 //-------選擇牌色以及背卡的圖案 end------

  192.                 //計時器間隔和方法
  193.                 mytimer.Interval = 100;
  194.                 mytimer.Tick += new EventHandler(mytimer_tick);
  195.                 //----重新按鈕方法-----
  196.                 void restart_click(object sender, EventArgs e)
  197.                 {
  198.                     if (soundOn == true)
  199.                         flip.Play();
  200.                     gamewin.Stop();
  201.                     startOver();
  202.                 }

  203.                 //重新開始的方法
  204.                 void startOver()
  205.                 {
  206.                     Controls.Remove(youWin);
  207.                     temp1.Image = null;//以防點開一張未匹配之前就重玩時,會殘留圖像
  208.                     temp2.Image = null;//不加這個,如果連按二次restart後,翻開的前二張不符合也不會翻回去
  209.                     for (int i = 0; i < cardNumber * 2; i++)
  210.                     {
  211.                         pShow[i].Image = pBack.Image;
  212.                         matched[i] = false;
  213.                         isClicked[i] = false;
  214.                     }
  215.                     randomPicture();
  216.                 }//重新開始的方法end

  217.                 void mytimer_tick(object sender, EventArgs e)
  218.                 {
  219.                     time++;//計時開始
  220.                     if (time == 4)
  221.                     {
  222.                         //目前翻開的二張圖格回到背卡
  223.                         pShow[tempN1].Image = pBack.Image;
  224.                         pShow[tempN2].Image = pBack.Image;
  225.                         isClicked[tempN1] = false;
  226.                         isClicked[tempN2] = false;
  227.                         mytimer.Stop();//計時器停止
  228.                         time = 0;//時間歸零
  229.                     }
  230.                 }

  231.                 imageChoice();//選擇牌色以及背卡的圖案


  232.                 for (int i = 0; i < cardNumber * 2; i++)
  233.                 {
  234.                     matched[i] = new bool();
  235.                     isClicked[i] = new bool();
  236.                     isClicked[i] = false;
  237.                     pShow[i] = new PictureBox();//主要圖形顯示格位
  238.                     //卡片位置排列是以所用螢幕長寬來決定,w/40是卡片與卡片的間隔空白,並由列數來決定上面空白長度
  239.                     if (row == 4)
  240.                         pShow[i].Location = new System.Drawing.Point(w * 2 + i % row * (w + w / 40), w / 2 + i / row * (h + w / 40));//中間有留白
  241.                     else if (row == 8)
  242.                         pShow[i].Location = new System.Drawing.Point(w + i % row * (w + w / 40), w + i / row * (h + w / 40));//中間有留白
  243.                     else if (row == 9)
  244.                         pShow[i].Location = new System.Drawing.Point(w + i % row * (w + w / 40), w / 2 + i / row * (h + w / 40));//中間有留白
  245.                     else if (row == 12)
  246.                         pShow[i].Location = new System.Drawing.Point(w + i % row * (w + w / 40), w * 5 / 4 + i / row * (h + w / 40));//中間有留白

  247.                     pShow[i].Size = new Size(w, h);//卡片尺寸
  248.                     pShow[i].SizeMode = PictureBoxSizeMode.StretchImage;//卡片依螢幕而放大或縮小
  249.                     pShow[i].Image = pBack.Image;
  250.                     Controls.Add(pShow[i]);
  251.                     pShow[i].Click += new EventHandler(pShow_click);

  252.                     //pRandom用來接納亂序之後的各圖形
  253.                     pRandom[i] = new PictureBox();
  254.                     pRandom[i].Size = new Size(w, h);
  255.                 }
  256.             }
  257.             //----------------主要牌卡設定以及被執行的功能 end-------------------

  258.             //判定是否贏的方法
  259.             void doYouWin()
  260.             {
  261.                 bool win = true;
  262.                 //如果中間有任何一個matched不是true,則win就變成false
  263.                 foreach (bool b in matched)
  264.                 {
  265.                     if (b != true) win = false;
  266.                     //break;
  267.                 }

  268.                 if (win)
  269.                 {
  270.                     
  271.                     if (soundOn == true)
  272.                         gamewin.Play();
  273.                     else
  274.                     {
  275.                         Controls.Add(youWin);
  276.                         youWin.BringToFront();
  277.                     }
  278.                 }
  279.             }

  280.             //主顯示圖形陣列被點擊時的方法
  281.             void pShow_click(object sender, EventArgs e)
  282.             {
  283.                 PictureBox p = (PictureBox)sender;
  284.                 whichPic(sender);//取得目前滑鼠點擊的格子是幾號格
  285.                 if (matched[whichOne] != true && time == 0 && isClicked[whichOne] == false)//如果點到圖格還未被matched,而且計時器的t已歸零,才能進行本次的翻牌
  286.                 {
  287.                     pShow[whichOne].Image = pRandom[whichOne].Image;//將原本的背卡圖換成本圖
  288.                     if (soundOn == true)
  289.                         flip.Play();

  290.                     temp();//進行翻牌後的方法

  291.                     //已翻了二次牌,如果二牌圖形相等,則二個格子所對應數字的matched都變成true
  292.                     if (temp1.Image == temp2.Image)
  293.                     {
  294.                         matched[tempN1] = true;
  295.                         matched[tempN2] = true;

  296.                         if (soundOn == true)
  297.                             correct.Play();

  298.                     }
  299.                     else if (flipCount == 2)//如果不相等而且翻牌的次數已是二次。(如果只有一次自然會不相等)
  300.                     {
  301.                         if (soundOn == true)
  302.                             error.Play();

  303.                         mytimer.Start();//計時開始,好讓被翻開但不相同牌色的二張卡都回到背卡狀態
  304.                     }
  305.                 }
  306.                 doYouWin();//每次都檢查你贏的條件是否達成,如果是就顯示訊息
  307.             }

  308.             //翻牌時進行的方法
  309.             void temp()
  310.             {
  311.                 if (temp1.Image != null && temp2.Image != null)//如果二個臨時圖都已有放圖了
  312.                 {
  313.                     //將翻牌次數以及二個臨時存放的圖格歸零
  314.                     flipCount = 0;
  315.                     temp1.Image = null;
  316.                     temp2.Image = null;
  317.                 }

  318.                 if (temp1.Image == null)//如果第一個臨時存圖格是空的
  319.                 {
  320.                     flipCount = 1;//翻牌次數算一次
  321.                     isClicked[whichOne] = true;
  322.                     temp1.Image = pShow[whichOne].Image;
  323.                     tempN1 = whichOne;
  324.                 }
  325.                 else//如果第一個已有圖檔存入,就放進第二個
  326.                 {
  327.                     flipCount = 2;//翻牌次數算第二次
  328.                     isClicked[whichOne] = true;
  329.                     temp2.Image = pShow[whichOne].Image;
  330.                     tempN2 = whichOne;
  331.                 }
  332.             }
  333.             //翻牌時進行的方法end

  334.             //被點擊的圖片是哪一格位

  335.             void whichPic(object sender)
  336.             {
  337.                 PictureBox p = (PictureBox)sender;
  338.                 for (int i = 0; i < cardNumber * 2; i++)
  339.                     if (p.Location == pShow[i].Location)
  340.                         whichOne = i;
  341.             }
  342.             //被點擊的圖片是哪一格位end

  343.             //---------------將圖片亂排 begin ---------------
  344.             void randomPicture()//先設立一個函數
  345.             {
  346.                 void randomPickNoRepeat(int[] array)
  347.                 {
  348.                     for (int i = 0; i < array.Length; i++)
  349.                     {
  350.                         array[i] = rnd.Next(array.Length);

  351.                         for (int j = 0; j < i; j++)
  352.                             if (array[j] == array[i])
  353.                             {
  354.                                 i--;
  355.                                 break;
  356.                             }
  357.                     }
  358.                 }

  359.                 //第一次亂數,每隔一格子插入一個圖片
  360.                 randomPickNoRepeat(array4random);
  361.                 for (int i = 0; i < cardNumber; i++)
  362.                 {
  363.                     pRandom[i * 2 + 1].Image = pImage[array4random[i]].Image;
  364.                 }

  365.                 //第二次亂數,每隔一格子插入一個圖片
  366.                 randomPickNoRepeat(array4random);
  367.                 for (int i = 0; i < cardNumber; i++)
  368.                 {
  369.                     pRandom[i * 2].Image = pImage[array4random[i]].Image;
  370.                 }
  371.             }
  372.             //----------將圖片亂排end-------------

  373.         }

  374.     }//---------public Form2() end ---------
  375. }
複製代碼
回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|手機版|Archiver|code.club  

GMT+8, 2024-4-20 22:52 , Processed in 0.098084 second(s), 16 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表