本文共 4197 字,大约阅读时间需要 13 分钟。
今天微信这边开始做游戏了,感觉挺好玩儿的,我被分到了一个砸金蛋的游戏。先看下实现后的效果:
这个游戏因为不仅涉及到前台的JS等等,还要从后台获取数据,比如,设置的概率什么的,奖品数量。
ok,上源码:
引入的CSS:
锤子
- 1
- 2
- 3
.egg{width:660px; height:400px; margin:50px auto 20px auto;} .egg ul li{z-index:999;} .eggList{padding-top:110px;position:relative;width:660px;} .eggList li{float:left;background:url(../images/egg_1.png) no-repeat bottom;width:158px; height:187px;cursor:pointer;position:relative;margin-left:35px;} .eggList li span{position:absolute; width:30px; height:60px; left:68px; top:64px; color:#ff0; font-size:42px; font-weight:bold} .eggList li.curr{background:url(../images/egg_2.png) no-repeat bottom;cursor:default;z-index:300;} .eggList li.curr sup{position:absolute;background:url(../images/img-4.png) no-repeat;width:232px; height:181px;top:-36px;left:-34px;z-index:800;} .hammer{background:url(../images/img-6.png) no-repeat;width:74px;height:87px;position:absolute; text-indent:-9999px;z-index:150;left:168px;top:100px;} .resultTip{position:absolute; background:#ffc ;width:148px;padding:6px;z-index:500;top:200px; left:10px; color:#f60; text-align:center;overflow:hidden;display:none;z-index:500;} .resultTip b{font-size:14px;line-height:24px;}
images文件:
后台处理:
public class setSettings2 : IHttpHandler { private double Chance = 0.9;//中奖概率 public ListGetDataFromDB() { #region 模拟读到的数据库的奖项级别 List prices = new List () { new PriceInfo(){ GotPriceChance=float.Parse("1"), PriceLevel=1, PriceLevelName="一等奖", PriceNum=100}, new PriceInfo(){ GotPriceChance=float.Parse("1"), PriceLevel=2, PriceLevelName="二等奖", PriceNum=100}, new PriceInfo(){ GotPriceChance=float.Parse("1"), PriceLevel=3, PriceLevelName="三等奖", PriceNum=100} }; #endregion return prices; } public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string strResult = "不好意思,您没有获奖,么么哒~~~~"; //返回给用户的抽奖结果 int intTotal = 0; //获取总抽奖次数 GetDataFromDB().ForEach(item => { intTotal += item.PriceNum; }); int intResult = this.GetRandom((int)(intTotal/Chance)); //生成一个用户的随机整数 this.GetPrice(GetDataFromDB()).ForEach(item => //对奖项集合进行变量并于用户的随机整数对比 { if (intResult == item.PriceLevel) { strResult = item.PriceLevelName; //奖项等级名称赋值 //抽到的奖项的数量减一——此处要修改数据库 context.Response.Write(strResult); context.Response.End(); } }); context.Response.Write(strResult); } /// /// 判断中了几等奖 /// /// ///public List GetPrice(List priceInfos) { List pricelist = new List (); priceInfos.ForEach(p => { int j = 0; for (int i = 0; i < p.PriceNum; i++) { pricelist.Add(new PriceInfo() { GotPriceChance = p.GotPriceChance, PriceLevel = ++j,//level里面放置编号 PriceLevelName = p.PriceLevelName, PriceNum = p.PriceNum }); } }); return pricelist; } /// /// 生成指定数量内的随机数字 /// /// ///public int GetRandom(int maxNum) { Random ran = new Random(); ran.Next(1, 100); int intResult = ran.Next(1, maxNum + 1); return intResult; } public bool IsReusable { get { return false; } } } /// /// 奖品信息 /// public class PriceInfo { public float GotPriceChance { get; set; }//中奖概率:例如:50% public int PriceLevel { get; set; }//中奖级别:例如:1 public string PriceLevelName { get; set; }//中奖级别名称:例如,一等奖 public int PriceNum { get; set; }//奖品数量 :例如:100 }
唉,又找回了当年疯狂粘人代码的赶脚。。。