Home · Create New Account New Account · Login
About This Site
> Making Computer Games
Thanks for visiting!
How to Write Your Own Computer Games in C++
"Learn how to write your own PC games in C++"
written by gregsometimes


This page assumes no previous experience developing games, and is targeted at the absolute beginner, someone who is looking for a place to start.


Welcome to my guide on how to make computer games on your PC. We will choose an IDE (Integrated Development Environment) and start with setting it up so we can begin writing some code and create our first window. First we will write a simple text-based game "Guess" to demonstrate what code almost every game utilize at its core. We will then use what we learned with our first game to build our next games that gradually become more advanced, and more fun. You are encouraged to follow the links throughout this page to learn about subjects you don't already know, for example how to create your first window on a Windows-based operating system, which is required knowledge for making PC games that contain graphics.

A game that resembles the classic Wolf3d, programmed in C++.
I want to thank you for stopping by! If you are just as excited about building your own computer games as I am, then proceed reading. Just to clarify, the purpose of this page is not to reach a quick-and-dirty solution on how to slap games together and sell them. In fact, this page has nothing to do with selling games, and I hope that you are just as passionate about the art of writing computer games as I am. Unfortunately, many of today's computer games are based on how well they look on the computer screen, or in other words their graphics. But I believe that the quality of graphics alone shouldn't be the defining factor of how good a game is. If you are with me on this, this and my consequent tutorials will hold invaluable information on how to design and build your own computer games. I remember the time when I just started learning about programming computer games in Windows 95. It was the time when DirectX7 was the latest graphics API for the Windows operating system. When we are learning something we have an interest in, even simplest experiments can be exciting and gratifying. One of my first computer programs was a star-field.


Let me tell you a little about myself first. I designed my first computer game when I was 11 years old. I programmed it in the language known as Basic on a ZX-Spectrum 48K computer. That computer wasn't very popular in the United States at the time. I don't remember what I called my game, but the point was to control your character (that was 16 pixels wide and 24 pixels in height) from one screen to another, pick up several objects and use them to unlock doors as well as other things. The plot was very simple and went as follows: the terrorists opened a valve in the sewer to release deadly gas into the air and have the whole city infected with the poisonous substance (I recently read a book by Stephen King - The Regulators, it's about children's imagination, and I think here we see a perfect example of that). Your mission started in the city's airport. You had to find a shovel, dig a hole in the ground, shut off the valve and return back to the airport.

The screen resolution of the ZX-Spectrum computer was 256 by 192 pixels, so drawing graphics for the game wasn't very challenging, as every graphics tile of my tile-based "graphics engine" was only 8 by 8 pixels. Hardly a graphics engine. At 11, I couldn't program computer software to save my life, and I was just using for-loops and if-statements for everything! but already, I was poking my nose into the Assembly Language code, which was very exciting at the time, even though I understood little of it and copied many lines of code from Science Magazines and the only book on the subject that I had. I always liked to create or build things - anything that I could find - rather than play. Don't get me wrong, I have also played hundreds of games for hours on my ZX Spectrum computer, waiting for each game to load for 3 to 5 minutes, but ever since my uncle had shown me that I could draw a line on the screen of the computer with a one line Basic command, I was hooked. I wanted to draw more lines, I wanted to create character animations and I wanted to build entire games! I would actively talk my parents into going to computer fairs and have them buy me ZX-Spectrum tape cassettes (that's what games were stored on back then), not with games on them though, but rather with collections of arbitrary tools that were used to create games. Even to this day, rarely do I buy computer games. The only two games that I ever bought out of curiosity, were Half-Life and Starcraft. In both cases, when I brought the games back home, I didn't install the games first, I installed the game editing software that was provided with the two games. Half-Life had an editor called Worldcraft, and Starcraft had a map editor called StarEdit. Creating game environments and designing game maps or "levels" is a science into itself, but of course it will be discussed here, only later.


As the title of this page suggests, you have no previous knowledge about making games whatsoever. I remember when I developed an interest in writing computer games and how hard it was to learn building your own games from scratch. Game development is a branch of software engineering that is considered one of the most difficult types of computer programming. Ironically, the most fun and enjoyable games also take a tremendous amount of time and effort to develop. As the computer game industry grew and game development became a popular profession, the mere fact has given a birth to several Computer Game Design Schools, that teach everything from Level Design, 3D Modeling and Programming. For some people going to school would work better, and it's an excellent way to meet people who have the same passion for making computer games.

However, I didn't go to school. I happened to be the kind of a person who likes to learn from an open book and not necessarily a teacher. I remember how difficult it was to start. I looked through all tutorials I could find on the Internet, and purchased several books on the subject. Since then I've learned quite a lot about games and I wanted to write this page to make it easier for every new-comer to understand what the best tools for developing games are and how to start in general. Therefore, the information on this page assumes no previous knowledge of making computer games. I do assume the basic knowledge of a computer language such as C++. I am planning to continue writing about making games further and gradually we will get to more advanced game programming topics and tutorials that come with source code. But for now, let's start from the absolute zero and make one game after another with each game being better than the previous one.


A game engine can be thought of as a library of different game component functions responsible for graphics rendering, sound output, artificial intelligence, physics calculations and others, depending on the game type. In a bomberman-like game, for example, we don't need physics, because the game is so simple, but we do need 2D sprite rendering functions and playing sound effects. The game engine is the core of your game technology. Games that graphically operate in three dimensions are known to be based on 3D graphics engines. A modern graphics engine utilizes OpenGL or DirectX graphics APIs, which means polygon rendering is done in hardware with the help of a powerful graphics card.


Where does one begin learning about making their own computer games? Writing computer games takes patience and effort. You are required to know a computer language. There are many different computer languages out there and many Operating Systems to program for. One of the most popular languages for programming computers games is C, or C++. Of course you would need to know the basic OOP (Object Oriented Programming) rules if you are going to write your game in C++. We are going to focus specifically on writing games in C/C++ and compile our code with the help of the Microsoft Visual C++ compiler on a Windows-based computer. To start writing games you must use an IDE (Integrated Development Environment) and a compiler. More than often your compiler will already come with the IDE of your choice. One of the best IDEs for programming games (or other software) on Windows is Microsoft Visual C++. Once you download the most recent version, you will be ready to start programming your first window application. You can learn how to do so from a link you saw in the first paragraph of this page. As you hopefully already noticed, I have written a separate tutorial just for that. When you understand how creating windows works, you will be ready to go on to the next step and learn about the three basic steps which are: game initialization, the game loop and the game destruction function.


The Three Main Functions - Game Initialization, Game Loop and DestructionWhat is a paradigm anyway? A paradigm in any system is a pattern or model. Because I decided to write about making computer games from absolute scratch, in this section you will learn the very basics of the computer game design pattern. Therefore I propose, let us not focus on fancy graphics but rather create a simple framework for our first game that we may use later to build any kind of games we want.

Almost any game usually contains the three important parts: Game Initialization, The Game Loop and Game Destruction. More than often, game developers think of programming games as building state-machines where only certain parts of the code are executed at a time, depending on what state the game is in. To give you a practical example, let's say that you are coming closer to your enemy in a game, but they can't see you because you are behind them So, perhaps we could set that enemy object's state to "idle". Had the enemy been facing the player, theoretically having the player in his eyesight, he may have been set to a state we would call "alert", and react in a way that is defined by the artificial intelligence algorithm assigned to him - to run away or attack the player (you). At this point we will not think about states in depth and only discuss them when it's appropriate. But it's certainly something to think about.


This is the part where you allocate memory for your game objects, initialize the game elements such as the game menu or options screen and put them into their default states. In your first game we will not concern ourselves with complex game initialization, because frankly, there is not much to initialize. The idea is to understand that this is one of the first functions that we are going to call that initializes the game. Later on we will use this function to load graphics, high score tables, and so forth. All of the code here will depend on what type of game you are making.


The game loop is where your main game code goes. This is where your game monsters dance, so to speak. A game loop function such as "DoFrame()" is called once on each frame of animation from the Window's message loop. Depending on the current state of the game we will do state-specific things. If we are currently choosing an option from the main menu, for example, we will be executing the code that has to do with the main menu logic. After we select "Start Game", a game level data will be loaded, characters will be reset to their default positions and their health level will be reset to 100%. Again, we will not concern ourselves with this for now. Simply know that this is the raw meat of your game, where everything happens (for a reason, I hope).


When the game window is closed, we need to deallocate (release back to the Operating System) memory that was allocated in the initialization stage and the game loop. We need to release memory associated with our game objects (player, monsters, levels, and so forth). This can be thought of as the reverse step of Initialization. Software Engineers are known to be making up all kinds of funky phrases, that make sense in the context of computer programming. So if we "initialize" something, the reverse function would be "uninitialize" or "destroy" game objects. It is common to "uninitialize" game objects one by one in the reverse order as they were created in the Game Initialization step.


Based on what we just learned about the three basic game functions, we are going to write a short text game called "Guess". In this game the computer will "think" of a number between 0 and 100 and have you guess it. You have 5 tries. If you think it's easy, compile the game and run it!

Aside from giving you the idea of how the basic game logic components works, "Guess" features a simple Artificial Intelligence (AI) algorithm which allows the computer to tell you whether the number you are guessing is higher or lower than the target number. There is really not much more to it and at this point we are ready to start writing the game. So fire up your Microsoft Visual C++ IDE and create a new console application. The main() function is known as the entry point. This function returns an integer (0), which means a graceful exit (we are exiting the application, but not because we encountered an error).


Download Guess C++ source code [here] This console application should compile under Microsoft Visual C++.


Here is an example of a possible situation in the game "Guess". Unfortunately, the player loses the game. Notice that the functions InitGame() and Destroy() are called at the beginning of the game and at the end respectively.

Playing 'Guess'

Please log in or register new account to post comments on this page
If someone thumbs up your comment, your gain +1 mana (authority) on this page's subject.
If someone thumbs down your comment, you lose 1 mana.
If your comment is thumbed down by the author of this page, you will lose 3 manas.

Are you good at writing? Then Authentic Society needs your help! Add your own articles to the site!

Other articles you might like:

Programming 3D Physics for Computer Games and Interactive Simulations
First Windows Application (Win32)
Simple OpenGL Primitives: Drawing Points, Lines and Polygons with glVertex
PHP Reference Manual and Tutorials for beginners
Entry Point (Programming)
Thoughts on A.I.
The Genesis of 3D Graphics
Polygon vs. Rectangle Clipping Algorithm

You need to be logged in to have access to all parts of the site. Luckily, Authentic Society is free to join.

© 2007-08 Authentic Society. Page load time: 0.718ms