PHP幸運大轉盤源碼,支援ThinkPHP,大轉盤thinkphp
HTML代碼
代碼是基於thinkphp的,如果你的不是,修改裡面的url路徑就行了
- charset="utf-8">
- name="keywords"content="幸運大轉盤,cnsecer.com"/>
- name="description"content="幸運大轉盤"/>
- 幸運大轉盤
- type="text/css">
- .demo{width:417px; height:417px; position:relative; margin:50pxauto}
- #disk{width:417px; height:417px; background:url(__STATIC__/images/disk.jpg) no-repeat}
- #start{width:163px; height:320px; position:absolute; top:46px; left:130px;}
- #start img{cursor:pointer}
- src="http://cdn.bootcss.com/jquery/1.11.0/jquery.min.js">
- type="text/javascript"src="__STATIC__/js/jQueryRotate.2.2.js">
- type="text/javascript"src="__STATIC__/js/jquery.easing.min.js">
- type="text/javascript">
- $(function(){
- $("#startbtn").click(function(){
- lottery();
- });
- });
- function lottery(){
- $.ajax({
- type:'POST',
- url:'{:U(\'game/run\')}',//提交地址 改為你自己的
- dataType:'json',
- cache:false,
- error:function(){
- alert('出錯了!');
- returnfalse;
- },
- success:function(json){
- $("#startbtn").unbind('click').css("cursor","default");
- var a = json.angle;//角度
- var p = json.prize;//獎項
- $("#startbtn").rotate({
- duration:3000,//轉動時間
- angle:0,
- animateTo:1800+a,//轉動角度
- easing: $.easing.easeOutSine,
- callback:function(){
- var con = confirm('恭喜你,中得'+p+'\n還要再來一次嗎?');
- if(con){
- lottery();
- }else{
- returnfalse;
- }
- }
- });
- }
- });
- }
- id="main">
- class="msg">
- class="demo">
- id="disk">
- id="start">src="__STATIC__/images/start.png"id="startbtn">
- id="footer">
幕後處理代碼
- php
- namespaceHome\Controller;
- useThink\Controller;
- classGameControllerextendsBaseController{
- publicfunction index(){
- $this->display("index");
- }
- publicfunction run(){
- $proArr = array();
- //v 是中獎機率 相加之後最好為100的整數倍 id為獎品編號 min max 分別為最大和最小角度
- $prize_arr = array(
- '0'=> array('id'=>1,'min'=>1,'max'=>29,'prize'=>'一等獎','v'=>0),
- '1'=> array('id'=>2,'min'=>302,'max'=>328,'prize'=>'二等獎','v'=>0),
- '2'=> array('id'=>3,'min'=>242,'max'=>268,'prize'=>'三等獎','v'=>0),
- '3'=> array('id'=>4,'min'=>182,'max'=>208,'prize'=>'四等獎','v'=>0),
- '4'=> array('id'=>5,'min'=>122,'max'=>148,'prize'=>'五等獎','v'=>0),
- '5'=> array('id'=>6,'min'=>62,'max'=>88,'prize'=>'六等獎','v'=>0),
- '6'=> array('id'=>7,'min'=>array(32,92,152,212,272,332),'max'=>array(58,118,178,238,298,358),'prize'=>'七等獎','v'=>100)
- );
- //擷取隨機獎品
- foreach($prize_arr as $v){
- $proArr[$v['id']]= $v['v'];
- }
- $rid = $this->getRand($proArr);//根據機率擷取獎項id
- $res = $prize_arr[$rid-1];//中獎項
- // dd($res);die;
- $min = $res['min'];
- $max = $res['max'];
- if($res['id']==7){//七等獎
- $i = mt_rand(0,5);
- $result['angle']= mt_rand($min[$i],$max[$i]);
- }else{
- $result['angle']= mt_rand($min,$max);//隨機產生一個角度
- }
- $result['prize']= $res['prize'];
- echo json_encode($result);
- }
- protectedfunction getRand($proArr){
- $result ='';
- //機率數組的總機率精度
- $proSum = array_sum($proArr);
- //機率數組迴圈
- foreach($proArr as $key => $proCur){
- $randNum = mt_rand(1, $proSum);
- if($randNum <= $proCur){
- $result = $key;
- break;
- }else{
- $proSum -= $proCur;
- }
- }
- unset ($proArr);
- return $result;
- }
- }
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。