Dev C%2b%2b Game Code

Tank Game in C++ is s simple application developed as a gaming project in C++ programming language. Tank Game can be called “tank war game” too, as it seems to be inspired from the battle city game to some extent. This project can be used to learn C++, its features and application in simple game development. The source code is completely error free and well tested for the bugs.

The game is written much more in the procedural style of C rather than in the object-oriented style of C. The game itself could be an object, with most of the procedures as functions of that object. This would reduce coupling and make the program easier to understand. Also, each of the blocks could quite obviously be an object. Game Intro Game Canvas Game Components Game Controllers Game Obstacles Game Score Game Images Game Sound Game. Try it Yourself Examples. With our online editor, you can edit the code, and click on a button to view the result. Function startGame myGamePiece = new component(30, 30, 'red', 10, 120); myGamePiece.gravity = 0.05.

Numerous comment lines have been embedded in the source codes to help you understand the project better. You can find an in-depth explanation of Tank game development along with the step-by-step process required to complete this final year project in the download files.

Further explanations on how to run and play the game have also been provided. Simple download the project file, unzip the downloaded file and go through the “tutorial file”.

Download Tank Game in C++ with Source Code and Program Files

[sociallocker]

Download Tank Game in C++with Source Code and Program Files

[/sociallocker]

Tank Game – Project Introduction:

Tank Game is a war game which is played by two players at a time and both of them are given tanker and powers for fighting on other person’s post. Every tank has a command place from where you start playing. While playing Tank Game in C++, major tasks you have to do is collect the items which are placed around the arena and get score as much as possible by fighting the opponent.

The more you collect gold or attack and destroy the enemy base camp or enemy tank, the more score you earn. The decision of the winner is taken based on the points or scores earned by the player by the end of game. I have listed the scoring keys or points awarded for specific events below. And, you can find more about the game in the download file itself.

To run the game, simply open the terminal, and type “make play“.

Header Files Used:

These header files are the building blocks of the project source code. Each of them has been designed to perform certain specific task in game controlling. You can find detailed description about header files in the “tutorial file” obtained from the download link provided within this post. The major header files which has been defined in the source code of Tank Game in C++ are listed as follows:

  • Arena.h
  • Constants.h
  • DecisionMaker.h
  • DecisionMaker1.h
  • DecisionMaker2.h
  • Info.h
  • Map.h
  • Misc_Classes.h
  • Tank.h

Score/Points:

Code

Screenshots:

Also see,
Helicopter Game in C++
Bike Game in C++
More C++ Projects

The use of effective graphics has made the game interesting to some extent. I hope, you’ll use this tank game in c++ for academic purpose – learning game development techniques using C++. The overall project has been tried to make bug free, so your valuable suggestions and feedbacks regarding this game can be mentioned in the comments section.



In this short tutorial, I've provided the source codes which you can use as a learning aid. You can copy and paste them into your compiler and see the outcomes with ease.
Welcome to Game Programming in C++ console!
This article will guide you to develop a very simple car racing game in windows console. In the first part of the tutorial, I've listed a few fascinating functions that c++ provides, needed for our game. In the second, I've tried to create my own functions using those functions from the first part while also explaining in detail how they work. In the third (last) part, all the functions I've prepared in the previous parts are all put together to make the final c++ game.
There is no better way to improve your programming skills other than writing codes and the more challenging codes you write and try to understand, the more knowledge you will grasp.
The language of choice in this tutorial is C++ and the compiler is CodeBocks. Other compilers such as Dev C++, Microsoft Visual C++, etc… should work fine as well.
If you are not familiar with topics on C++ such as the following:
1. C++ Pointers
2. C++ Functions/Procedures
3. C++ Classes
Then, I suggest you do a quick review of them first.

Download Source Code

1. The Sleep( ) function

The sleep function is used to suspend/delay the execution of the program for sometime. The value which is passed to the sleep function as an argument is in milliseconds. This function exists in windows.h library in C++. So, you must include 'windows.h' header at the top.

Example

If you compile and run the above code, you will notice that the for loop runs very slowly, one cycle per second (per 1000 milliseconds). That's because the value of myDelay is set to 1000 milliseconds. The program suspends or rests for 1 second after displaying the value of 'i'(After the 'cout' instruction).

2. Changing the Cursor Position using gotoXY()

GotoXY is used to move the cursor on the console screen. It positions the cursor at (X,Y), X in horizontal, Y in vertical direction relative to the origin of your console window. So, if you put 'gotoXY(5,7);' before the statement 'Cout<<'Hello There';' , it will display 'Hello World' 5 digits to the right and 7 digits to the bottom of your console origion/corner.
Note: Before you can use the gotoXY() function, you must define it first! See the example code below.

Example

Dev C 2b 2b Game Code C++

3. Multithreading in C++ II

Say you have five different jobs to do in one hour. You have the choice of doing each work one after the other. You also have the choice of doing all five works at the same time, simultaneously.
With out multithreading, Computer programs can execute only one instruction at a time, starting from the top of your code down to the bottom. By using C++ Multithreading, a game application can draw on the screen in one thread and listen to user inputs on the other, both at the same time. Multithreading: the ability to execute multiple processes or threads concurrently (at the same time). Play with the code below till you are comfortable with using c++ II multithreading .
Note: In order to use the threads in your c++ programs, you must enable C++ II support in codeblocks.
    How TO Enable C++ II ISO Language Standard in CodeBlocks.
  1. Go to Toolbar -> Settings -> Compiler
  2. In the Selected compiler drop-down menu, make sure GNU GCC Compiler is selected
  3. Below that, select the compiler settings tab and then the compiler flags tab underneath
  4. In the list below, make sure the box for 'Have g++ follow the C++11 ISO C++ language standard [-std=c++11]' is checked
  5. Click OK to save.

Dev C 2b 2b Game Code Download


Example

4. Listening to keyboard input

Our game needs only two buttons. 'RIGHT' and 'LEFT' arrows on the keyboard.So, we're gonna listen and respond to only those keys.
GetAsyncKeyState( ): This function determines whether a key is up or down at the time the function is called. It takes one parameter, 'the key code' and the return value specifies whether the key was pressed.
-Some Key Code examples are
We only need the first two 'VK_LEFT' & 'VK_RIGHT' because they represent key codes for the left and the right arrows. The last two are used for mouse clicks which we are not interested in.
The example below shows a usage of this good function to handle input in a windows C/C++ console game.

Example

5. Generating a Random Number

Rand( ): This function generates a random numberbetween 0 & 32767(at least).
If you want to get a random number in a determined range, say between 0 & 100, you can simply use...
int myRand = rand()%100.
And now the integer variable myRand holds some random number between 0 & 100...
And that's it we have finished Part One of this article!

1. Drawing a simple Rectangle

Dev C 2b 2b Game Code Free

In next code example, I've defined a new 'Two-Dimensional Array' of type integer , matrix[8][20]. See line 14 of the example code below.The purpose of this matrix is to hold the information about our Game Screen, which is of width 8 and height 20.
If you assign matrix[3][7]=1, that means you have successfully drawn a point '0' at x=3 & y=7. If, on the other hand, you assign matrix[3][7]=0, it means you have removed that point '0' from the screen, at the position of x=3 & y=7.
startGame(): In line 16, which is basically the games engine, redraws the game screen several times per second. This is the function responsible for drawing a pixel or a point '0' at the position(i,j), only if the value of matrix[i][j] is already assigned to 1. See the 'if' statement at line 24.
The game ends when the 'While Loop' at line 19 stops. If the Boolean variable that I named 'running'(line 18), is assigned to false at any point of the game, this while loop ends. Game Over!!!
In the main function, I've assigned the value 1 to our matrix[x][y] array for different values of x and y. I started at x=2,y=4 and finished at x=5,y=6. If you label all my ten points on a piece of paper with x/y coordinates, you will notice that they create a rectangle.
I'm not gonna explain the gotoXY() definition (line 6) here because I assume you have understood that in part 1.
And that's all... The most difficult part of this article!.
Try to read and understand the example code below, maybe try to draw more shapes... before you proceed to the next example.

Example

2. Drawing the Boarder (Road Sides)


The road sides should be positioned at x=0 & x=7, because the game screen width is up to 8. This can easily be accomplished by adding this segment of code in the above While loop, Game engine

3. Functions to Clear the Screen and to Draw a Point

Dev C%2b%2b Game Code
The function drawPoint(x,y) assigns our matrix with 1 at (x,y). It's easier to remember than assigning matrix every time you wanna draw a point/pixel.
The function resetBoard() clears the screen by assigning all elements of our matrix with 0's. Self Explanatory! look at the example below.

4. Creating the Player Car

The above images show our car in it's two possible positions(Left & Right). You can see that our car consists of 8 points.
That's why the draw() method of this class in line 10 has 8 methods. The car is drawn using 8 drawPoint functions.
It's drawn relative to the variables xPos & yPos, means if you change the value of xPos or yPos, the car will be redrawn at a different position.
In Line 20 & 23, there are functions to control the car's movement.
moveLeft(): assigns xPos to 2. Which changes the X Position of the car. As a result, the car shifts to the left.
moveRight(): assigns xPos to 5. Therefore, The car shifts to the right.
In Line 26, we have the checkCollusion() function which is used for ending the while loop(and the game) if the positions of the two cars match(collide), by assigning the variable 'running' to false or zero.

5. Creating the Other Racing Car

This class is the same as the previous one, except for its movement characteristics. It only changes the vertical position yPos while the previous car only changes the xPos for horizontal shifts.
The move() function increments yPos every time it's called (line 31). That makes the car travel downwards.
The appear() function (line 9), because our game screen is 8x20, the car goes invisible after its yPos value exceeds 20. So, this function recreates this car into the game screen by setting a new yPos value, less than 20 and greater than 0.
The xPos, on the other hand, is randomly decided and its value can only be 2 or 5 (Left or Right).

6. Listener for Keyboard inputs

The code segment below listens for keyboard strokes and responds by moving the car whenever the LEFT or the RIGHT keys are pressed. I already explained this in part I.

Dev C 2b 2b Game Codes


The Complete Source Code

Example


This is the end of Part 3 - Putting It All Together.
And I guess this is also the end of my tutorial on Game programming in C++ Console.
~Until we meet again~
~Peace Out!