Home >Backend Development >PHP Tutorial >Methods for developing and implementing the 2048 game on WeChat public platform, Public 2048_PHP tutorial
This article describes the method of developing and implementing the 2048 game on the WeChat public platform. Share it with everyone for your reference. The details are as follows:
1. Overview of the 2048 game
"2048" is a popular digital game. The original version of 2048 was first released on github. The original author is Gabriele Cirulli. It is a new digital game developed based on the gameplay of "1024" and "Little 3 Legend".
Subsequently, various versions of 2048 appeared on various major platforms. The version ported to iOS by Ketchapp is the most popular, with about 10 million downloads now, and its name is exactly the same as the original version. The most famous of the derivative versions is the "2048 Hexagon" version, which has ranked among the top 200 board games in 81 countries around the world. The very popular Android version is "Challenge 2048", which has added two-player battles since version 2.0.0. The second, more special one is the 2048 Chinese Dynasty Edition. There is also a 2048 customized version, where you can define your own text and pictures. "2048" is a popular one on iOS.
HOW TO PLAY:Use yourarrow keys to move the tiles. When two tiles with the same number touch, theymerge into one!
NOTE:This site is the official version of 2048. You can play it on your phone via.All other apps or sites are derivatives or fakes, and should be used with caution.
Created by Gabriele Cirulli.Based on 1024 by Veewo Studioand conceptually similar to Threes by Ashe Vollmer.
The game rules are very simple. You can choose one of the up, down, left, or right directions to slide each time. Every time you slide, all the number squares will move closer to the sliding direction. The system will also randomly appear a number square in the blank space. The same Numbered squares will add up when they come close to each other and collide. The number cubes given by the system are either 2 or 4. Players have to find a way to make up the number cube "2048" in this small 16-frame range.
The screen of the game is very simple. At the beginning, most of the 16 squares are gray. When the player appears in the puzzle, the color will change. The overall style is very simple.
The gameplay rules are also very simple. At the beginning, two small numbers, such as 2 or 4, will appear in the square. Players only need to move the appearing numbers in one direction, up, down, left, or right, and all the numbers will slide in the direction. The direction is closer, and a number will appear randomly in the blank square that slides out. When the same numbers collide, they will be superimposed and moved closer. And then it will continue like this. The number 2048 will be pieced together after continuous superposition, which is considered successful.
If you are a numbers enthusiast, or a relatively talented math genius, you will be fascinated by it as soon as you get started. Even if you are not a math genius, ordinary players can play this game. If you are interested, download it and experience it.
Currently this game is open source, so there is no need to re-develop it,
Click here to download the complete example code from this website.
2. WeChat public platform
Put the 2048 source code on your own server and get the game url.
When the user follows, the user will be prompted to reply 2048 to play this game,
When the user replies to 2048, reply with a picture and text message with a 2048 game link in the picture and text.
The complete code is shown below.
<?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; } } ?>
I hope this article will be helpful to everyone in developing WeChat public platform based on PHP.