Windows.h In Dev C%2b%2b



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.

The C library function void exit(int status) terminates the calling process immediately. Any open file descriptors belonging to the process are closed and any children of the process are inherited by process 1, init, and the process parent is sent a SIGCHLD signal. C is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell Labs.C runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. This C tutorial adopts a simple and practical approach to describe the concepts of C for beginners to advanded software engineers. Why to Learn C. C is a MUST for students and working.

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

Dev
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

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.

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!

Windows.h In Dev C%2b%2b

1. Drawing a simple Rectangle

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.

Windows.h In Dev C 2b 2b 1

Example

Dev

Windows.h In Dev C 2b 2b 3

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

Windows.h In Dev C 2b 2b 4

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

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.

Windows.h In Dev C 2b 2b 1b

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!