C + + Practice--COCOS2DX, 2048v1.0

Source: Internet
Author: User

2048 game Logic

Using a two-dimensional array as the display of the game interface,

2048 game logic: "1" Generate game object (2 numbers) Createperson () Createperson () "2" Update Game view interface if Victory is not appropriate user input if (iswin) {return;} if (UP) {Ctlfunc ()}else if (off) {Ctlfunc ()}else if (left) {Ctlfunc ()}else if (right) {Ctlfunc ()}----------------------- ----randomly generated position createperson () {As long as the array element is empty and the current coordinate position is recorded. The move Control Ctlfunc () {//For the No. 0 row does not need to move, is already the most upstream for (int y = 1; y < 4; y++) {for (int x = 0; x < 4; + +) {person = personlist[x, y] if (person = = null) {//If there is no object at the current position, no action is required continue;} If there is an object at the current position, execute the following code//To determine if there is an object int destpos = -1;for (int y1 = y-1; y1 >= 0; y1--) {Personbefore = Personlist[x in front of the current position) , y1];if (Personbefore! = null) {//If there is, no longer looking forward if (Person.name.Equals (personbefore.name)) {//If the same object is merged, No longer need to move the current object Destpos = -1;destroy (person); Personlist[x, y] = null;//update the merged number personbefore.sendmessage ("Updatelevel");} break;} else{//if not, continue looking forward destpos = y1;}} if (Destpos >-1) {//complete move//1, update position matrix information personlist[x, y] = null; Personlist[x, Destpos] = PERSON;//2, change the position of the current game object Person.transform.position + = new Vector3 (0, (y-destpos) * ITCASTHELPER.Y Offset, 0);}}} New Build Game Object Createperson ();}

Code structure:

Using MVC's software model, scene and layer decoupling and.



Experience:

The Cocos engine is really powerful enough to practice the C++,c code to write the Linux kernel source code to the extreme. C + + code will be cocos also written as the Acura. Careful reading of the source code, it is really elegant to the code out.

The combination of online resources, coupled with their own understanding, even if you write a demo.


cmd enters thein the E:\BaiduYunDownload\6yue\cocos2dx_env\cocos2d-x-3.1\tools\cocos2d-console\bin directory,>python cocos.py new wuyq-2048-p com.wuyq.cn-l cpp-d e:\

Source code, create a new project and overwrite the files in the classes and resources directories.


Initially, it was intended to control the game's interaction by using the Voice control keyboard input on the Windows platform. Baidu a bit,cocos2d is mainly for touch screen equipment, the location under Windows is more or less the equivalent of a simulator, so there is not too much PC under the important keyboard support. However, responding to keyboard messages can provide a lot of convenience for debugging. The bottom is to add the keyboard message response by changing the source of the cocos2d-x. It doesn't make much sense. For voice control demo, do a demo to practice.


Late:

Port the code below Windwos to Android.


C + + Practice--COCOS2DX, 2048v1.0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.