How to Develop a game

Source: Internet
Author: User

Recently, many people asked me how to develop a game. I didn't have any articles on this topic on the Internet, so I decided to write something to share my experience, the entire process of game development. Remember that this is just an overview and will change depending on the project.

Step 1. Select your game Library
Unless you want to write your own game library, including those troublesome graphics and sound programming models, you should need an open-source game library that provides the same basic functions.
Features required by any excellent game Library:
Load and play sound;
Load and display images;
Basic image operations (rotation and scaling );
Original drawing method (points, lines, rectangles, etc );
Display text;
Multithreading support;
The basic timer function.

Some game engines:
Simple fast multi-media library (sfml): http://www.sfml-dev.org/
Simple DirectMedia Layer (SDL): http://www.libsdl.org/
Allegro: http://www.allegro.cc/
Pengl (GFX only, however, there are wrapper libs like allegrogl): http://www.opengl.org/
DirectX (Windows only): http://msdn.microsoft.com/en-us/directx/
Irrlicht (3D Lib): http://irrlicht.sourceforge.net/

Step 2. Confirm the script
All games start from here and the idea comes from the brain.
First, come up with a game idea. Once you have a simple idea, expand it. For example, in a board game, what is the theme, what are the conditions for victory, and what are the rules. If a game has people or stories, create them. Make sure that when your game is complete, you know exactly what it will be like. The more complex the game is, the more time you need to plan before you start, so that you don't have to worry about these problems when coding. Remember, your game will look like you created it.

Step 3. Customize your engine
In this step, you need to plan the components required by your game engine and integrate them together. Depending on the complexity of your project, you may not need this step, this is also a good time to test the normal operation of your game engine brother and ensure that they work properly before they are put into real projects. At the same time, you should also design the classes in your project (if you use OOP ). Remember, there are already some ready-made engines available for most projects.

Step 4. compile your engine (If You Want To Do It Yourself)
Now we can officially start writing game engines. This is not to say that we are writing games, but core rendering, physics, file management, and so on. Use the classes and methods in the engine to build your game. Based on the complexity of the game, the engine code may be similar to the game code.
For a very complex game, you may also need a resource manager that manages resources (images, music, sounds, and so on) Just like its name ), it keeps the code clean and helps you avoid memory leaks. You can refer to an excellent resource manager xandre314. try to make your code rigorous and the interface simple. after doing so, when you are writing a game, you do not need to check the source code and find the function name. A good programming method is oop.

//put related components into classesclass collisions {    bool check_col(obj1*, obj2*); //you should have a base object class that all    void handle_col(obj1*, obj2*); //game objects are derived from, for easy passing to functions    public:        void handle_all(); //handles EVERYTHING collision related    }Collision;class rendering {    void bots();    void bullets();    void players();    public:        void draw_all(); //calls other functions for rendering    }Renderer;//this allows collision management and rendering in your game loop to be as simple as:Renderer.draw_all();Collision.handle_all();

Resource Manager by xandre314

/* ResourceManagerB.hpp - Generic template resource manager (C) Alexander Thorne (SFML Coder) 2011 <a href="http://sfmlcoder.wordpress.com/">http://sfmlcoder.wordpress.com/</a> Manages loading and unloading of a resource type specified by a template argument.****************************************************************/#include <map>#include <string>#include <exception>typedef const std::string URI;// exceptionsnamespace Exceptions {// thrown if user requests a resource URI not present in the manager's listclass URINotFound : public std::runtime_error { public: URINotFound(const std::string& Message = "The specified URI was not found in the resource index."): runtime_error(Message) { } };// thrown if a resource allocation failsclass BadResourceAllocation : public std::runtime_error {public: BadResourceAllocation(const std::string& Message = "Failed to allocate memory for resource."): runtime_error(Message) {}};}template <class Resource> class ResourceManagerB {typedef std::pair<URI, Resource*> ResourcePair;typedef std::map<URI, Resource*> ResourceList;// the list of the manager's resourcesResourceList Resources;public:~ResourceManagerB() { UnloadAll(); }// Load a resource with the specified URI// the URI could represent, e.g, a filenameURI& Load(URI& Uri);// unload a resource with the specified URIvoid Unload(URI& Uri);// unload all resourcesvoid UnloadAll();// get a pointer to a resourceResource* GetPtr(URI& Uri);// get a reference to a resourceResource& Get(URI& Uri);};template <class Resource>URI& ResourceManagerB<Resource>::Load(URI& Uri){// check if resource URI is already in list// and if it is, we do no moreif (Resources.find(Uri) == Resources.end()){// try to allocate the resource// NB: if the Resource template argument does not have a// constructor accepting a const std::std::string, then this// line will cause a compiler errorResource* temp = new (std::nothrow) Resource(Uri);// check if the resource failed to be allocated// std::nothrow means that if allocation failed// temp will be 0if (!temp)throw Exceptions::BadResourceAllocation();// add the resource and it's URI to the manager's listResources.insert(ResourcePair(Uri, temp));}return Uri;}template <class Resource>void ResourceManagerB<Resource>::Unload(URI& Uri){// try to find the specified URI in the listResourceList::const_iterator itr = Resources.find(Uri);// if it is found...if (itr != Resources.end()){// ... deallocate itdelete itr->second;// then remove it from the listResources.erase(Uri);}}template <class Resource>void ResourceManagerB<Resource>::UnloadAll(){// iterate through every element of the resource listResourceList::iterator itr;for (itr = Resources.begin(); itr != Resources.end(); itr++)// delete each resourcedelete itr->second;// finally, clear the listResources.clear();}template <class Resource>Resource* ResourceManagerB<Resource>::GetPtr(URI& Uri){// find the specified URI in the listResourceList::const_iterator itr;// if it is there...if ((itr = Resources.find(Uri)) != Resources.end())// ... return a pointer to the corresponding resourcereturn itr->second;// ... else return 0return 0;}template <class Resource>Resource& ResourceManagerB<Resource>::Get(URI& Uri){// get a pointer to the resourceResource* temp = GetPtr(Uri);// if the resource was found...if (temp)// ... dereference the pointer to return a reference// to the resourcereturn *temp;else// ... else throw an exception to notify the caller that// the resource was not foundthrow Exceptions::URINotFound();}


Step 5. images and sounds
Based on your game's point of view, start to create your graphics and sound effects. When you start in-depth development, you may need to create a larger GFX and SFX, and delete unnecessary resources. This process may run through the entire process.

Step 6. Write a game
After you have completed your game engine, you can start to write the game. This includes many things, including rules, stories, and the main loop of the game will also be created here. This loop will run over and over again and update everything in the game.

Let's look at the example below. If the engine you write is correct, it will be easier and more interesting than the writing engine. It will be great when you add some sound effects somewhere. When the stage is set up, you should save a code that can even run, and make sure what you see is what you expected.

//your loop will probaly be very different from this, especially for board games//but this is the basic structurewhile (!Game.lost()) //or whatever your condition is (esc key not pressed, etc){    Game.handle_input(); //get user input    AI.update_bots(); //let your bots move    Collision.handle_col(); //check for collisions    Game.check_win(); //see if the player won or lost    Renderer.draw_all(); //draw everything    Game.sleep(); //pause so your game doesn't run too fast    //your game lib of choice will have a function for this}


Step 7. Optimization
When the game can run, it does not mean it is complete. In addition to adding the last plot, you can also optimize the code, including memory usage (do not use global variables, check memory allocation, and so on ), game acceleration (ensure that your code is not too slow, and you are not consuming a lot of CPU at any time ). This step can also be used for testing.

Step 8. Package and release
Now that your game is complete, you need to package it and then release it. For packaging, try to maintain conditioning, and put the final product into a single file (Installation File, zip, etc.) to make publishing easier.

Prompt
I have learned a lot about game production so far, and some things are really difficult.
You should do the following:
First, be organized. You should have a good arrangement of everything, your code, your pictures, your sound effects, and so on. I suggest you put the code in different files according to the purpose, for example, the collision detection is put into one file, the resource management is put into another file, and the AI is put into another file ...... in this case, if you need to track a bug, you can easily find the corresponding method, and you may also find the bug itself directly. Keep the code in the same structure (different classes handle different things, rendering, AI, collision detection, etc., rather than writing hundreds of methods in one class ).
At the same time, we strive to keep the code clean and efficient, reuse variables as much as possible, avoid using global variables, check for memory leaks, and do not load all graphics and sounds at once.
Instead of hard-coding data into the game, you can implement a data file loading system, which will keep the code maintainable and you do not need to compile all the files when you want to upgrade,

Some suggestions for beginners of chrisname
You don't have to work so hard. What you need to do is to learn a set of course courses. Do not do too many things in a day. Otherwise, you will get bored or have no motivation. It is useless to set the target by time. If you stop learning, you will forget what you learned. Go through the tutorials on this web site (http://cplusplus.com/doc/tutorial. The target is two articles per day. Do not stop in the middle (unless you do need to take a break), do not do too much at once, otherwise you will not be able to remember, I suggest you carefully read, in addition, you need to repeat every example (instead of copying and pasting, You can manually record it, which will help you understand the meaning of the Code), compile and execute it, see what has changed, and modify the code, let's see what's changed. I also suggest you take a look at other people's excellent code. When you are studying the tutorial, remember one thing-if you cannot explain it clearly, it is not an understanding.
Once you start learning this tutorial or other tutorials (I have read three tutorials on the same topic, because it is helpful to express the same thing in different ways ), you can read the sfml tutorial (http://sfml-dev.org/tutorials/1.6/), Learning sfml will teach you how to do 2D games, I also recommend you learn SDL (http://lazyfoo.net/SDL_tutorials/index.php), because many games use it, you are also likely to complete the process.
Then, if you want to create a 3D game, you should learn OpenGL and sfml to make it very simple. The sfml tutorial contains the OpenGL tutorial, for OpenGL, other people may recommend some books or tutorials.
In the whole process, you should remember that it is very important to master your own pace. Do not eat fat people in one breath and cannot digest them. If you have an exam the next day, do not stay up till midnight.

To make development easier and make game development more effective, you still need to do a lot, but these are the most important.

Link: http://www.cplusplus.com/articles/1w6AC542/

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.