Advertisement
Guest User

Untitled

a guest
Oct 29th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <vector>
  5. #include <ctime>
  6. #include <conio.h>
  7. #include <Windows.h>
  8. using namespace std;
  9.  
  10.  
  11. //*************************DATA****************
  12. class coord
  13. {public:
  14.       int x,y;
  15. };
  16.  
  17. void game_over();
  18. void start();
  19. void game();
  20. int lung = 0;
  21. int difficulty;
  22. char space[20][70];
  23. coord element[100], gandac, dir;
  24.  
  25.  
  26. // ********************** FUNCTIONS**************
  27.  
  28.  
  29. void wait ( int ms )
  30. {
  31.   clock_t endwait;
  32.   endwait = clock () + ms * CLOCKS_PER_SEC /1000;
  33.   while (clock() < endwait) {}
  34. }
  35.  
  36. void set_screen()
  37. {
  38.      int i,j;
  39.      for(i=0;i<20;i++)
  40.         for(j=0;j<70;j++)
  41.             space[i][j] = 0;
  42. }
  43. void set_new_bug()
  44. {
  45.      srand( (unsigned)time( NULL ) );
  46.      do {
  47.      gandac.x = rand() % 70;
  48.      gandac.y = rand() % 20;
  49.      } while (space[gandac.y][gandac.x] != 0);
  50.      space[gandac.y][gandac.x] = '&';
  51. }
  52. void mananca()
  53. {      
  54.     if((element[lung].x + dir.x)>=70 || (element[lung].x + dir.x) < 0 || (element[lung].y + dir.y) >=20 || (element[lung].y + dir.y) < 0 )                                      
  55.             game_over();
  56.     if ( space[ element[lung].y + dir.y][ element[lung].x + dir.x] == 0 );  
  57.      else if ( space[element[lung].y + dir.y][element[lung].x + dir.x] == '&')
  58.      {  
  59.           lung += 1;
  60.           element[lung].x = gandac.x;
  61.           element[lung].y = gandac.y;
  62.           set_new_bug();
  63.      }
  64.      else game_over();
  65. }
  66.  
  67. void misca()
  68. {
  69.      set_screen();
  70.      int i;
  71.      for(i=0;i<lung;i++)
  72.      {
  73.           element[i].x = element[i+1].x;
  74.           element[i].y = element[i+1].y;
  75.  
  76.           }
  77.      element[lung].x += dir.x;
  78.      element[lung].y += dir.y;
  79.      for(i=0;i<=lung;i++)
  80.           space[element[i].y][element[i].x] = '*';
  81.      space[gandac.y][gandac.x] = '&';
  82. }
  83.  
  84.  
  85.  
  86. void display()
  87. {
  88.     system("cls");
  89.     int i,j;
  90.     cout << "           SCORE : " << lung << endl; 
  91.     cout << "|";
  92.     for(j=0;j<70;j++)
  93.         cout << "-";
  94.     cout << "|" << endl;
  95.  
  96.     for(i=0;i<20;i++)
  97.     {
  98.         cout << "|";
  99.         for(j=0;j<70;j++)
  100.             cout << space[i][j];
  101.         cout << "|" << endl;
  102.     }
  103.  
  104.     cout << "|";
  105.     for(j=0;j<70;j++)
  106.     cout << "-";
  107.     cout << "|";
  108. }
  109.  
  110. void game_over()
  111. {
  112.      system("cls");
  113.      cout <<"         GAME OVER!       " << endl << " Ati vrea sa reincercati?(Y/N)";
  114.      char c;
  115.      cin >> c;
  116.      if (c == 'y' || c == 'Y')
  117.      {
  118.             start();
  119.             system("cls");
  120.      }            
  121.      else
  122.      exit(1);
  123.      
  124. }
  125.  
  126.  
  127. void start()
  128. {
  129.      cout << "Va rugam stati o dificultate intre 1 si 10:" << endl;
  130.     cin >> difficulty;
  131.     if(difficulty > 10 || difficulty < 1)
  132.            {
  133.             MessageBox(NULL, "Dificultatea introdusa este incorecta", "Eroare", 0);
  134.             system("cls");
  135.             start();
  136.            }
  137.     else game();
  138.    
  139. }
  140.  
  141. void game()
  142. {
  143.      char c;
  144.     dir.x = 1;
  145.     dir.y = 0;
  146.     lung = 0;
  147.     element[0].x = 35;
  148.     element[0].y = 10;
  149.     space[10][35] = '*';
  150.      srand( (unsigned)time( NULL ) );
  151.      gandac.x = rand() % 70;
  152.      gandac.y = rand() % 20;
  153.      space[gandac.y][gandac.x] = '&';
  154.      while(lung < 100)
  155.      {
  156.            display();  
  157.            misca();
  158.            mananca();
  159.            wait (100 * (11 - difficulty));
  160.            if(_kbhit())
  161.            {
  162.                c = getch();
  163.                if( c == 80)
  164.                {
  165.                    dir.x = 0;
  166.                    dir.y = 1;
  167.                }
  168.                else if( c == 75)
  169.                {
  170.                    dir.x = -1;
  171.                    dir.y = 0;
  172.                }
  173.                else if( c == 77)
  174.                {
  175.                    dir.x = 1;
  176.                    dir.y = 0;
  177.                }
  178.                else if( c == 72)
  179.                {
  180.                    dir.x = 0;
  181.                    dir.y = -1;
  182.                }
  183.                else if ( c == 13 )
  184.                     exit(1);
  185.            }
  186.      }
  187.      
  188. }
  189.  
  190. //**************************MAIN***************************************
  191. int main()
  192. {
  193.    
  194.     cout << "Bine ati venit la jocul de rama!" << endl ;
  195.     start();
  196.  
  197.  
  198.     cout << endl;  
  199.     system("pause");
  200.     return 0;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement