Advertisement
TAHMID37

dodge_car

Dec 24th, 2020
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.70 KB | None | 0 0
  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\tYOUR_SCORE:"<<score<<endl;
  104.     cout<<"\t\t__________________________"<<endl;
  105.     cout<<"\t\t__________________________"<<endl;
  106.     cout<<"\t\t_________Game Over________"<<endl;
  107.     cout<<"\t\t__________________________"<<endl;
  108.     cout<<"\t\t_________Play again_______"<<endl;
  109.     cout<<"\t\t__________________________"<<endl<<endl;
  110.     cout<<"\t\tPress any key to go back to menu.";
  111.     getch();
  112. }
  113. void updateScore(){
  114.     gotoxy(WIN_WIDTH + 7, 5);cout<<"Score: "<<score<<endl;
  115. }
  116.  
  117. void instructions(){
  118.  
  119.     system("cls");
  120.     cout<<"Instructions";
  121.     cout<<"\n*************";
  122.     cout<<"\n Avoid Cars by moving left or right. ";
  123.     cout<<"\n\n Press 'a' to move left";
  124.     cout<<"\n Press 'd' to move right";
  125.     cout<<"\n Press 'esc' to exit";
  126.     cout<<"\n\nPress any key to go back to menu";
  127.     getch();
  128. }
  129. void DOWNLOADING()
  130. {
  131.     int t = 0;
  132.     char c1 = '%';
  133.     int I = 100000000; while(I--){}
  134.     system("cls");
  135.     for(int i = 0; i < 25; i++) {
  136.         char c = 178;
  137.         t = (i+1) * 4;
  138.         if(i != 24) printf("\n\n\t\t\t\t\n\n\t\t\t\tLoading...           %3d%c", t, c1);
  139.         else        printf("\n\n\t\t\t\t\n\n\t\t\t\tLoaded               %3d%c", t, c1);
  140.         printf("\n\t\t\t\t");
  141.         for(long long j = 0; j <= i; j++) printf("%c", c);
  142.         I = 50000000; while(I--){}
  143.         if(i != 24) system("cls");
  144.     }
  145.     I = 500000000; while(I--){}
  146.     system("cls");
  147. }
  148.  
  149. int lol=0;
  150.  
  151. void play(){
  152.  
  153.  
  154.     int i;
  155.     for(i=0;i<1000000000;i++){}
  156.     DOWNLOADING();
  157.  
  158.     carPos = -1 + WIN_WIDTH/2;
  159.     score = 0;
  160.     enemyFlag[0] = 1;
  161.     enemyFlag[1] = 0;
  162.     enemyY[0] = enemyY[1] = 1;
  163.  
  164.     system("cls");
  165.     drawBorder();
  166.     updateScore();
  167.     genEnemy(0);
  168.     genEnemy(1);
  169.  
  170.     gotoxy(WIN_WIDTH + 7, 2);cout<<"DODGE_CAR";
  171.     gotoxy(WIN_WIDTH + 6, 4);cout<<"----------";
  172.     gotoxy(WIN_WIDTH + 6, 6);cout<<"----------";
  173.     gotoxy(WIN_WIDTH + 3, 12);cout<<"Control_Buttons";
  174.     gotoxy(WIN_WIDTH + 7, 13);cout<<"------ ";
  175.     gotoxy(WIN_WIDTH + 2, 14);cout<<" A Key  Left(<--)";
  176.     gotoxy(WIN_WIDTH + 2, 15);cout<<" D Key  Right(-->)";
  177.  
  178.     gotoxy(18, 5);cout<<"Press any key to start";
  179.     getch();
  180.     gotoxy(18, 5);cout<<"                      ";
  181.  
  182.  
  183.     while(1){
  184.         if(kbhit()){
  185.             char ch = getch();
  186.             if( ch=='a' || ch=='A' ){
  187.                 if( carPos > 18 )
  188.                     carPos -= 4;
  189.             }
  190.             if( ch=='d' || ch=='D' ){
  191.                 if( carPos < 50 )
  192.                     carPos += 4;
  193.             }
  194.             if(ch==27){
  195.                 break;
  196.             }
  197.         }
  198.  
  199.         drawCar();
  200.         drawEnemy(0);
  201.         drawEnemy(1);
  202.         if( collision() == 1  ){
  203.             gameover();
  204.             return;
  205.         }
  206.  
  207.         if(score%10==0)
  208.             lol+=5;
  209.         if(lol==45)
  210.             lol=0;
  211.         Sleep(60-lol);
  212.         eraseCar();
  213.         eraseEnemy(0);
  214.         eraseEnemy(1);
  215.  
  216.         if( enemyY[0] == 10 )
  217.             if( enemyFlag[1] == 0 )
  218.                 enemyFlag[1] = 1;
  219.  
  220.         if( enemyFlag[0] == 1 )
  221.             enemyY[0] += 1;
  222.  
  223.         if( enemyFlag[1] == 1 )
  224.             enemyY[1] += 1;
  225.  
  226.         if( enemyY[0] > SCREEN_HEIGHT-4 ){
  227.             resetEnemy(0);
  228.             score++;
  229.             updateScore();
  230.         }
  231.         if( enemyY[1] > SCREEN_HEIGHT-4 ){
  232.             resetEnemy(1);
  233.             score++;
  234.             updateScore();
  235.         }
  236.     }
  237. }
  238.  
  239.  
  240.  
  241. int main()
  242. {
  243.     setcursor(0,0);
  244.     srand( (unsigned)time(NULL));
  245.  
  246.     do{
  247.         system("cls");
  248.         gotoxy(30,5); cout<<"                            ";
  249.         gotoxy(30,6); cout<<"         DODGE__CAR         ";
  250.         gotoxy(30,7); cout<<"*****************************";
  251.         gotoxy(30,9); cout<<"1. Start  ";
  252.         gotoxy(30,10); cout<<"2. Instructions";
  253.         gotoxy(30,11); cout<<"3. Quit";
  254.         gotoxy(30,13); cout<<"Press button: ";
  255.         char op = getche();
  256.  
  257.         if( op=='1') play();
  258.         else if( op=='2') instructions();
  259.         else if( op=='3') exit(0);
  260.  
  261.     }while(1);
  262.  
  263.     return 0;
  264. }
  265.  
  266.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement