0% found this document useful (0 votes)
12 views10 pages

Computer Graphics Project Report

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Computer Graphics Project Report

Uploaded by

hetavimodi2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ASSIGNMENT – 8(MINI PROJECT)

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)

Learning Objective: To implement basic game mechanics using


graphics programming in C.

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
}

Installation of Graphics.h on Turbo C++:


To set up the graphics.h library in Turbo C++:
1. Download and install Turbo C++ IDE.
2. Copy the graphics.h, winbgim.h, and other required files to the
Turbo C++ include directory.
3. Place the corresponding .lib files in the Turbo C++ lib directory.

Prerequisites for Graphics.h:


Graphics.h requires a C compiler (like Turbo C++) capable of running
in a DOS environment. Basic knowledge of C programming is
essential to understand the structure of the game.

Getting Started with the Game:


Overview of the Game Structure:
- Main Function: Initializes the graphics mode and starts the game
loop.
- Game Loop: Continuously updates the game state, processes user
input, and redraws the screen.
- Drawing Functions: Called to render the player’s car and obstacles
on the screen.
- Input Handling: Detects keyboard inputs to control the car and
handle game events such as collisions.
Program Flow:
1. Initialize graphics using initgraph().
2. Set up game variables (car position, obstacle positions, scores).
3. Enter the game loop to handle rendering, input, and game logic.
4. Check for collisions and update the score.
5. Display game over message and options to restart

=============================================
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>

// Draw lane dividers (move them


int carX = 300; // Car's X position
downward to simulate car moving
(lane)
forward)
int carY = 400; // Car's initial Y
setcolor(WHITE);
position (stationary Y)
for (int i = laneY; i < getmaxy(); i
int score = 0; // Game score
+= 40) {
int highScore = 0; // High score
line(getmaxx() / 2 - 5, i,
int obstacleX[3]; // X positions of getmaxx() / 2 - 5, i + 20); // Dashed
obstacles (three lanes) lane line
int obstacleY[3]; // Y positions of }
obstacles
int laneY = 0; // Y position for the
// Reset laneY when it goes off-
moving lane dividers
screen (loop effect)
bool gameOver = false;
laneY += 5;
if (laneY > 40) {
// Function to draw the road (with
laneY = 0;
moving lane dividers)
}
} bar(obstacleX[i], obstacleY[i],
obstacleX[i] + 40, obstacleY[i] + 40);
// Square obstacles
// Function to draw the car
}
void drawCar() {
}
// Set car color to blue
}
setfillstyle(SOLID_FILL, BLUE);
bar(carX, carY, carX + 60, carY +
// Function to move obstacles based
30); // Car body
on car's forward movement
void moveObstacles() {
// Car wheels (black)
for (int i = 0; i < 3; i++) {
setfillstyle(SOLID_FILL, BLACK);
obstacleY[i] += 5; // Move
circle(carX + 15, carY + 30, 10); obstacles down
floodfill(carX + 15, carY + 30,
BLACK);
// Reset obstacle after it moves
circle(carX + 45, carY + 30, 10); off screen
floodfill(carX + 45, carY + 30, if (obstacleY[i] > getmaxy()) {
BLACK);
obstacleY[i] = -40; // Reset
} obstacle position (above the screen)
obstacleX[i] = 150 + (rand() %
// Function to draw the obstacles 3) * 150; // Place it in a random
(appear and disappear as car lane
moves) }
void drawObstacles() { }
setfillstyle(SOLID_FILL, WHITE); // }
Obstacles are white
for (int i = 0; i < 3; i++) {
// Function to reset the game
if (obstacleY[i] >= 0 &&
void resetGame() {
obstacleY[i] < getmaxy()) {
score = 0;
for (int i = 0; i < 3; i++) { outtextxy(getmaxx() - 200, 10,
highScoreText);
obstacleX[i] = 150 + i * 150; //
Obstacles in each lane }
obstacleY[i] = -200 - i * 200; //
Random Y positions above the
// Main game loop
screen
void gameLoop() {
}
while (!gameOver) {
laneY = 0;
// Draw everything in one
gameOver = false;
frame
}
drawRoad();
drawCar();
// Function to display the score
drawObstacles();
void displayScore() {
displayScore();
char scoreText[20];
displayHighScore();
setcolor(WHITE);
settextstyle(3, HORIZ_DIR, 2);
// Move obstacles as the car
sprintf(scoreText, "Score: %d", moves forward
score);
moveObstacles();
outtextxy(10, 10, scoreText);
}
// Check if car hits an obstacle
for (int i = 0; i < 3; i++) {
// Function to display the high score
if (obstacleY[i] >= carY &&
void displayHighScore() { obstacleY[i] <= carY + 30 &&
obstacleX[i] == carX) {
char highScoreText[20];
gameOver = true; // Game
setcolor(WHITE);
over if car hits an obstacle
settextstyle(3, HORIZ_DIR, 2);
}
sprintf(highScoreText, "High
}
Score: %d", highScore);
} else if (ch == 27) { // Escape
key to exit
// Increase score as the car
moves forward gameOver = true; //
Terminate the game if escape is
score += 1;
pressed
if (score > highScore) {
}
highScore = score; // Update
}
high score
}
}

// Display game over message


// Control game speed
cleardevice(); // Clear screen for
delay(30);
the game over message
settextstyle(3, HORIZ_DIR, 3);
// Handle user input to move
outtextxy(getmaxx() / 2 - 100,
the car between lanes and forward
getmaxy() / 2, "Game Over!");
if (kbhit()) {
outtextxy(getmaxx() / 2 - 200,
char ch = getch(); getmaxy() / 2 + 30, "Press any key to
if (ch == 75 && carX > 150) restart");
{ // Left arrow key
carX -= 150; // Move to the // Wait for key press to restart
left lane the game
} else if (ch == 77 && carX < getch();
getmaxx() - 250) { // Right arrow key
resetGame(); // Restart game
carX += 150; // Move to the
}
right lane
} else if (ch == 72 && carY >
0) { // Up arrow key // Main function
carY -= 20; // Move car int main() {
forward
int gd = DETECT, gm;
initgraph(&gd, &gm, NULL);
srand(time(NULL)); // Seed the
random number generator
closegraph(); // Close the
graphics window
resetGame(); // Initialize game return 0;
variables
}
gameLoop(); // Start the game
loop

Output :

You might also like