Car Racing Game In C Source-code
Car Racing Game in C Source-code
Car Racing Game in C Source-code
A car racing game is a genre of video games, either in the first-person or third-person perspective, in which the player partakes in a racing competition with any type of land, water, air, or space vehicles. Car racing games are popular among gamers of all ages and skill levels, as they offer a thrilling and immersive experience of speed, control, and competition. In this article, we will explore how to create a simple car racing game in C using graphics.
Graphics Library in C
To create a car racing game in C, we need to use a graphics library that can provide us with functions to draw shapes, colors, images, and animations on the screen. One of the most widely used graphics libraries in C is the Borland Graphics Interface (BGI), which was originally developed for the Turbo C compiler. BGI provides a set of functions to initialize the graphics mode, draw basic shapes like lines, circles, rectangles, polygons, etc., fill colors, display text, load and display images, and manipulate pixels. BGI also supports keyboard and mouse input, which are essential for creating interactive games.
Download Zip: https://t.co/nSB78N0FEH
To use BGI in C, we need to include the header file graphics.h in our program. We also need to link the BGI library file libbgi.a and the BGI driver files egavga.bgi or bgi.scr depending on the graphics mode we want to use. The BGI library file and driver files are usually located in the bgi folder of the Turbo C installation directory. We can specify the path of these files using the linker options in our compiler settings.
Initializing the Graphics Mode
The first step to create a car racing game in C using graphics is to initialize the graphics mode. The graphics mode determines the resolution and color depth of the screen. BGI supports several graphics modes, such as CGA, EGA, VGA, SVGA, etc., each with different resolutions and color depths. To initialize the graphics mode, we need to use the function initgraph(), which takes three parameters: a pointer to an integer variable that stores the error code if any, a pointer to a string that specifies the path of the BGI driver file, and a pointer to a string that specifies the path of the BGI library file. For example:
int gd = DETECT; // auto-detect the graphics mode int gm; // variable to store the error code char *driver = "C:\\TURBOC3\\BGI\\EGAVGA.BGI"; // path of the driver file char *mode = "C:\\TURBOC3\\BGI"; // path of the library file initgraph(&gd,&gm,driver); // initialize the graphics mode
The function initgraph() sets the graphics mode to VGA with 640x480 resolution and 16 colors by default. If we want to use a different graphics mode, we can specify it using the variable gd. For example:
int gd = EGAHI; // set the graphics mode to EGA with 640x350 resolution and 16 colors int gm; char *driver = "C:\\TURBOC3\\BGI\\EGAVGA.BGI"; char *mode = "C:\\TURBOC3\\BGI"; initgraph(&gd,&gm,driver);
The function initgraph() also checks for any errors that may occur while initializing the graphics mode. If there is an error, it sets the variable gm to a non-zero value and displays an error message on the screen. We can also use the function graphresult() to get the error code and use it in a switch statement to display custom error messages. For example:
int gd = DETECT; int gm; char *driver = "C:\\TURBOC3\\BGI\\EGAVGA.BGI"; char *mode = "C:\\TURBOC3\\BGI"; initgraph(&gd,&gm,driver); switch(graphresult()) // check for errors case grOk: // no error break; case grNoInitGraph: // graphics mode not initialized printf("Graphics mode not initialized\n"); exit(1); case grDriverNotFound: // driver file not found printf("Driver file not found\n"); exit(1); case grInvalidDriver: // invalid driver file printf("Invalid driver file\n"); exit(1); case grInvalidMode: // invalid graphics mode printf("Invalid graphics mode\n"); exit(1); default: // other errors printf("Unknown error\n"); exit(1);
Drawing the Road and the Car
After initializing the graphics mode, we can start drawing the road and the car on the screen. To draw the road, we can use the function rectangle(), which takes four parameters: the x and y coordinates of the top-left corner and the bottom-right corner of the rectangle. We can also use the function setfillstyle() to set the fill pattern and color of the rectangle, and the function floodfill() to fill the rectangle with the specified color. For example:
setfillstyle(SOLID_FILL,LIGHTGRAY); // set the fill pattern to solid and color to light gray rectangle(100,0,540,480); // draw a rectangle with top-left corner at (100,0) and bottom-right corner at (540,480) floodfill(101,1,LIGHTGRAY); // fill the rectangle with light gray color
To draw the car, we can use the function line(), which takes four parameters: the x and y coordinates of the starting point and the ending point of the line. We can also use the function setcolor() to set the color of the line. To draw a simple car, we can use four lines to draw a trapezoid for the body of the car, and two circles for the wheels of the car. We can also use the function setbkcolor() to set the background color of the car, so that it does not overlap with the road. For example:
setcolor(BLACK); // set the color to black setbkcolor(LIGHTGRAY); // set the background color to light gray line(300,400,340,400); // draw a line from (300,400) to (340,400) line(300,400,280,420); // draw a line from (300,400) to (280,420) line(340,400,360,420); // draw a line from (340,400) to (360,420) line(280,420,360,420); // draw a line from (280,420) to (360,420) circle(290,430,10); // draw a circle with center at (290,430) and radius 10 circle(350,430,10); // draw a circle with center at (350,430) and radius 10
Moving the Car
To make the car racing game more interactive, we need to move the car according to the user's input. To do this, we need to use the functions kbhit() and getch() to detect if a key is pressed and get its ASCII code. We can use a switch statement to check which key is pressed and move the car accordingly. To move the car left or right, we need to change its x coordinates by adding or subtracting a certain amount. To move the car up or down, we need to change its y coordinates by adding or subtracting a certain amount. To erase the previous position of the car, we need to redraw it with the same background color as the road. For example:
while(1) // infinite loop if(kbhit()) // check if a key is pressed char ch = getch(); // get the ASCII code of the key switch(ch) // check which key is pressed case 'a': // move left setcolor(LIGHTGRAY); // set color to light gray setbkcolor(LIGHTGRAY); // set background color to light gray line(300,400,340,400); // erase previous position of car by redrawing it with same color as road line(300,400,280,420); line(340,400,360,420); line(280,420,360,420); circle(290,430,10); // erase previous position of car by redrawing it with same color as road circle(350,430,10); setcolor(BLACK); // set color to black setbkcolor(LIGHTGRAY); // set background color to light gray line(290,400,330,400); // draw new position of car by shifting x coordinates by -10 line(290,400,270,420); line(330,400,350,420); line(270,420,350,420); circle(280,430,10); // draw new position of wheels by shifting x coordinates by -10 circle(340,430,10); break; case 'd': // move right setcolor(LIGHTGRAY); // set color to light gray setbkcolor(LIGHTGRAY); // set background color to light gray line(300,400,340,400); // erase previous position of car by redrawing it with same color as road line(300,400,280,420); line(340,400,360,420); line(280,420,360,420); circle(290,430,10); // erase previous position of wheels by redrawing them with same color as road circle(350,430,10); setcolor(BLACK); // set color to black setbkcolor(LIGHTGRAY); // set background color to light gray line(310,400,350,400); // draw new position of car by shifting x coordinates by +10 line(310,400,290,420); line(350,400,370,420); line(290,420,370,420); circle(300,430,10); // draw new position of wheels by shifting x coordinates by +10 circle(360, circle(360,430,10); // draw new position of wheels by shifting x coordinates by +10 break; case 'w': // move up setcolor(LIGHTGRAY); // set color to light gray setbkcolor(LIGHTGRAY); // set background color to light gray line(300,400,340,400); // erase previous position of car by redrawing it with same color as road line(300,400,280,420); line(340,400,360,420); line(280,420,360,420); circle(29