本帖最後由 csharp 於 2021-6-30 13:35 編輯
nothing
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Media;
- namespace WindowsFormsApp28
- {
- public partial class Form1 : Form
- {
- Panel panel = new Panel();
- PictureBox[] pb = new PictureBox[12];
- PictureBox[] pb1 = new PictureBox[12];
- PictureBox[] pb2 = new PictureBox[12];
- PictureBox[] pb3 = new PictureBox[12];
- PictureBox[] pb4 = new PictureBox[12];
- Label[] table = new Label[12];
- PictureBox[] pbIcon = new PictureBox[4];
- SoundPlayer touch = new SoundPlayer(Properties.Resources.quick000);
- SoundPlayer ending = new SoundPlayer(Properties.Resources.ending001);
- int oldX, oldY;
- int[] ary = new int[12];
- bool isDown;
- bool[] matched = new bool[12];
- PictureBox howto = new PictureBox();
-
- public Form1()
- {
- InitializeComponent();
-
- setPanel();
- setIcon();
- setTable();
- setPicturebox();
- howto.Image = Properties.Resources.howtoplay;
- howto.Location = new System.Drawing.Point(Width / 3 * 2, 15);
- howto.Size = new Size(120,20);
- panel.Controls.Add(howto);
-
- }
-
- void setPanel()
- {
- Rectangle screen = Screen.FromControl(this).Bounds;//這三行是求得整個電腦視窗的長寬值
- Width = screen.Width;//這三行是求得整個電腦視窗的長寬值
- Height = screen.Height;//這三行是求得整個電腦視窗的長寬值
-
- panel = new Panel();
- panel.Location = new System.Drawing.Point(35, 5);
- panel.Size = new Size(Width / 10 * 9, Height / 100 * 95);
- //panel.BackgroundImage = Properties.Resources._111;
- //panel.BackgroundImageLayout = ImageLayout.Stretch;
- panel.BackColor = Color.AliceBlue;
- Controls.Add(panel);
- }
- void setPicturebox()
- {
- for(int i = 0; i < 12; i++)
- {
- //先將物件陣列實體化
- pb[i] = new PictureBox();
- pb1[i] = new PictureBox();
- pb2[i] = new PictureBox();
- pb3[i] = new PictureBox();
- pb4[i] = new PictureBox();
- //將各圖檔尺寸定出來
- pb[i].Size = new Size(100, 100);
- pb1[i].Size = new Size(100, 100);
- pb2[i].Size = new Size(100, 100);
- pb3[i].Size = new Size(100, 100);
- pb4[i].Size = new Size(100, 100);
- //除掉主要的陣列之外,將其它陣列給予圖片
- pb1[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("a"+i);
-
- pb2[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("b"+i);
-
- pb3[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("d"+i);
-
- pb4[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("k"+i);
-
- //將主要陣列賦予位置,並且將其置入panel中
- pb[i].Location = new System.Drawing.Point(30+i%6*110,325+i/6*103);
- pb[i].BackColor = Color.Transparent;
- panel.Controls.Add(pb[i]);
-
- pb[i].MouseDown += new MouseEventHandler(pb_mousedown);
- pb[i].MouseMove += new MouseEventHandler(pb_mousemove);
- pb[i].MouseUp += new MouseEventHandler(pb_mouseup);
- }
- }
-
- void pb_mousedown(object sender, MouseEventArgs e)
- {
- isDown = true;
- oldX = e.X;
- oldY = e.Y;
- }
- void pb_mousemove(object sender, MouseEventArgs e)
- {
- PictureBox pb = (PictureBox)sender;
- if (isDown)
- {
- pb.Left += e.X - oldX;
- pb.Top += e.Y - oldY;
- pb.BringToFront();
- }
- }
- void pb_mouseup(object sender, MouseEventArgs e)
- {
- isDown = false;
- closeToBorder(sender, table);
-
- isThisPictureCorrect(pb, table);
-
- if (allMatched())
- {
- for (int i = 0; i < 12; i++)
- {
- pb[i].Enabled = false;
- }
- ending.Play();
- }
- }
- void setIcon()
- {
- for(int i = 0; i < 4; i++)
- {
- pbIcon[i] = new PictureBox();
- pbIcon[i].Image = (Image)Properties.Resources.ResourceManager.GetObject("icon" + (i + 1));
- pbIcon[i].Size = new Size(120, 90);
- pbIcon[i].SizeMode = PictureBoxSizeMode.StretchImage;
- pbIcon[i].Location = new System.Drawing.Point(Width/3*2,20+i*110+30);
- panel.Controls.Add(pbIcon[i]);
-
- pbIcon[i].Click += new System.EventHandler(pb_click);
- }
- }
-
- void pb_click(object sender, EventArgs e)
- {
- PictureBox p = (PictureBox)sender;
- pbRestart();
- if (p == pbIcon[0])
- randomPb(pb, pb1);
- else if (p == pbIcon[1])
- randomPb(pb, pb2);
- else if (p == pbIcon[2])
- randomPb(pb, pb3);
- else if (p == pbIcon[3])
- randomPb(pb, pb4);
- }
-
- void setTable()
- {
- for (int i = 11; i >= 0; i--)
- {
- table[i] = new Label();
- table[i].Size = new Size(100, 100);
- table[i].BorderStyle = BorderStyle.FixedSingle;
- table[i].BackColor = Color.Transparent;
- table[i].Location = new System.Drawing.Point(150+(11 - i) % 4 * 100,10+ (11 - i) / 4 * 100);
- panel.Controls.Add(table[i]);
-
- }
- }
- void closeToBorder(object sender, Label[] lbl)
- {
- PictureBox p = (PictureBox)sender;
- for (int i = 0; i < 12; i++)
- {
- if (Math.Abs(p.Left - lbl[i].Left) < 30 && Math.Abs(p.Top - lbl[i].Top) < 30)
- {
- p.Left = lbl[i].Left;
- p.Top = lbl[i].Top;
- touch.Play();
-
- }
- }
- }
-
-
- void isThisPictureCorrect(PictureBox[] p, Label[] lbl)
- {
- for (int i = 0; i < 12; i++)
- for(int j=0;j<12;j++)
- {
- if ((p[i].Top == lbl[j].Top) && (p[i].Left == lbl[j].Left)&&(pbValue[i]==j))
- matched[i] = true;
- else matched[i] = false;
- }
- }
-
- bool allMatched()
- { bool ok=true;
- for (int i = 0; i < 12; i++)
- {
- if (matched[i] == false) ok== false;
- }
- return ok;
- }
-
- void randomPickNoRepeat(int[] a)
- {
- Random rnd = new Random();
- for (int i = 0; i < a.Length; i++)
- {
- a[i] = rnd.Next(a.Length);
-
- for (int j = 0; j < i; j++)
- if (a[j] == a[i])
- {
-
- i--;
- break;
- }
- }
- }
- void pbRestart()
- {
- for (int i = 0; i < 12; i++)
- {
- pb[i].Enabled = true;
- pb[i].Location = new System.Drawing.Point(30 + i % 6 * 110, 330 + i / 6 * 103);
- }
- }
- int[] a = new int[12] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
- int[] pbValue = new int[12];
- void randomPb(PictureBox[] pb, PictureBox[] p1)
- {
-
- randomPickNoRepeat(a);
-
- for (int i = 0; i < 12; i++)
- {
- pb[i].Image = p1[a[i]].Image;
- pbValue[i]=a[i];
-
-
- }
- }
- }
- }
複製代碼 |