Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #include <vector>
- #include <ctime>
- #include <conio.h>
- #include <Windows.h>
- using namespace std;
- //*************************DATA****************
- class coord
- {public:
- int x,y;
- };
- void game_over();
- void start();
- void game();
- int lung = 0;
- int difficulty;
- char space[20][70];
- coord element[100], gandac, dir;
- // ********************** FUNCTIONS**************
- void wait ( int ms )
- {
- clock_t endwait;
- endwait = clock () + ms * CLOCKS_PER_SEC /1000;
- while (clock() < endwait) {}
- }
- void set_screen()
- {
- int i,j;
- for(i=0;i<20;i++)
- for(j=0;j<70;j++)
- space[i][j] = 0;
- }
- void set_new_bug()
- {
- srand( (unsigned)time( NULL ) );
- do {
- gandac.x = rand() % 70;
- gandac.y = rand() % 20;
- } while (space[gandac.y][gandac.x] != 0);
- space[gandac.y][gandac.x] = '&';
- }
- void mananca()
- {
- 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 )
- game_over();
- if ( space[ element[lung].y + dir.y][ element[lung].x + dir.x] == 0 );
- else if ( space[element[lung].y + dir.y][element[lung].x + dir.x] == '&')
- {
- lung += 1;
- element[lung].x = gandac.x;
- element[lung].y = gandac.y;
- set_new_bug();
- }
- else game_over();
- }
- void misca()
- {
- set_screen();
- int i;
- for(i=0;i<lung;i++)
- {
- element[i].x = element[i+1].x;
- element[i].y = element[i+1].y;
- }
- element[lung].x += dir.x;
- element[lung].y += dir.y;
- for(i=0;i<=lung;i++)
- space[element[i].y][element[i].x] = '*';
- space[gandac.y][gandac.x] = '&';
- }
- void display()
- {
- system("cls");
- int i,j;
- cout << " SCORE : " << lung << endl;
- cout << "|";
- for(j=0;j<70;j++)
- cout << "-";
- cout << "|" << endl;
- for(i=0;i<20;i++)
- {
- cout << "|";
- for(j=0;j<70;j++)
- cout << space[i][j];
- cout << "|" << endl;
- }
- cout << "|";
- for(j=0;j<70;j++)
- cout << "-";
- cout << "|";
- }
- void game_over()
- {
- system("cls");
- cout <<" GAME OVER! " << endl << " Ati vrea sa reincercati?(Y/N)";
- char c;
- cin >> c;
- if (c == 'y' || c == 'Y')
- {
- start();
- system("cls");
- }
- else
- exit(1);
- }
- void start()
- {
- cout << "Va rugam stati o dificultate intre 1 si 10:" << endl;
- cin >> difficulty;
- if(difficulty > 10 || difficulty < 1)
- {
- MessageBox(NULL, "Dificultatea introdusa este incorecta", "Eroare", 0);
- system("cls");
- start();
- }
- else game();
- }
- void game()
- {
- char c;
- dir.x = 1;
- dir.y = 0;
- lung = 0;
- element[0].x = 35;
- element[0].y = 10;
- space[10][35] = '*';
- srand( (unsigned)time( NULL ) );
- gandac.x = rand() % 70;
- gandac.y = rand() % 20;
- space[gandac.y][gandac.x] = '&';
- while(lung < 100)
- {
- display();
- misca();
- mananca();
- wait (100 * (11 - difficulty));
- if(_kbhit())
- {
- c = getch();
- if( c == 80)
- {
- dir.x = 0;
- dir.y = 1;
- }
- else if( c == 75)
- {
- dir.x = -1;
- dir.y = 0;
- }
- else if( c == 77)
- {
- dir.x = 1;
- dir.y = 0;
- }
- else if( c == 72)
- {
- dir.x = 0;
- dir.y = -1;
- }
- else if ( c == 13 )
- exit(1);
- }
- }
- }
- //**************************MAIN***************************************
- int main()
- {
- cout << "Bine ati venit la jocul de rama!" << endl ;
- start();
- cout << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement