0% found this document useful (0 votes)
31 views2 pages

New Text Document

new

Uploaded by

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

New Text Document

new

Uploaded by

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

#include <conio.

h>
#include <graphics.h>
#include <dos.h>
#include <stdlib.h>

// Function to draw the road


void drawRoad() {
setcolor(WHITE);
rectangle(150, 0, 300, getmaxy());
line(225, 0, 225, getmaxy());
}

// Function to draw the car


void drawCar(int x, int y) {
setcolor(GREEN);
rectangle(x, y, x + 40, y + 60); // Car body
rectangle(x + 5, y + 10, x + 35, y + 50); // Car windows
}

// Function to draw an obstacle (random blocks representing other cars or


roadblocks)
void drawObstacle(int x, int y) {
setcolor(RED);
rectangle(x, y, x + 40, y + 60); // Obstacle body
}

// Function to check if car hits the obstacle


int checkCollision(int carX, int carY, int obsX, int obsY) {
if (carY < obsY + 60 && carY + 60 > obsY) {
if (carX < obsX + 40 && carX + 40 > obsX) {
return 1; // Collision detected
}
}
return 0; // No collision
}

void main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI");

int carX = 180, carY = 350; // Car initial position


int obsX = 180, obsY = 0; // Obstacle initial position
int score = 0;
char ch;

// Game loop
while (1) {
cleardevice(); // Clear the screen

// Draw road, car, and obstacle


drawRoad();
drawCar(carX, carY);
drawObstacle(obsX, obsY);

// Move the obstacle down


obsY += 10;

// Reset obstacle if it goes off the screen


if (obsY > getmaxy()) {
obsY = 0;
obsX = (rand() % 2) ? 180 : 240; // Randomize obstacle position
score++;
}

// Check for collision


if (checkCollision(carX, carY, obsX, obsY)) {
setcolor(RED);
outtextxy(200, 200, "GAME OVER!");
break;
}

// Car controls
if (kbhit()) {
ch = getch();
if (ch == 27) // Exit if ESC key is pressed
break;
if (ch == 75 && carX > 160) // Left arrow key
carX -= 10;
if (ch == 77 && carX < 260) // Right arrow key
carX += 10;
}

// Display score
setcolor(WHITE);
char scoreStr[10];
sprintf(scoreStr, "Score: %d", score);
outtextxy(10, 10, scoreStr);

// Slow down the game loop for better control


delay(100);
}

// Wait for user to press a key before exiting


getch();
closegraph();
}

You might also like