0% found this document useful (0 votes)
46 views

C++ Code

This C++ program defines a car game with the following key elements: 1. It includes header files for input/output functions and sets screen dimensions. 2. It defines variables to track the car position, score, and enemy positions. 3. It includes functions to draw and erase the car, enemies, and borders and to detect collisions. 4. The main function displays a menu to start the game, view instructions, or quit. It calls the play function which runs the game loop moving and drawing the car and enemies until a collision occurs.

Uploaded by

Sameer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

C++ Code

This C++ program defines a car game with the following key elements: 1. It includes header files for input/output functions and sets screen dimensions. 2. It defines variables to track the car position, score, and enemy positions. 3. It includes functions to draw and erase the car, enemies, and borders and to detect collisions. 4. The main function displays a menu to start the game, view instructions, or quit. It calls the play function which runs the game loop moving and drawing the car and enemies until a collision occurs.

Uploaded by

Sameer
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

#include<iostream>

#include<conio.h>

#include<dos.h>

#include <windows.h>

#include <time.h>

#define SCREEN_WIDTH 90

#define SCREEN_HEIGHT 26

#define WIN_WIDTH 70

Using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);

COORD CursorPosition;

Int enemyY[3];

Int enemyX[3];

Int enemyFlag[3];

Char car[4][4] = { ‘ ‘,’±’,’±’,’ ‘,

‘±’,’±’,’±’,’±’,

‘ ‘,’±’,’±’,’ ‘,

‘±’,’±’,’±’,’±’ };

Int carPos = WIN_WIDTH/2;

Int score = 0;

Void gotoxy(int x, int y){

CursorPosition.X = x;

CursorPosition.Y = y;
SetConsoleCursorPosition(console, CursorPosition);

Void setcursor(bool visible, DWORD size) {

If(size == 0)

Size = 20;

CONSOLE_CURSOR_INFO lpCursor;

lpCursor.bVisible = visible;

lpCursor.dwSize = size;

SetConsoleCursorInfo(console,&lpCursor);

Void drawBorder(){

For(int i=0; i<SCREEN_HEIGHT; i++){

For(int j=0; j<17; j++){

Gotoxy(0+j,i); cout<<”±”;

Gotoxy(WIN_WIDTH-j,i); cout<<”±”;

For(int i=0; i<SCREEN_HEIGHT; i++){

Gotoxy(SCREEN_WIDTH,i); cout<<”±”;

Void genEnemy(int ind){

enemyX[ind] = 17 + rand()%(33);

Void drawEnemy(int ind){

If( enemyFlag[ind] == true ){

Gotoxy(enemyX[ind], enemyY[ind]); cout<<”****”;

Gotoxy(enemyX[ind], enemyY[ind]+1); cout<<” ** “;


Gotoxy(enemyX[ind], enemyY[ind]+2); cout<<”****”;

Gotoxy(enemyX[ind], enemyY[ind]+3); cout<<” ** “;

Void eraseEnemy(int ind){

If( enemyFlag[ind] == true ){

Gotoxy(enemyX[ind], enemyY[ind]); cout<<” “;

Gotoxy(enemyX[ind], enemyY[ind]+1); cout<<” “;

Gotoxy(enemyX[ind], enemyY[ind]+2); cout<<” “;

Gotoxy(enemyX[ind], enemyY[ind]+3); cout<<” “;

Void resetEnemy(int ind){

eraseEnemy(ind);

enemyY[ind] = 1;

genEnemy(ind);

Void drawCar(){

For(int i=0; i<4; i++){

For(int j=0; j<4; j++){

Gotoxy(j+carPos, i+22); cout<<car[i][j];

Void eraseCar(){

For(int i=0; i<4; i++){

For(int j=0; j<4; j++){

Gotoxy(j+carPos, i+22); cout<<” “;


}

Int collision(){

If( enemyY[0]+4 >= 23 ){

If( enemyX[0] + 4 – carPos >= 0 && enemyX[0] + 4 – carPos < 9 ){

Return 1;

Return 0;

Void gameover(){

System(“cls”);

Cout<<endl;

Cout<<”\t\t--------------------------“<<endl;

Cout<<”\t\t-------- Game Over -------“<<endl;

Cout<<”\t\t--------------------------“<<endl<<endl;

Cout<<”\t\tPress any key to go back to menu.”;

Getch();

Void updateScore(){

Gotoxy(WIN_WIDTH + 7, 5);cout<<”Score: “<<score<<endl;

Void instructions(){

System(“cls”);

Cout<<”Instructions”;
Cout<<”\n----------------“;

Cout<<”\n Avoid Cars by moving left or right. “;

Cout<<”\n\n Press ‘a’ to move left”;

Cout<<”\n Press ‘d’ to move right”;

Cout<<”\n Press ‘escape’ to exit”;

Cout<<”\n\nPress any key to go back to menu”;

Getch();

Void play(){

carPos = -1 + WIN_WIDTH/2;

score = 0;

enemyFlag[0] = 1;

enemyFlag[1] = 0;

enemyY[0] = enemyY[1] = 1;

system(“cls”);

drawBorder();

updateScore();

genEnemy(0);

genEnemy(1);

gotoxy(WIN_WIDTH + 7, 2);cout<<”Car Game”;

gotoxy(WIN_WIDTH + 6, 4);cout<<”----------“;

gotoxy(WIN_WIDTH + 6, 6);cout<<”----------“;

gotoxy(WIN_WIDTH + 7, 12);cout<<”Control “;

gotoxy(WIN_WIDTH + 7, 13);cout<<”-------- “;

gotoxy(WIN_WIDTH + 2, 14);cout<<” A Key – Left”;

gotoxy(WIN_WIDTH + 2, 15);cout<<” D Key – Right”;


gotoxy(18, 5);cout<<”Press any key to start”;

getch();

gotoxy(18, 5);cout<<” “;

while(1){

if(kbhit()){

char ch = getch();

if( ch==’a’ || ch==’A’ ){

if( carPos > 18 )

carPos -= 4;

If( ch==’d’ || ch==’D’ ){

If( carPos < 50 )

carPos += 4;

If(ch==27){

Break;

drawCar();

drawEnemy(0);

drawEnemy(1);

if( collision() == 1 ){

gameover();

return;

Sleep(50);
eraseCar();

eraseEnemy(0);

eraseEnemy(1);

if( enemyY[0] == 10 )

if( enemyFlag[1] == 0 )

enemyFlag[1] = 1;

if( enemyFlag[0] == 1 )

enemyY[0] += 1;

if( enemyFlag[1] == 1 )

enemyY[1] += 1;

if( enemyY[0] > SCREEN_HEIGHT-4 ){

resetEnemy(0);

score++;

updateScore();

If( enemyY[1] > SCREEN_HEIGHT-4 ){

resetEnemy(1);

score++;

updateScore();

Int main()

{
Setcursor(0,0);

Srand( (unsigned)time(NULL));

Do{

System(“cls”);

Gotoxy(10,5); cout<<” -------------------------- “;

Gotoxy(10,6); cout<<” | Car Game | “;

Gotoxy(10,7); cout<<” --------------------------“;

Gotoxy(10,9); cout<<”1. Start Game”;

Gotoxy(10,10); cout<<”2. Instructions”;

Gotoxy(10,11); cout<<”3. Quit”;

Gotoxy(10,13); cout<<”Select option: “;

Char op = getche();

If( op==’1’) play();

Else if( op==’2’) instructions();

Else if( op==’3’) exit(0);

}while(1);

Return 0;

You might also like