1. 程式人生 > >c# 抽獎小程式

c# 抽獎小程式

 1 const int N = 8;
 2         Button button = new Button();
 3         Image[] images = new Image[N];
 4         PictureBox[] pictures = new PictureBox[N];
 5         int[] dx = new int[N] { 112, 112, 0, 0, -113, -113, 0, 0 };
 6         int[] dy = new int[N] { 0, 0, 113, 113, 0, 0, -112, -112 };
 7         private void Form1_Load(object sender, EventArgs e)
 8         {
 9             InitialImage();
10             InitialSurface();//初始化窗體介面
11         }
12         //新增圖片
13         private void InitialImage()
14         {
15             images[0] = Resources.保溫杯;
16             images[1] = Resources.體重秤;
17             images[2] = Resources.x1;
18             images[3] = Resources.檯燈;
19             images[4] = Resources.耳機;
20             images[5] = Resources.x2;
21             images[6] = Resources.電動牙刷;
22             images[7] = Resources.時鐘;
23             this.BackgroundImage = Resources.formbg3;
24         }
25         //初始化介面
26         private void InitialSurface()
27         {
28             this.Size = new Size(537, 760);
29             Panel pa = new Panel();
30             pa.Size = new System.Drawing.Size(342, 348);
31             pa.BackgroundImage = Resources.bga1;
32             pa.BackgroundImageLayout = ImageLayout.Stretch;
33             pa.Location = new Point((int)(this.Width * 0.17), (int)(this.Height * 0.418));//90, 315);
34             pa.BackColor = Color.Transparent;
35             this.Controls.Add(pa);
36             int x = 11, y = 11;
37             for (int i = 0; i < N; i++)
38             {
39                 x += dx[i];
40                 y += dy[i];
41                 PictureBox box = new PictureBox();
42                 box.Size = new Size(95, 95);
43                 box.Location = new Point(x, y);
44                 box.Image = images[i];
45                 box.SizeMode = PictureBoxSizeMode.StretchImage;
46                 box.BorderStyle = BorderStyle.FixedSingle;
47                 box.Padding = new Padding(4);
48                 box.BackColor = Color.White;
49                 pictures[i] = box;
50                 pa.Controls.Add(box);
51             }
52             button.Text = "開始" + "\r\n" + "抽獎";
53             button.Font = new Font("宋體", 14, FontStyle.Bold);
54             button.Size = new Size(97, 97);
55             button.Location = new Point((int)(pa.Width * 0.36), (int)(pa.Height * 0.36));//123, 124);
56             button.Click += Button_Click;
57             pa.Controls.Add(button);
58 
59             PictureBox box0 = new PictureBox();
60             box0.Location = new Point(0, 0);
61             box0.Image = Resources.抽獎;
62             box0.SizeMode = PictureBoxSizeMode.StretchImage;
63             box0.Size = new Size(this.Width, (int)(this.Height * 0.30));//220);
64             box0.BorderStyle = BorderStyle.None;
65             this.Controls.Add(box0);
66 
67             PictureBox box1 = new PictureBox();
68             box1.Image = Resources.bgGIF;
69             box1.SizeMode = PictureBoxSizeMode.StretchImage;
70             box1.BorderStyle = BorderStyle.None;
71             box1.Size = new Size(395, 400);
72             box1.Location = new Point((int)(this.Width * 0.12), (int)(this.Height * 0.38));//64, 290);
73 
74 
75 
76             Label probability = new Label();
77             probability.BackColor = Color.Transparent;
78             probability.Size = new System.Drawing.Size(500,20);
79             probability.Text = "獎品概率: 保溫杯7% 電動牙刷7% 體重秤4% 時鐘4% 檯燈3% 耳機3% ";
80             probability.Font = new System.Drawing.Font("宋體", 10, FontStyle.Regular);
81             probability.Location = new Point((int)(this.Width * 0.10), (int)(this.Height * 0.30));
82             this.Controls.Add(probability);
83             this.Controls.Add(box1);
84             this.MaximizeBox = false;
85             this.BackgroundImageLayout = ImageLayout.None;
86             this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
87         }
初始化程式碼

介面效果如下:

實現旋轉是使用的改變控制元件大小,背景顏色(把padding設定4可以看見背景顏色)。

 1 Random random = new Random();
 2         SoundPlayer sp = new SoundPlayer(Resources.Untitled_Project);
 3         int circle;
 4         int index;
 5         int stopNum;
 6         bool flag = true;//奇數和偶數的標誌,為true表示偶數
 7         private void Button_Click(object sender, EventArgs e)
 8         {
 9             circle = random.Next(5, 8);//隨機產生旋轉的圈數
10             stopNum = random.Next(0, 8);//隨機確定選擇抽中獎品的下標
11             button.Enabled = false;
12             time.Interval = 10;
13             index = 0;
14             flag = true;
15             sp.Play();
16             time.Start();
17         }
18 
19         private void time_Tick(object sender, EventArgs e)
20         {
21             if (flag)//放大picturebox,實現轉動效果
22             {
23                 pictures[index].Size = new Size(103, 103);
24                 pictures[index].BackColor = Color.YellowGreen;
25                 flag = false;
26             }
27             else if (flag == false)//將原來的picturebox縮放回原來的尺寸
28             {
29                 pictures[index].Size = new Size(95, 95);
30                 pictures[index].BackColor = Color.White;
31                 index++;
32                 flag = true;
33             }
34             if (index == 8)
35             {
36                 index = 0;
37                 circle -= circle > 0 ? 1 : 0;//每次轉動8次,旋轉的圈數-1
38             }
39             //倒數兩圈慢
40             time.Interval = circle > 2 ? 10 : 100;
41             if (circle == 0 && index == stopNum)
42             {
43                 pictures[index].Size = new Size(103, 103);
44                 pictures[index].BackColor = Color.YellowGreen;
45                 pictures[index].Location = new Point(pictures[index].Location.X - 4, pictures[index].Location.Y - 4);
46                 StopRotate(index);
47                 pictures[index].Size = new Size(95, 95);
48                 pictures[index].BackColor = Color.White;
49                 pictures[index].Location = new Point(pictures[index].Location.X + 4, pictures[index].Location.Y + 4);
50             }
51         }
旋轉程式碼

停止再加上一個簡單的概率會有點不了不流暢。

 1  private void StopRotate(int sum)
 2         {
 3             string message = null;
 4             switch (index)
 5             {
 6                 case 0: if (random.Next(0, 2) == stopNum) message = "恭喜獲取保溫杯一個"; else { circle = 1; stopNum = 2; } break;//1/2  14  7%
 7                 case 1: if (random.Next(0, 3) == stopNum) message = "恭喜獲取體重秤一個"; else { circle = 1; stopNum = 5; } break;//1/3  21  4%
 8                 case 2: message = "謝謝惠顧"; break;
 9                 case 3: if (random.Next(3, 7) == stopNum) message = "恭喜獲取檯燈一個"; else { circle = 1; stopNum = 5; } break;//1/4   28   3%
10                 case 4: if (random.Next(0, 5) == stopNum) message = "恭喜獲取耳機一個"; else { circle = 1; stopNum = 5; } break;//1/4  28   3%
11                 case 5: message = "謝謝惠顧"; break;
12                 case 6: if (random.Next(6, 8) == stopNum) message = "恭喜獲取電動牙刷一個"; else { circle = 1; stopNum = 2; } break;//1/2  1/14   7&
13                 case 7: if (random.Next(7, 10) == stopNum) message = "恭喜獲取時鐘一個"; else { circle = 1; stopNum = 2; } break;//1/3   1/21    4%
14             }
15             if (message != null)
16             {
17                 sp.Stop();
18                 time.Stop();//先關閉定時器
19                 MessageBox.Show(message);
20                 this.button.Enabled = true;
21             }
22         }
停止程式碼

簡單的旋轉程式就完成