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