微信活动——砸金蛋

简介:                   今天微信这边开始做游戏了,感觉挺好玩儿的,我被分到了一个砸金蛋的游戏。先看下实现后的效果:            这个游戏因为不仅涉及到前台的JS等等,还要从后台获取数据,比如,设置的概率什么的,奖品数量。


        

         今天微信这边开始做游戏了,感觉挺好玩儿的,我被分到了一个砸金蛋的游戏。先看下实现后的效果:



       



   这个游戏因为不仅涉及到前台的JS等等,还要从后台获取数据,比如,设置的概率什么的,奖品数量。


   ok,上源码:


 

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link href="Styles/egg.css" rel="stylesheet" type="text/css" />
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>



</head>

<body>

<div id="main">
  	
	<div class="egg">
		<ul class="eggList">
			<p class="hammer" id="hammer">锤子</p>
			<p class="resultTip" id="resultTip"><b id="result"></b></p>
			<li><span>1</span><sup></sup></li>
			<li><span>2</span><sup></sup></li>
			<li><span>3</span><sup></sup></li>
		</ul>
	</div>
	
</div>

<script type="text/javascript">

    var flag = false;

    function eggClick(obj) {
        var _this = obj;
        $.get("setSettings2.ashx", function (res) {

            if (_this.hasClass("curr")) {
                alert("蛋都碎了,别砸了!刷新再来.");
                return false;
            }

            $(".hammer").css({ "top": _this.position().top - 55, "left": _this.position().left + 185 });
            $(".hammer").animate({
                "top": _this.position().top - 25,
                "left": _this.position().left + 125
            }, 30, function () {
                _this.addClass("curr"); //蛋碎效果
                _this.find("sup").show(); //金花四溅
                $(".hammer").hide();
                $('.resultTip').css({ display: 'block', top: '100px', left: _this.position().left + 45, opacity: 0 }).animate({ top: '50px', opacity: 1 }, 300, function () {

                    //                    if (res.msg == 1) {
                    //                        $("#result").html("恭喜,您中得" + res.prize + "!");
                    //                    } else {
                    //                        $("#result").html("很遗憾,您没能中奖!");
                    //                    }
                    $("#result").html(res);
                });
            }
		);
        });
    }


    $(".eggList li").click(function () {
        if (flag == false) {
            $(this).children("span").hide();
            eggClick($(this));
            flag = true;
        } else {
            $("#result").html("您已经砸过一次了!!!!!");
        }
    });

    $(".eggList li").hover(function () {
        var posL = $(this).position().left + $(this).width();
        $("#hammer").show().css('left', posL);
    });



</script>



</body>
</html>

引入的CSS:


.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 List<PriceInfo> GetDataFromDB()
        {
            #region 模拟读到的数据库的奖项级别

            List<PriceInfo> prices = new List<PriceInfo>() { 
            
                        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);

        }

        /// <summary>
        /// 判断中了几等奖
        /// </summary>
        /// <param name="priceInfo"></param>
        /// <returns></returns>
        public List<PriceInfo> GetPrice(List<PriceInfo> priceInfos)
        {
            List<PriceInfo> pricelist = new List<PriceInfo>();
            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;

        } 
        


        /// <summary>
        /// 生成指定数量内的随机数字
        /// </summary>
        /// <param name="maxNum"></param>
        /// <returns></returns>
        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;
            }
        }


    }

    /// <summary>
    /// 奖品信息
    /// </summary>
    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

    }




        唉,又找回了当年疯狂粘人代码的赶脚。。。




           

   

目录
相关文章
|
JavaScript 前端开发 PHP
微信公众平台开发(53)砸金蛋
微信公众平台开发 微信公众平台开发模式 企业微信公众平台 砸金蛋 作者:方倍工作室 地址:http://www.cnblogs.com/txw1958/p/crack-golden-eggs.html 砸金蛋被广泛应用于庆典活动、商家促销、电视娱乐等场合,它的趣味、悬念能迅速活跃现场气氛。
1286 0
|
13天前
|
小程序 前端开发 API
微信小程序全栈开发中的异常处理与日志记录
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的异常处理和日志记录,强调其对确保应用稳定性和用户体验的重要性。异常处理涵盖前端(网络、页面跳转、用户输入、逻辑异常)和后端(数据库、API、业务逻辑)方面;日志记录则关注关键操作和异常情况的追踪。实践中,前端可利用try-catch处理异常,后端借助日志框架记录异常,同时采用集中式日志管理工具提升分析效率。开发者应注意安全性、性能和团队协作,以优化异常处理与日志记录流程。
|
13天前
|
小程序 安全 数据安全/隐私保护
微信小程序全栈开发中的身份认证与授权机制
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的身份认证与授权机制。身份认证包括手机号验证、微信登录和第三方登录,而授权机制涉及角色权限控制、ACL和OAuth 2.0。实践中,开发者可利用微信登录获取用户信息,集成第三方登录,以及实施角色和ACL进行权限控制。注意点包括安全性、用户体验和合规性,以保障小程序的安全运行和良好体验。通过这些方法,开发者能有效掌握小程序全栈开发技术。
|
13天前
|
JavaScript 前端开发 小程序
微信小程序全栈开发之性能优化策略
【4月更文挑战第12天】本文探讨了微信小程序全栈开发的性能优化策略,包括前端的资源和渲染优化,如图片压缩、虚拟DOM、代码分割;后端的数据库和API优化,如索引创建、缓存使用、RESTful API设计;以及服务器的负载均衡和CDN加速。通过这些方法,开发者可提升小程序性能,优化用户体验,增强商业价值。
|
13天前
|
小程序 前端开发 JavaScript
微信小程序全栈开发中的PWA技术应用
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中PWA技术的应用,PWA结合Web的开放性和原生应用的性能,提供离线访问、后台运行、桌面图标和原生体验。开发者可利用Service Worker实现离线访问,Worker处理后台运行,Web App Manifest添加桌面图标,CSS和JavaScript提升原生体验。实践中需注意兼容性、性能优化和用户体验。PWA技术能提升小程序的性能和用户体验,助力开发者打造优质小程序。
|
1月前
|
小程序 前端开发 程序员
微信小程序开发入门教程-小程序账号注册及开通
微信小程序开发入门教程-小程序账号注册及开通
|
2天前
|
数据采集 存储 人工智能
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
【Python+微信】【企业微信开发入坑指北】4. 企业微信接入GPT,只需一个URL,自动获取文章总结
12 0
|
2天前
|
人工智能 机器人 API
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
【Python+微信】【企业微信开发入坑指北】3. 如何利用企业微信API给微信群推送消息
4 0
|
2天前
|
缓存 人工智能 API
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
【Python+微信】【企业微信开发入坑指北】2. 如何利用企业微信API主动给用户发应用消息
6 0
|
13天前
|
SQL 安全 小程序
探索微信小程序全栈开发的安全性问题
【4月更文挑战第12天】本文探讨了微信小程序全栈开发中的安全性问题,包括数据安全、接口安全、隐私保护和代码安全。为解决这些问题,建议采取数据加密、使用HTTPS协议、身份认证与授权、输入验证、安全审计及漏洞扫描以及安全培训等措施。通过这些方法,开发者可提升小程序安全性,保护用户隐私和数据。

热门文章

最新文章