Prepare for android Game Development

Source: Internet
Author: User
Tags android games java se

This article serves as the translation of your own learning materials. It may not be accurate in some places, so you can take a look at it. It would be better to learn things.

Address: http://www.rbgrn.net/content/54-getting-started-android-game-development

If you like android game development, you need to know a lot of basic things. I am a developer of light racer, light racer 3D, antigen, deadly chambers and wixel. Today these games are all placed on the Android Market. Before I develop a game, my first Android Application was the original light racer. I also learned a lot about writing Android games, so I would like to share it with you. I even wrote an online book about detailed development of light racer 3D-Development of Light racer 3D. It introduces a lot of basic knowledge and key code snippets. If you already have game development experience, it will not be too difficult to jump to the mobile development platform. You only need to learn about the android framework and APIs. If you are just learning about game development, I will provide you with a list that you must know to get started. It includes various types of games, such as action, strategy, simulation, and problem solving.

The android environment is based on Java. Because Java is a development language that is easier to use than C ++ and also a specification for mobile development, and I am using it now, it will be a good start for new users. Google has also done a lot of work in this area, such as providing documentation APIs and a large number of examples. These examples use almost 100% of API functions, so they are also called API demos. If you are familiar with Java and are already using eclipse, the first example will be quite easy. If you have never learned any code before, it will keep attracting you as you move forward, but you cannot be discouraged. If you have some experience in C ++ development and want to use C ++ to develop cross-platform or high-quality Android games, check out batterytech. Currently, I am using it to develop games.

Get the SDK

The first step is to start the android platform to obtain the SDK, which includes the Core Component Library, simulators, tools, and sample code. Eclipse and android plug-ins are recommended. If you are only doing android development, Eclipse IDE for Java Developers is a good choice. If this is your first java Development Project, you need to download the java se jdk.

Learn the application architecture

First, it is important to understand the android Application Framework. If you don't learn about it, it will be difficult for you to learn to solve game problems later. You should understand how Applications, Activities, Intents are associated with them. Google has provided a lot of good information in this framework. The more important thing is to understand why a game requires multiple activities and how to design a game to have a good user experience. All are associated with the lifecycle of the activity.

Learn the activity Lifecycle

The lifecycle of an Activity is managed by the android system. The system controls the Activity creation, recovery, pause, and destruction statuses. It is very important to handle these events correctly if you want to have a good operation application. Understanding how this works before designing a game saves you time to debug and design. For most applications, the default setting of android can satisfy most applications, but for games, you may need to consider setting the singleton flag. Because when the default value is set, android will create a new activity instance every time when it is appropriate. For games, you may only need an activity instance. This means you need to manage the status of these things, but for me, you need to consider solving some resource management problems.

The main loop

No matter what type of game you are writing, you may have no primary loop. If your game does not rely on time or simply responds to user actions, there will be no visual changes except waiting for user input, and you may not need the main loop. However, if you are writing a game that includes animation, time, or other automation, you should consider using the main thread.

The main loop of a game is the part that "ticks" sub systems in a specific order and usually as regular times per second as possible.

The main loop needs to run in its own thread, because all threads that update the UI must update the UI in the main Android UI thread. The execution sequence of the updated UI is usually as follows: State, input, AI, physics, animation, sound and video.

The update of the scenario means to manage the transition of the scenario, such as the end of the game, the selection of characters, or the next scenario. Generally, you will wait for several seconds in a scenario. The scenario management will handle these latencies and set the next scenario when the time expires.

The input is triggered by a user's keyboard operation, scrolling, and touch. The input must be processed before the physical nature is processed, because the input usually affects the physical nature, therefore, first processing input will bring more effective responses to the game. In Android, an input event occurs in the main thread. Therefore, you must use code to buffer your input events so that the main loop has time to execute. This is not a difficult task. Define the field, and save the input of the next user through the onkeypressed or ontouchevent event. When this input is a valid input, it will change the scene of the game and let the physical nature handle the response to it to update the input.

AI updates are similar to users who decide to "Press" for the next step. Learning how to write AI is beyond the scope of this article, but the general idea is that AI will press the button as the user does. This will also cause and respond to Physical updates.

Physical updates may not be physical. For example, in an action-type game, considering the last update as the key point, the current time will update the user's input. The input of AI has determined whether all things are collided. For a game, if you grasp an object visually and slide them, it will be a part of the slide or let it fall into other places. For a puzzle Q & A game, it will be part of whether the answer is correct or not. You can also say something else about you. All the games in this article are part of this game engine. I refer to it as physical.

An animation is not as simple as a GIF animation. You need to depict each frame at the right time of the game. This is not as difficult as sound processing. The fields for saving these scenes, such as isDancing, danceFrame and lastDanceFrameTime, enable the animation to update Frames Based on time. This is what all animations do. In fact, all the changes to the display animation are updated by the video.

The sound is updated by processing the triggered sound, stopping the sound, changing the volume, and changing the tone of the sound. Generally, when writing a game, the sound update is actually sent to the sound buffer by generating a bytes stream, which is managed by android. Therefore, you can choose SoundPool or MediaPlayer to use the game. They are both a little sensitive but know that because of some low level implementation details, small, low bitrate OGGs will yield the best performance results and the best stability.

Video updates take into account the scene of the game, the position of the character, score, status, behavior and screen painting. If you use the main loop, you will use surfaceview for painting. Compared with other views, the view itself calls back the painting operation and the main loop will have to be done. Surfaceview has the highest frame speed per second and the most suitable function for games to be animated or move parts on the screen. All the video update shocould do is take the state of the game and draw it for this instance in time. Any other automation is better handled by a different update task.

What is the code? Here is an example:

Public void run (){
While (isrunning ){
While (ispaused & isrunning ){
Sleep (100 );
}
Update ();
}
}

Private void Update (){
Updatestate ();
Updateinput ();
Updateai ();
Updatephysics ();
Updateanimations ();
Updatesound ();
Updatevideo ();
}

3D or 2D?

Before starting your game, you need to decide whether you want to move to 3D or 2D. 2D games have relatively low learning curves and are generally easier to achieve good performance. 3D games require more in-depth mathematical skills and may have performance problems if you are not careful. They also require the ability to use modeling tools, such as 3D Studio and Maya. If you plan to have complicated boxes and circles. Using OpenGL, Android supports OpenGL 3D programming, and many good tutorials can be found in OpenGL.

Build Simple, high quality Methods

At the beginning, make sure you want to avoid writing the method too long. If you follow the main loop mode, as described above, this is quite easy. Each method can only complete one function and ensure that there is no error. For example, if you need to wash the cards on the table, you should write a method called "shufflecards" to only use the card Washing function.

Exercise code is an important part in game development. Debugging is stateful, which is very difficult in a real system. Try to maintain that your method has only one task, instead of completing multiple tasks in one method. If you want to write a method to draw the background of a scene, you can call it "drawbackground ". This allows you to quickly understand complex systems in the future.

It's all about efficiency!

Performance is a major issue for any game. the goal is to make the game as responsive as possible and to also look as smooth as possible. certain methods like Canvas. drawLine are going to be slow. also drawing an entire screen-sized bitmap onto the main canvas every frame will also be costly. balancing things like that is necessary to achieve the best performance. make sure to manage your resources well and use tricks to use the least amount of CPU to achieve your task. even the best game will not be very fun if it can't perform well. people in general have little tolerance for choppiness or poor response.

Tips and tricks

Take a look at the example for LunarLander in the SDK. it uses a SurfaceView and that wocould be the appropriate view to use for a game that needs the highest number of frames per second possible. if you're going 3D, take a look at GLSurfaceView. it takes care of the OpenGL device initialization and provides a mechanic for rendering. for LightRacer, I had to optimize the way I have everything drawn or else the framerate wocould be drastically lower. I drew the background to a Bitmap only once which was when the view is initialized. the light trails are in their own bitmap which gets updated as the racers move. those two bitmaps are drawn to the main canvas every frame with the racers drawn on top and then finally an explosion. this technique made the game run at a playable rate.

It's also a good practice to have your bitmaps be the exact size you intend to draw them on screen, if applicable. this makes it so that no scaling is needed and will save some CPU.

Use a consistent Bitmap Configuration (like RGBA8888) throughout the game. This will save the graphics library CPU in having to translate the different formats.

If you're determined to develop a 3D game but have no 3D knowledge, you will want to pick up a book or two on 3D game programming and study up on linear algebra. at a bare minimum, you must understand dot products, cross products, vectors, unit vectors, normals, matrixes and translation. the best book I have come into SS for this math is called Mathematics for 3D Game Programming and Computer Graphics.

Keep the sound small and at a low bitrate. The less there is to load, the faster loading times will be and the less memory the game will use.

Use OGGs for sound, PNGs for graphics.

Make sure to release all media players and null out all of your resources when the activity is destroyed. this will ensure that the garbage collector gets to everything and that you don't have any memory leaks between launches of the game.

Join the Android Google group and find community support. There will be people that can help you along the way.

Above all, spend time testing and retesting and making sure that every little thing works exactly the way you wowould perform CT it. polishing the game up is the longest and hardest part of development. if you rush it out to market, you will probably have a disappointed crowd and you may feel that all your hard work is wasted. it's not possible to have 100% of people love what you write but you shoshould at least try to put out the highest quality work that you can.

Related Article

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.