Advertisement

How does a game work?

Started by July 07, 2014 12:09 PM
3 comments, last by Dalphin 10 years, 6 months ago

Hello everyone,

For quiete a time i'm thinking of how games work.

When i see the directory of games (especially under contruction) i always see so many source codes.

for example a source code named Bullet, another named characters and things like that.

I always thought that you had so many different source codes for different things and then one (main) code,

where it all gets put together in one game.

Is this true and if not, could you explain me how it works then.

Thanks.

As there are entire books and websites devoted to the subject, you might want to start with one or more of the 27 million hits (sleep.png ) google provides for a search of "programming creating a game."

As a very general approach, some "main" code does some basic setup, perhaps creates some resources and makes calls to code which (perhaps) creates more resources, updates some parameters, gets input from the user, and outputs information (maybe text or graphics) to the user.

Depending on the complexity of the game, that may be 100 lines of code in one file, or 1000s of lines of code spread across 100s of files.

If you can indicate your level of knowledge with regard to programming, and ask something a bit more specific, you can probably get more specific information.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

Advertisement

Most of the times using different files for different things is just for organization, it's easier to add new things or fix bugs if you have a file with code that is related and everything about it is there.

The "main" file doesn't "put togheter" everything, it's just some piece of code that uses other pieces and you can (and will) have other pieces of code that uses some other pieces apart from the main one. For example, you can have a file with physic simulation code that uses the code from other files too (like a RigidBody file).

Every program has an entry point, which is the first thing that will be executed when the program starts, and at that point the code would check for different conditions to do different things. In a game and other visual applications the "main" code will have a piece of code that runs forever in a loop until you quit or will start some other code that does that. It's kind of standard to name that code "main", but it's not really different from other source code.

Anyway, when talking about programming you have a lot of things happening that depends on more factors. In some languages like C you do end up with one compiled file that has all the code in it (you don't have to make it, there's a compiler that will do it) even if you wrote different files. In other languages like JavaScript you have all different files that you have to put in one file if you want to (for optimization maybe).

Programming is really complex and, like Buckeye said, there are books about it. There's no short and easy answer for your question, and more important, there's not a unique answer. You should try to learn how more simple applications work before looking at a game's source code.

The reason you will see code split into various sections is primarily for organisation, though sometimes things like encapsulation will necessitate that you split up your script somewhat. A full game will often have hundreds of lines of code, and for someone looking through that (eg. Someone else on the dev team) it can be a pain in the rear to work through all that to find the code they need to work on. So, the code will be split up into separate scripts, each taking charge of a particular part of the game. The "Bullet" script you mention might take charge of drawing the bullet on screen, giving it a velocity, and dealing with impacts against enemies. Then another "damage" script might in turn reference the bullet script, saying that if the bullet hits an enemy, then an amount of damage, within a certain range, will be inflicted.

This method allows other coders to just go straight to the bit of code they want. As for encapsulation, it also minimises the chance of a fellow programmer accidentally affecting a bit of code that you want to be left alone.

okey thanks everyone

This topic is closed to new replies.

Advertisement