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         }

停止代码

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

原文地址:https://www.cnblogs.com/xuejie-shool/p/10915503.html

时间: 2024-10-10 14:31:33

c# 抽奖小程序的相关文章

Java抽奖小程序

package com.test; import java.awt.Color; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Random; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import

以前写的抽奖小程序,备忘下以备不时之需。

界面其丑无比.......很少写winform程序...... 链接: http://pan.baidu.com/s/1gdpnimf 密码: 49bd

基于vs2012的C# winform抽奖小程序的总结

哈希表的使用 Hashtable hashtable = new Hashtable(); hashtable.ContainsValue(tmp);//判断哈希表中有没有tmp hashtable.add(tmp,tmp);//加入哈希表 DataSet的使用 SqlDataAdapter sda = new SqlDataAdapter(sqlCount, myconn); DataSet ds = new DataSet(); sda.Fill(ds); 获取ds里的值 ds.Tables

一个简单的中奖抽奖小程序

<!DOCTYPE html> <html> <head> <title></title> <script type="text/javascript" src="jquery.js"></script> <style type="text/css"> #zhongjiang{ width: 100px; height: 100px; backgrou

用 python 写一个年会抽奖小程序

使用 pyinstaller 打包工具常用参数指南 pyinstaller -F demo.py 参数 含义 -F 指定打包后只生成一个exe格式的文件 -D –onedir 创建一个目录,包含exe文件,但会依赖很多文件(默认选项) -c –console, –nowindowed 使用控制台,无界面(默认选项) -w –windowed, –noconsole 使用窗口,无控制台 -p 添加搜索路径,让其找到对应的库. -i 改变生成程序的icon图标 pyinstaller -F -w -

微信小程序-整理各种小程序源码和资料免费下载

微信小程序整理下载 [小程序源码]微信小程序-车源宝微信版 [小程序源码]小程序-微赞社区(论坛demo) [小程序源码]微信小程序-收支账单 [小程序工具]微信小程序-日历 [小程序源码]小程序-在线聊天功能 [小程序源码]微信小程序-大好商城(新增功能天气查询和2048游戏) [小程序源码]微信小程序-查询号码归属地 [小程序源码]微信小程序-备忘录2 [小程序源码]微信小程序-QQ音乐 [小程序源码]小程序-货币汇率 [小程序源码]微信小程序-大学图书馆 [小程序源码]小程序-积分商城 [

微信小程序学习指南

作者:初雪链接:https://www.zhihu.com/question/50907897/answer/128494332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 微信小程序正式公测, 张小龙全面阐述小程序,定档1月9日上线(附90分钟演讲全文) ... 前言:新人第一坑,跳坑指南:修改后,必须保存:ctrl+S: 1:官方工具:https://mp.weixin.qq.com/debug/w ... tml?t=1476434678461 2:简易教

微信小程序开发之大转盘 仿天猫超市抽奖

天猫超市翻牌的转盘经常用,以前做Android,没啥想法,现在尝试微信小程序,看到别人家APP里有啥好玩的,就想去做一个. 上GIF看效果: 简要的说一下. 1.外面一圈闪烁的小球是用js控制的样式.500ms改变一次样式.简单粗暴; 2.抽奖的item也是js控制背景,但是怎么样让它优雅的停下来是个问题.动画中有timingFunction可以设置速度.自己用js就没那么简单了.我这里用setInterval(),时间是线性变化的.换个斜率先小后大的函数效果应该会好一些. 技术相关: 1.微信

用微信小程序开发的Canvas绘制可配置的转盘抽奖

使用https://github.com/givebest/GB-canvas-turntable代码移植过而来. 其它 微信小程序感觉是个半成品,代码移植过程比较繁琐麻烦.canvas API 部分都被重写了...canvas z-index不生效,永远在最上层,不支持rotate动画. 更多:点击打开链接