Skip to content

Commit a58bf75

Browse files
first commit
0 parents  commit a58bf75

File tree

2 files changed

+226
-0
lines changed

2 files changed

+226
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/cargame.exe

cargame.cpp

+225
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
#include<iostream>
2+
#include<conio.h>
3+
#include<dos.h>
4+
#include <windows.h>
5+
#include <time.h>
6+
7+
#define SCREEN_WIDTH 90
8+
#define SCREEN_HEIGHT 26
9+
#define WIN_WIDTH 70
10+
11+
using namespace std;
12+
13+
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
14+
COORD CursorPosition;
15+
16+
int enemyY[3];
17+
int enemyX[3];
18+
int enemyFlag[3];
19+
char car[4][4] = { ' ','+','+',' ',
20+
'+','+','+','+',
21+
' ','+','+',' ',
22+
'+','+','+','+' };
23+
24+
int carPos = WIN_WIDTH/2;
25+
int score = 0;
26+
27+
void gotoxy(int x, int y){
28+
CursorPosition.X = x;
29+
CursorPosition.Y = y;
30+
SetConsoleCursorPosition(console, CursorPosition);
31+
}
32+
void setcursor(bool visible, DWORD size) {
33+
if(size == 0)
34+
size = 20;
35+
36+
CONSOLE_CURSOR_INFO lpCursor;
37+
lpCursor.bVisible = visible;
38+
lpCursor.dwSize = size;
39+
SetConsoleCursorInfo(console,&lpCursor);
40+
}
41+
void drawBorder(){
42+
for(int i=0; i<SCREEN_HEIGHT; i++){
43+
for(int j=0; j<17; j++){
44+
gotoxy(0+j,i); cout<<"+";
45+
gotoxy(WIN_WIDTH-j,i); cout<<"+";
46+
}
47+
}
48+
for(int i=0; i<SCREEN_HEIGHT; i++){
49+
gotoxy(SCREEN_WIDTH,i); cout<<"+";
50+
}
51+
}
52+
void genEnemy(int ind){
53+
enemyX[ind] = 17 + rand()%(33);
54+
}
55+
void drawEnemy(int ind){
56+
if( enemyFlag[ind] == true ){
57+
gotoxy(enemyX[ind], enemyY[ind]); cout<<"****";
58+
gotoxy(enemyX[ind], enemyY[ind]+1); cout<<" ** ";
59+
gotoxy(enemyX[ind], enemyY[ind]+2); cout<<"****";
60+
gotoxy(enemyX[ind], enemyY[ind]+3); cout<<" ** ";
61+
}
62+
}
63+
void eraseEnemy(int ind){
64+
if( enemyFlag[ind] == true ){
65+
gotoxy(enemyX[ind], enemyY[ind]); cout<<" ";
66+
gotoxy(enemyX[ind], enemyY[ind]+1); cout<<" ";
67+
gotoxy(enemyX[ind], enemyY[ind]+2); cout<<" ";
68+
gotoxy(enemyX[ind], enemyY[ind]+3); cout<<" ";
69+
}
70+
}
71+
void resetEnemy(int ind){
72+
eraseEnemy(ind);
73+
enemyY[ind] = 1;
74+
genEnemy(ind);
75+
}
76+
77+
void drawCar(){
78+
for(int i=0; i<4; i++){
79+
for(int j=0; j<4; j++){
80+
gotoxy(j+carPos, i+22); cout<<car[i][j];
81+
}
82+
}
83+
}
84+
void eraseCar(){
85+
for(int i=0; i<4; i++){
86+
for(int j=0; j<4; j++){
87+
gotoxy(j+carPos, i+22); cout<<" ";
88+
}
89+
}
90+
}
91+
92+
int collision(){
93+
if( enemyY[0]+4 >= 23 ){
94+
if( enemyX[0] + 4 - carPos >= 0 && enemyX[0] + 4 - carPos < 9 ){
95+
return 1;
96+
}
97+
}
98+
return 0;
99+
}
100+
void gameover(){
101+
system("cls");
102+
cout<<endl;
103+
cout<<"\t\t--------------------------"<<endl;
104+
cout<<"\t\t-------- Game Over -------"<<endl;
105+
cout<<"\t\t--------------------------"<<endl<<endl;
106+
cout<<"\t\tPress any key to go back to menu.";
107+
getch();
108+
}
109+
void updateScore(){
110+
gotoxy(WIN_WIDTH + 7, 5);cout<<"Score: "<<score<<endl;
111+
}
112+
113+
void instructions(){
114+
115+
system("cls");
116+
cout<<"Instructions";
117+
cout<<"\n----------------";
118+
cout<<"\n Avoid Cars by moving left or right. ";
119+
cout<<"\n\n Press 'l' to move left";
120+
cout<<"\n Press 'r' to move right";
121+
cout<<"\n Press 'escape' to exit";
122+
cout<<"\n\nPress any key to go back to menu";
123+
getch();
124+
}
125+
126+
void play(){
127+
carPos = -1 + WIN_WIDTH/2;
128+
score = 0;
129+
enemyFlag[0] = 1;
130+
enemyFlag[1] = 0;
131+
enemyY[0] = enemyY[1] = 1;
132+
133+
system("cls");
134+
drawBorder();
135+
updateScore();
136+
genEnemy(0);
137+
genEnemy(1);
138+
139+
gotoxy(WIN_WIDTH + 7, 2);cout<<"Car Game";
140+
gotoxy(WIN_WIDTH + 6, 4);cout<<"----------";
141+
gotoxy(WIN_WIDTH + 6, 6);cout<<"----------";
142+
gotoxy(WIN_WIDTH + 7, 12);cout<<"Control ";
143+
gotoxy(WIN_WIDTH + 7, 13);cout<<"-------- ";
144+
gotoxy(WIN_WIDTH + 2, 14);cout<<" L Key - Left";
145+
gotoxy(WIN_WIDTH + 2, 15);cout<<" R Key - Right";
146+
147+
gotoxy(18, 5);cout<<"Press any key to start";
148+
getch();
149+
gotoxy(18, 5);cout<<" ";
150+
151+
while(1){
152+
if(kbhit()){
153+
char ch = getch();
154+
if( ch=='l' || ch=='L' ){
155+
if( carPos > 18 )
156+
carPos -= 4;
157+
}
158+
if( ch=='r' || ch=='R' ){
159+
if( carPos < 50 )
160+
carPos += 4;
161+
}
162+
if(ch==27){
163+
break;
164+
}
165+
}
166+
167+
drawCar();
168+
drawEnemy(0);
169+
drawEnemy(1);
170+
if( collision() == 1 ){
171+
gameover();
172+
return;
173+
}
174+
Sleep(50);
175+
eraseCar();
176+
eraseEnemy(0);
177+
eraseEnemy(1);
178+
179+
if( enemyY[0] == 10 )
180+
if( enemyFlag[1] == 0 )
181+
enemyFlag[1] = 1;
182+
183+
if( enemyFlag[0] == 1 )
184+
enemyY[0] += 1;
185+
186+
if( enemyFlag[1] == 1 )
187+
enemyY[1] += 1;
188+
189+
if( enemyY[0] > SCREEN_HEIGHT-4 ){
190+
resetEnemy(0);
191+
score++;
192+
updateScore();
193+
}
194+
if( enemyY[1] > SCREEN_HEIGHT-4 ){
195+
resetEnemy(1);
196+
score++;
197+
updateScore();
198+
}
199+
}
200+
}
201+
202+
int main()
203+
{
204+
setcursor(0,0);
205+
srand( (unsigned)time(NULL));
206+
207+
do{
208+
system("cls");
209+
gotoxy(10,5); cout<<" -------------------------- ";
210+
gotoxy(10,6); cout<<" | Car Game | ";
211+
gotoxy(10,7); cout<<" --------------------------";
212+
gotoxy(10,9); cout<<"1. Start Game";
213+
gotoxy(10,10); cout<<"2. Instructions";
214+
gotoxy(10,11); cout<<"3. Quit";
215+
gotoxy(10,13); cout<<"\n Select option: ";
216+
char op = getche();
217+
218+
if( op=='1') play();
219+
else if( op=='2') instructions();
220+
else if( op=='3') exit(0);
221+
222+
}while(1);
223+
224+
return 0;
225+
}

0 commit comments

Comments
 (0)