Stud can also develop Android version 2048 (App Inventor)

Source: Internet
Author: User
Tags android games

To write Android games, Java is too difficult to try the app Inventor, although some people think he is a middle school toy, but also can write 2048 such a hot game, do not need too complex algorithm.


The entire game has several modules:
First, game initialization

The sequence is transformed into a graph, using the canvas and the sprite, relative to the list, here Note that the elf position is fixed, just change the picture, how many points, for n.jpg,0 points is the background image. move around, using the flung judgment, absolute judgment, horizontal speed and vertical speed which is large, used to determine whether to move or move up or down.


Second, scoring module

The general score is increased at the merging point, with the highest score of tiny db, which is judged at the end of the game. The game starts with a 0-meter distinction.


Three, mobile module (CORE)
Mobile module is the movement of the 4*4 matrix, first simplifying the left and right movement, further simplifying the matrix of 4*1, here to use the list.
First judge 0000, do not move, true, false to judge.

Second, the right to move, by observing the law, 2000 changed to 0002,2480 to 0248,2040 into 0024, the 4th bit is 0, removed and placed to the 1th place. Cycle up to 3 times. If the 4th bit is not 0, the 3rd bit is 0, the maximum loop is 2 times, and the 2nd digit is 0, 1 times. The same can be resolved to move to the left. then extend to 4*4. With a foreach loop.

Fixed move left and right, start to resolve move up and down. The left-right movement is essentially the rotation of the matrix. such as 1234

1234
1234
1234
Into
4444
3333
2222
1111
Therefore, the new list can be obtained by the conversion of rows and columns, moving up and down into the left and right movement. Note that each direction move affects both the column array and the row array, so you convert to each other.

Iv. merge Modules


Merge module Note the following rules, merging only once at a time, such as 2222, merging right to 0044, not merging to 8. 0222 merge right to 0024. Merging is primarily to determine whether the list of 12,23,34 bits is the same.

such as 34-bit the same right merge, 4-bit double, 3-bit removed, and then 1-bit plus 0. This needs to be associated with the integration module, plus the combined score.



V. Randomly add 2 or 4 modules


The selection of 2 and 4 can be set by probability, relatively simple


The selection of the insertion position is actually randomly selected at all 0 locations, where the zerolist is set to save all 0 positions, save according to the 1-16 number set, and the module to LIST0. If 5th indicates the 2nd row 1th column 0, uses the quotient and the remainder judgment, the module is Add.


Six, the game end judgment

There is no game set to win, that is, up to 2048 games are still going on, and the game ends with 3 conditions.
1 is no space, that is, zerolist is empty
2 Horizontal cannot be merged
3 cannot be merged vertically
pop-up selection box when both are true.
modules for Nomerge and Gameover


AIA file and apk download http://bbs.appinventor.com.cn/thread-3604-1-1.html


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.