Computer Graphics Project Report
Computer Graphics Project Report
Title:
Implementation of a Car Racing Game using C++ with Graphics.h
Aim:
Design and implement a simple car racing game using the graphics.h
library in C. The project aims to demonstrate the use of fundamental
programming techniques and graphics rendering capabilities.
Pre-requisite:
1. Basic programming skills in C
2. Graphics.h library
3. A compatible compiler (e.g., Turbo C++ or any environment
supporting graphics.h)
Prepared By:
Name: Hetavi modi Name: Snehal Gahavane
Batch: B3 Batch: B3
Roll no.: COSB63 Roll no. : COSB62
Theory:
Graphics Library Basics:
The graphics.h library is a graphics programming interface for C that
provides functions for drawing 2D graphics, including shapes, colors,
and text. It is typically used for educational purposes to teach basic
graphics programming concepts. The library allows users to create
simple games and animations by providing easy-to-use functions for
rendering graphics.
Design:
The graphics.h library includes a variety of functions for drawing
shapes, filling areas with color, and handling user input. The library
uses a pixel-based rendering approach, allowing the programmer to
control the graphics at a low level.
Development:
While graphics.h is relatively straightforward, it is limited to 2D
graphics and may not support more advanced rendering techniques
found in modern graphics libraries. However, it remains a useful tool
for simple projects and educational purposes.
Associated Libraries:
Although graphics.h is a standalone library, it can be complemented
with additional libraries for handling more complex graphics, sound,
and user input. For advanced graphics projects, libraries like SDL
(Simple DirectMedia Layer) or SFML (Simple and Fast Multimedia
Library) can be used.
Implementation:
The project utilizes the graphics.h library to create a car racing game
where the player controls a car and avoids obstacles. The game
consists of the following components:
1. Car Drawing Function: Responsible for rendering the player's car
on the screen.
2. Obstacle Drawing Function:Draws obstacles (like other cars) on the
screen.
3. Movement Functions: Functions to move obstacles down the
screen and detect collisions with the player’s car.
4. Control Functions: Handles user input to control the car’s
movement.
5. Game Loop:The core of the game where rendering, input handling,
and collision detection occur.
Syntax :
1. Initialization:
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
2. Drawing Shapes:
rectangle(x1, y1, x2, y2); // Draws a rectangle
setfillstyle(SOLID_FILL, BLUE);
floodfill(x, y, WHITE); // Fills the shape with color
3. Handling Input:
if (GetAsyncKeyState(VK_LEFT)) { /* Move left */ }
if (GetAsyncKeyState(VK_RIGHT)) { /* Move right */ }
4. Collision Detection:
if (checkCollision(carX, carY, obsX, obsY)) { /* Handle collision */ }
5. Game Loop:
while (1) {
// Game logic and rendering
}
=============================================
Program: #include <graphics.h> void drawRoad() {
#include <conio.h> setfillstyle(SOLID_FILL, BLACK);
#include <stdlib.h> bar(100, 0, getmaxx() - 100,
getmaxy()); // Black road
#include <time.h>
Output :