微信公众平台开发实现2048游戏的方法 |
本文标签:微信公众平台,开发,2048游戏 本文实例讲述了微信公众平台开发实现2048游戏的方法 。分享给大家供大家参考 。具体如下: ppstrong>一、2048游戏概述 《2048》是比较流行的一款数字游戏 。原版2048首先在github上发布,原作者是Gabriele Cirulli 。它是基于《1024》和《小3传奇》的玩法开发而成的新型数字游戏 。 随后2048便出现各种版本,走各大平台 。由Ketchapp公司移植到IOS的版本最为火热,现在约有1000万下载,其名字跟原版一模一样 。衍生版中最出名的是《2048六边形》版本,先后在全球81个国家中的board game中排进了前200 。安卓版非常火爆的有《挑战2048》,其2.0.0版以后还加入了双人对战 。其次比较特别的有2048中国朝代版 。更有2048自定义版,可以自己定义文字和图片 。《2048》是IOS中流行的一款 。 HOW TO PLAY:Use yourarrow keysto move the tiles. When two tiles with the same number touch, theymerge into one! 游戏规则很简单,每次可以选择上下左右其中一个方向去滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢外,系统也会在空白的地方乱数出现一个数字方块,相同数字的方块在靠拢、相撞时会相加 。系统给予的数字方块不是2就是4,玩家要想办法在这小小的16格范围中凑出“2048”这个数字方块 。 游戏的画面很简单,一开始整体16个方格大部分都是灰色的,当玩家拼图出现数字之后就会改变颜色,整体格调很是简单 。 在玩法规则也非常的简单,一开始方格内会出现2或者4等这两个小数字,玩家只需要上下左右其中一个方向来移动出现的数字,所有的数字就会向滑动的方向靠拢,而滑出的空白方块就会随机出现一个数字,相同的数字相撞时会叠加靠拢,然后一直这样,不断的叠加最终拼凑出2048这个数字就算成功 。 如果你是一个数字爱好者,或者是比较有天赋的数学天才,一上手便会为之着迷 。就算不是数学天才,一般的玩家也能够玩转这款游戏,感兴趣的话就去下载体验一番 。 目前这个游戏是开源的,所以不需要再来重新开发, 完整实例代码点击此处本站下载 。 二、微信公众平台 把2048源码放到自己的服务器上,得到游戏url 。 当用户关注时,提示回复2048可玩这个游戏, 当用户回复2048时,回复图文消息,图文中带2048游戏链接 。 完整代码如下所示 。 <?php /* 方倍工作室 CopyRight 2014 All Rights Reserved */ define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if (!isset($_GET[echostr])) { $wechatObj->responseMsg(); }else{ $wechatObj->valid(); } class wechatCallbackapiTest { //验证签名 public function valid() { $echoStr = $_GET["echostr"]; $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if($tmpStr == $signature){ echo $echoStr; exit; } } //响应消息 public function responseMsg() { $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; if (!empty($postStr)){ $postObj = simplexml_load_string($postStr, SimpleXMLElement, LIBXML_NOCDATA); $RX_TYPE = trim($postObj->MsgType); //消息类型分离 switch ($RX_TYPE) { case "event": $result = $this->receiveEvent($postObj); break; case "text": $result = $this->receiveText($postObj); break; } echo $result; }else { echo ""; exit; } } //接收事件消息 private function receiveEvent($object) { $content = ""; switch ($object->Event) { case "subscribe": $content = "欢迎关注方倍工作室\n回复 2048 开始游戏"; break; } if(is_array($content)){ if (isset($content[0])){ $result = $this->transmitNews($object, $content); }else if (isset($content[MusicUrl])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } return $result; } //接收文本消息 private function receiveText($object) { $keyword = trim($object->Content); if (strstr($keyword, "2048")){ $content = array(); $content[] = array("Title"=>"2048游戏", "Description"=>"游戏规则很简单,每次可以选择上下左右其中一个方向去滑动,每滑动一次,所有的数字方块都会往滑动的方向靠拢外,系统也会在空白的地方乱数出现一个数字方块,相同数字的方块在靠拢、相撞时会相加 。系统给予的数字方块不是2就是4,玩家要想办法在这小小的16格范围中凑出“2048”这个数字方块 。", "PicUrl"=>"http://img.laohu.com/www/201403/27/1395908994962.png", "Url" =>"http://gabrielecirulli.github.io/2048/"); }else{ $content = date("Y-m-d H:i:s",time())."\n技术支持 方倍工作室"; } if(is_array($content)){ if (isset($content[0][PicUrl])){ $result = $this->transmitNews($object, $content); }else if (isset($content[MusicUrl])){ $result = $this->transmitMusic($object, $content); } }else{ $result = $this->transmitText($object, $content); } } //回复文本消息 private function transmitText($object, $content) { $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content); return $result; } //回复图文消息 private function transmitNews($object, $newsArray) { if(!is_array($newsArray)){ return; } $itemTpl = " <item> <Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description> <PicUrl><![CDATA[%s]]></PicUrl> <Url><![CDATA[%s]]></Url> </item> "; $item_str = ""; foreach ($newsArray as $item){ $item_str .= sprintf($itemTpl, $item[Title], $item[Description], $item[PicUrl], $item[Url]); } $xmlTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[news]]></MsgType> <ArticleCount>%s</ArticleCount> <Articles> $item_str</Articles> </xml>"; $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray)); return $result; } } ?> 希望本文所述对大家基于php的微信公众平台开发有所帮助 。 |