Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Play this amazing car game...
C++
‪#‎include‬ <stdio.h>
#include <conio.h>
#include <graphics.h>
#include <stdlib.h>
‪#‎define‬ RIGHT 19750
#define LEFT 19500
#define UP 18500
#define DOWN 20500
int carArr[10][5];
unsigned long int score = 0;

//the main function() 
void main() {
int gdriver = DETECT, gmode;
initgraph( &gdriver, &gmode, "c:\\TurboC3\\BGI" );
drawPath();
play();
closegraph();
}
//to draw the path
void Path() {
bar(210,40,222,440);
bar(318,40,322,440);
bar(418,40,430,440);
}
//draw the car at given position and in given color
void Car( int row, int col, int last, int color ) {
int r = row,c = col;
switch( last ) {
case LEFT : c = col + 1;
break;
case RIGHT : c = col - 1;
break;
case UP : r = row + 1;
break;
case DOWN : r = row - 1;
break;
}
setfillstyle( SOLID_FILL, BLACK ); // to erase
bar( 260 + c * 100 , 40 + r * 80 , 280 + c * 100 , 60 + r * 80 );
bar( 240 + c * 100 , 60 + r * 80 , 300 + c * 100 , 80 + r * 80 );
bar( 260 + c * 100 , 80 + r * 80 , 280 + c * 100 , 100 + r * 80 );
bar( 240 + c * 100 , 100 + r * 80 , 300 + c * 100 , 120 + r * 80 );
setfillstyle( SOLID_FILL, color ); // to draw
bar( 260 + col * 100 , 40 + row * 80 , 280 + col * 100 , 60 + row * 80 );
bar( 240 + col * 100 , 60 + row * 80 , 300 + col * 100 , 80 + row * 80 );
bar( 260 + col * 100 , 80 + row * 80 , 280 + col * 100 , 100 + row * 80 );
bar( 240 + col * 100 , 100 + row * 80 , 300 + col * 100 , 120 + row * 80 );
carArr[row][col] = 1;
carArr[r][c] = 0;
}
//erase the car and place it in new position
//when we make a move
void erase( int c ) {
int r = 4;
setfillstyle( SOLID_FILL, BLACK ); // to erase
bar( 260 + c * 100 , 40 + r * 80 , 280 + c * 100 , 60 + r * 80 );
bar( 240 + c * 100 , 60 + r * 80 , 300 + c * 100 , 80 + r * 80 );
bar( 260 + c * 100 , 80 + r * 80 , 280 + c * 100 , 100 + r * 80 );
bar( 240 + c * 100 , 100 + r * 80 , 300 + c * 100 , 120 + r * 80 );
carArr[r][c] = 0;
}
//this function will tell where we have to place the enemy car
void enemy( int row, int col) {
if( carArr[ row ][ col ] == 0 ) {
if( row < 5 ) {
Car( row++, col , DOWN, WHITE );
}
} else if( carArr[ row ][ col ] == 1 ) {
gotoxy(1,1);
printf("You Lose !!!!");
getch();
exit(0);
}
}
//random positions and numbers for enemy cars
//This provides instructions acc to player
//move!!
//speed increase with scores
void play() {
int key;
int row = 4, col = 0;
int r = row,c = col;
unsigned int last;
int ctr1 = 0, ctr2 = -2;
int col1, col2, col3;
col1 = rand() % 2;
col2 = rand() % 2;
col3 = rand() % 2;
Car( row, col, RIGHT, RED );
while( key != 283 ) {
while( bioskey( 1 ) == 0 ) {
while( kbhit() == 0 ) {
gotoxy(1,5);
printf("Score : %lu", score);
if( ctr1 < 5 ) {
enemy( ctr1 , col1 );
enemy( ctr2 , col2 );
if( ctr1 == 4 ) {
enemy( 0 , col3 );
}
} else {
score += 100;
gotoxy(1,5);
printf("Score : %lu", score);
eraseBottom(col1);
ctr1 = ctr2;
ctr2 = 1;
col1 = col2;
col2 = col3;
col3 = rand() % 2;
enemy( ctr1 , col1 );
enemy( ctr2 , col2 );
}
ctr1++;
ctr2++;
//delay(500);
if( score < 1000 )
delay(750);
else if( score < 2000 )
delay(500);
else if( score < 3000 )
delay(400);
else if( score < 4000 )
delay( 350 );
else if( score < 5000 )
delay(300);
}
}
key = bioskey( 0 );
switch( key ) {
case LEFT : c = col - 1;
last = LEFT;
break;
case RIGHT : c = col + 1;
last = RIGHT;
break;
case UP : r = row - 1;
last = UP;
break;
case DOWN : r = row + 1;
last = DOWN;
break;
}
if( c < 0 ) c = 0;
if( r < 0 ) r = 0;
if( c > 1 ) c = 1;
if( r > 4 ) r = 4;
if( carArr[r][c] != 0 ) {
gotoxy(1,1);
printf("You Lose !!!!");
getch();
exit(0);
}
Car(r,c,last, RED);
row = r;
col = c;
}
}
Posted
Updated 3-Aug-14 5:02am
v2

Sorry, we do not do your work for you. Please read Code Project Quick Answers FAQ[^]. If you want help then please format your code properly, and add a proper question.
 
Share this answer
 
Comments
ankit00 3-Aug-14 12:07pm    
what i wanted to ask was , can it be further optimized ?
Richard MacCutchan 3-Aug-14 13:08pm    
Yes, probably by throwing it away and redesigning it in something closer to 21st century technology.
There is really no need (and possibly no way) to optimize such a code: it should run fast as a bullet an all but very ancient PCs.
 
Share this answer
 
Comments
ankit00 3-Aug-14 12:08pm    
did you find the code elegant ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900