c#自制抽奖小程序 - 不长记性的脑子、

时间:2024-01-24 10:15:18

c#自制抽奖小程序

        #region 第一部分界面设计
        const int N = 8;
        Button button = new Button();
        Image[] images = new Image[N];
        PictureBox[] pictures = new PictureBox[N];
        int[] dx = new int[N] { 112, 112, 0, 0, -113, -113, 0, 0 };
        int[] dy = new int[N] { 0, 0, 113, 113, 0, 0, -112, -112 };
        private void Form1_Load(object sender, EventArgs e)
        {
            InitialImage();
            InitialSurface();//初始化窗体界面
        }
        //添加图片
        private void InitialImage()
        {
            images[0] = Resources.保温杯;
            images[1] = Resources.体重秤;
            images[2] = Resources.x1;
            images[3] = Resources.台灯;
            images[4] = Resources.耳机;
            images[5] = Resources.x2;
            images[6] = Resources.电动牙刷;
            images[7] = Resources.时钟;
            this.BackgroundImage = Resources.formbg;
        }
        //初始化界面
        private void InitialSurface()
        {
            Panel pa = new Panel();
            pa.Size = new System.Drawing.Size(342, 348);
            pa.BackgroundImage = Resources.bga1;
            pa.BackgroundImageLayout = ImageLayout.Stretch;
            pa.Location = new Point(91, 316);
            pa.BackColor = Color.Transparent;
            pa.TabIndex = 0;
            this.Controls.Add(pa);
            int x = 11, y = 11;
            for (int i = 0; i < N; i++)
            {
                x += dx[i];
                y += dy[i];
                PictureBox box = new PictureBox();
                box.Size = new Size(95, 95);
                box.Location = new Point(x, y);
                box.Image = images[i];
                box.SizeMode = PictureBoxSizeMode.StretchImage;
                box.BorderStyle = BorderStyle.FixedSingle;
                box.Padding = new Padding(4);
                box.BackColor = Color.White;
                pictures[i] = box;
                pa.Controls.Add(box);
            }
            button.Text = "开始" + "\r\n" + "抽奖";
            button.Font = new Font("宋体", 14, FontStyle.Bold);
            button.Size = new Size(97, 97);
            button.Location = new Point(123, 124);
            button.Click += Button_Click;
            pa.Controls.Add(button);

            PictureBox box1 = new PictureBox();
            box1.Image = Resources.bgGIF;
            box1.SizeMode = PictureBoxSizeMode.StretchImage;
            box1.BorderStyle = BorderStyle.None;
            box1.Size = new Size(395, 400);
            box1.Location = new Point(64, 290);
            this.Controls.Add(box1);
            this.MaximizeBox = false;
            this.BackgroundImageLayout = ImageLayout.Stretch;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
        } 
        #endregion
第一部分界面设计

界面效果如下:

实现旋转是使用的改变控件大小,背景颜色(把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         }
停止代码

简单的旋转程序就完成了。