1.关于本文dom
本文旨在模拟一种很是坑爹的抽奖形式-不等几率的抽奖(固然,这种抽奖形式在如今已经获得了很是广泛的应用)函数
程序下载地址:http://pan.baidu.com/s/1hETHkspa
2.抽奖系统抽奖规则code
对外的宣传是:一共有5个抽奖图标,用鼠标点击一个图标视为抽奖一次,该图标所对应的奖品会被翻出。抽奖总共有3次机会,第三次抽奖完毕后,亮出全部图标后面所表明的奖品。orm
实现的方法是:一共有5个抽奖图标,用鼠标点击一个图标视为抽奖一次,计算出一个通常奖品。抽奖总共有3次机会,第三次抽奖完毕后,将其他两个图标亮出优质奖品对应的图片。图片
下面是一张程序运行时的截图(抽奖抽到的东西为何老是这么不堪入目,看下代码就明白了~~)资源
3.图片资源get
本程序的资源都是PNG格式的图片,存储在Resource.resx中。这些图片都是从52poke上下载的。在本程序中,A00-A07被认为是优质奖品,Z00-Z04被认为是通常奖品。it
4.控件描述io
下图为本程序用的控件描述
5.程序代码
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; namespace LuckyDraw { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //抽奖次数统计 int iTimes = 0; //程序初始化 private void Form1_Load(object sender, EventArgs e) { //抽奖次数:0次 iTimes = 0; //初始化图片:精灵球 pcb00.Image = Properties.Resources.Ball; pcb00.Image.Tag = "Ball"; pcb01.Image = Properties.Resources.Ball; pcb01.Image.Tag = "Ball"; pcb02.Image = Properties.Resources.Ball; pcb02.Image.Tag = "Ball"; pcb03.Image = Properties.Resources.Ball; pcb03.Image.Tag = "Ball"; pcb04.Image = Properties.Resources.Ball; pcb04.Image.Tag = "Ball"; pcb00.Click += PictureClick; pcb01.Click += PictureClick; pcb02.Click += PictureClick; pcb03.Click += PictureClick; pcb04.Click += PictureClick; } //单击“开始抽奖”按钮 private void btnStart_Click(object sender, EventArgs e) { //抽奖次数:0次 iTimes = 0; //从新开始一局,初始化图片 pcb00.Image = Properties.Resources.Ball; pcb00.Image.Tag = "Ball"; pcb01.Image = Properties.Resources.Ball; pcb01.Image.Tag = "Ball"; pcb02.Image = Properties.Resources.Ball; pcb02.Image.Tag = "Ball"; pcb03.Image = Properties.Resources.Ball; pcb03.Image.Tag = "Ball"; pcb04.Image = Properties.Resources.Ball; pcb04.Image.Tag = "Ball"; } //单击一张图片 private void PictureClick(object sender, EventArgs e) { //若是已经用尽3次机会,则不触发后面的行为 if (iTimes >= 3) return; //已经翻出的牌不会再被翻出 if (((PictureBox)sender).Image.Tag != "Ball" as object) return; //翻一张牌 ((PictureBox)sender).Image = SelectPicture(false); //在翻牌位置绘制一个矩形 Graphics g = Graphics.FromImage(((PictureBox)sender).Image); g.DrawLines(Pens.Red, new Point[] { new Point(10, 10), new Point(90, 10), new Point(90, 90), new Point(10, 90), new Point(10, 10) }); g.Save(); //抽奖满三次,翻出全部牌 if (++iTimes == 3) { if (pcb00.Image.Tag == "Ball" as object) pcb00.Image = SelectPicture(true); if (pcb01.Image.Tag == "Ball" as object) pcb01.Image = SelectPicture(true); if (pcb02.Image.Tag == "Ball" as object) pcb02.Image = SelectPicture(true); if (pcb03.Image.Tag == "Ball" as object) pcb03.Image = SelectPicture(true); if (pcb04.Image.Tag == "Ball" as object) pcb04.Image = SelectPicture(true); MessageBox.Show("抽到了这么多只好精灵,这下真是赚到了!"); } } /// <summary> /// 随机数:供函数 SelectPicture 使用 /// </summary> Random rd = new Random(DateTime.Now.Millisecond); /// <summary> /// 随机返回一张精灵图片 /// </summary> /// <param name="isA">true:高级精灵A00-A07 false:低级精灵Z00-Z04</param> /// <returns></returns> private Image SelectPicture(bool isA) { Image result; if (isA) { switch (rd.Next(0, 8)) { case 0: result = Properties.Resources.A00; result.Tag = "A00"; return result; case 1: result = Properties.Resources.A01; result.Tag = "A01"; return result; case 2: result = Properties.Resources.A02; result.Tag = "A02"; return result; case 3: result = Properties.Resources.A03; result.Tag = "A03"; return result; case 4: result = Properties.Resources.A04; result.Tag = "A04"; return result; case 5: result = Properties.Resources.A05; result.Tag = "A05"; return result; case 6: result = Properties.Resources.A06; result.Tag = "A06"; return result; case 7: result = Properties.Resources.A07; result.Tag = "A07"; return result; } } else { switch (rd.Next(0, 5)) { case 0: result = Properties.Resources.Z00; result.Tag = "Z00"; return result; case 1: result = Properties.Resources.Z01; result.Tag = "Z01"; return result; case 2: result = Properties.Resources.Z02; result.Tag = "Z02"; return result; case 3: result = Properties.Resources.Z03; result.Tag = "Z03"; return result; case 4: result = Properties.Resources.Z04; result.Tag = "Z04"; return result; } } return null; } } }
END