-->

Android small game development foundation

With the development of smart phones, many games are constantly appearing and updated in mobile phones, such as the previous hot plants vs. zombies, angry birds, and so on. The essence of the game is to constantly display and update pictures on the screen, but not to update randomly, but to control according to program logic. A complete game requires multiple aspects of knowledge, such as the game's creativity, background, storyline, game sound effects, game style, game type, running speed, and compatible models. Moreover, the development of the game requires the coordinated work and tacit cooperation of planning, art, procedures, and testing.

Some basics used for beginner android mini game development

If you are interested in developing games for the Android platform, then you need to know a lot. If you have a little development experience and want to develop cross-platform games or outstanding Android mini games. Then you have to learn some knowledge provided by the following freelancer.

Learn application architecture

Understanding the Android application architecture is also very important. If you don't learn this aspect, it may be difficult to modify the problem after the game is completed. You need to understand applications, activities, intents, and the interconnections between them. Google has provided a lot of valuable information in terms of architecture. What really matters is to understand why games need to include more than one activity and what this means for designing a good user experience. What we are going to discuss next is the activity life cycle.

Get SDK

The first step in developing the Android platform is to obtain the Android SDK (Game Development Note: Software Development Kit). The SDK contains core libraries, simulators, tools, and code examples. I highly recommend using Eclipse and the Android Eclipse plugin. If you are only targeting the Android platform, the Eclipse IDE for Java Developers is a great tool. If this is your first Java development project, you can download the entire Java SE Development Kit ("JDK" for short) because it contains the tools you need for application authentication and deployment.

Learning activity life cycle

The activity life cycle is managed by the Android operating system. Your activity will be created, visited, paused, and destroyed according to the instructions of the operating system. Proper handling of these events is very important for building a well-behaved application so that the application properly meets the user's intentions. Before you start designing a game, it is necessary to know how all of this works, which will reduce your time for future debugging and redesign. For most applications, using the default settings is fine. But for games, you may want to consider turning on SingleInstance. When in the default settings, Android will automatically create new active instances when appropriate. For games, you may only want a single instance of a game activity. This is also a reminder of how you need to manage the state of the content, but for me it solves some resource management issues and should be considered for inclusion.

Main loop

Whether or not you have a main loop depends on the type of game you are writing. If your game has nothing to do with time, or if the game will only respond to user behavior and the visual scene will not change without user input, then you probably don't need the main loop. If you are writing an action game or a game with animations, timers, or other auto-running content, then you should seriously consider using the main loop.

The main loop of the game refers to running the subsystems in a specific order, and the number of main loops per second is usually increased as much as possible. Your main loop needs to run on its own thread. The reason is that Android has a main UI thread. If the main loop is not running on its own thread, the UI thread will be affected by the game, causing the Android OS to fail to complete ordinary update tasks. The execution order is usually: status, input, AI, physics, animation, sound, and video.

Updating status means managing status changes such as game over, character selection, or the next level. Normally, you need to wait for a few seconds on a state. The task of state management should be to handle this delay and set the next state after the time has passed.

Input is any key, scroll, or touch from the user. It is important to complete this aspect before processing physics, because input often affects the physical effect, so processing the input first will make the game more sensitive. In Android applications, input events come from the main UI thread, so you have to buffer the input through code so that your main loop can capture it in time. This is not a difficult task. All you need to do is determine the area for the next user input, and use onKeyPressed or onTouchEvent to set the next user action to this area. In a specific game state, all valid inputs need to be updated for input, so that the physical end can handle the response to the input.

The AI ​​update is similar to the key that the user decides to "press" next. Learning how to write AI is not the subject of this article, but the basic idea is that AI will press keys like a user. This will also be captured and responded to by physical updates.

Physical updates may or may not be physical. For action games, the main point is to consider the last update time, the current update time, user input and AI input at the same time to determine the position of all content in the game and whether there is a collision. For games where you can see object grabbing and sliding, the physical update also needs to handle the object's interaction or let it fall to the specified position. For quiz games, it also needs to decide if the answer is correct. Maybe you use a different name, but the game engine of each game needs to deal with the above content, I will call it "physical" in this article.

Animation is more than just adding gifs to the game. You need to make the game play every frame of the animation at the right time. However, it is not as difficult as it sounds. To maintain the status areas such as isDancing, danceFrame, and lastDanceFrameTime, allow the animation to update to decide whether to move to the next frame. That's all there is to the animation update. What is really responsible for rendering the animation change is the video update.

Sound effects update are responsible for triggering sound effects, pausing sound effects, changing volume and pitch. Usually when writing a game, the sound update will generate a series of bytes to be transmitted to the sound buffer, but Android manages the sound effects by itself, so for game development, you can use SoundPool or MediaPlayer. They are all a bit sensitive, but know that because of some low-level implementation details, small and low-bit-rate OGG can also produce the best performance and stability.

Video updates take into account game status, player position, score, and status, and draw everything on the screen. If you use the main loop, then you can consider using SurfaceView for "push" drawing. If you use other views, the view itself can call the drawing operation, so the main loop does not need to be responsible for this work. SurfaceView offers the highest frames per second and is best suited for games with animation or moving objects on the screen. What the video update should do is get the game status and draw it on the screen in time. In addition, other automation content is best done by other update tasks.

3D or 2D?

Before you start developing games, you need to decide whether to make 3D or 2D games. 2D games have less to learn, and it's usually easier to get good performance. 3D games require more in-depth mathematical skills, which can cause performance problems if not noticed. If you want to add more complex shapes than squares and circles, whether you choose 3D or 2D, you need to use modeling tools such as 3D Studio and Maya. Android supports 3D programming using OpenGL, and there are many great OpenGL tutorials on the web to read.

Build simple and high-quality methods

When development starts, make sure you don't write big and long methods. If you use the main loop style I described above, the approach should be simpler. Each method you write should be able to accomplish a specific task without errors. For example, if you need to shuffle in the game, you should write a method called "shuffleCards", and this method is used to complete the shuffle action.


This is a programming method that can be used for all software development, but it is particularly important in game development. In a stateful, real-time system, debugging can become very difficult. To keep methods simple and lightweight, it is best to ensure that each method has only one goal. If you need to draw a background for your scene, then you may need a method called "drawBackground". This will make your game development like building blocks. You can add new blocks at any time according to your needs without making the code difficult to understand.

Learn application architecture

Understanding the Android application architecture is also very important. If you don't learn this aspect, it may be difficult to modify the problem after the game is completed. You need to understand applications, activities, intents, and the interconnections between them. Google has provided a lot of valuable information in terms of architecture. What really matters is to understand why games need to include more than one activity and what this means for designing a good user experience. What we are going to discuss next is the activity life cycle.

Efficiency is key

Running effect is the main problem encountered in all games. Our goal is to make the game as responsive as possible and make the game run as smoothly as possible. Some methods such as Canvas.drawLine may perform slower. Also, drawing a full-screen bitmap on the main canvas of each frame can take a lot of time. To get the best performance from your application, balancing these actions becomes necessary. Ensure that resources are well managed and use a variety of techniques to accomplish tasks with minimal CPU. If it doesn't perform well, even the best games can be boring. Usually, people don't have a good opinion of slow response applications.

In general, publishing updates and bug fixes is another important task for developers, especially independent developers. Some android mini-game development is rarely updated once completed, unless there are major bug fixes. Other games, especially those that are more heavyweight and have more social content, will spend more energy on updating. At this time, independent developers also find that user feedback is a valuable resource.

Powered by Blogger