The following article focuses on the Android UI part of the interface. You can understand the basic operations and regard the entire screen as a two-dimensional array. Each element can be regarded as a square, therefore, each square can be in a different state during the game.
For example, idle, wall, apple, snake, or snake head ). In the process of operating the game, we are constantly modifying the status of the corresponding square, and then re-drawing the entire View. Of course, we also need to add some game status failures or successes) ).
Before the game is officially started, we need to initialize the game. when loading the View for the first time, we will first call onSizeChanged. Here is the best time to do this. Note that the default pixel size of the simulator screen is 320x400, and the default square size in the Code is 12. Therefore, the number of squares placed on the screen is 26x40. After dividing the screen into such a large one.
Set a two-dimensional int array to record the status of each square. Based on the status of the square, you can read the corresponding status icon from the icon file saved by mTileArray. After onSizeChanged is called for the first time, it will call onDraw for the first time to draw the View itself. Of course, because the status of all squares is 0, it will not draw anything on the screen.
- Getting started with Android SDK Development
- What do apple do? Steve Jobs asked programmers to delete Androi
- Detailed explanation of Android app thread Problems
- Work related to Android Devices
- Precautions for building the Android platform
OnDraw is easy to do, that is, scanning every square. According to the current status of the square, Android UI selects the corresponding icon from the icon file to draw it on this square. Of course, this onDraw will be continuously called during the game and the interface will be updated. Imagine taking a picture of the game screen and taking another picture of its next state. How is the difference between the two pictures? For the system.
It only knows that the onDraw is constantly called, and the latter is responsible for drawing the whole screen. In this case, the difference between two screens must be produced. Some data structures must be implemented through some means. For example, the two-dimensional square map) is adjusted, such as the user's control command, timer, etc.), and the changes will be reflected on the interface in the next onDraw.
Here we will emphasize the private long mMoveDelay = 600; although this member variable is very inconspicuous, it will be interesting to consider its role carefully, so how can we change the size of a game to make it feel faster or slower?
For example, when the Android UI is started at any time, the location of the snake and the apple are marked on the square map, then we modify the snake body in the update function to let the snake go one step forward to the north. At this time, this change is only stuck in the internal core data structure, namely, two-dimensional square map), and has not yet been displayed on the interface. Of course, we immediately wanted to display this change.
Isn't it enough to let the system call onDraw to draw it? However, the problem is that we don't know how long the system calls the onDraw function, so mMoveDelay is working now. With this function, you can set the sleep time. When the sleep time is reached, the system will immediately notify the snail keview to deduplicate the painting. You can increase the mMoveDelay value to see the effect of the "photo" I mentioned above.