Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #include <time.h>
  5. using namespace std;
  6.  
  7. void wait(double seconds)
  8.  
  9. {
  10.  
  11.     clock_t endwait;
  12.  
  13.     endwait = clock() + seconds * CLOCKS_PER_SEC;
  14.  
  15.     while (clock() < endwait) {}
  16.  
  17. }
  18.  
  19. void game()
  20. {
  21.     int heady = 5, headx = 10, lengh = 3, inp = 'a', last = 'a', score = 0;
  22.     int mapa[21][21] = {};
  23.     int fruity = 2, fruitx = 2;
  24.     double delay = 0.2;
  25.  
  26.     while (true) {
  27.         system("cls");
  28.         if (heady == fruity && headx == fruitx) {
  29.             lengh++;
  30.             fruity = rand() % 10;
  31.             fruitx = rand() % 20;
  32.             score += 50;
  33.             delay /= 1.05;
  34.         }
  35.  
  36.         cout << "#######################" << endl;
  37.  
  38.         for (int i = 0; i <= 10; i++) {
  39.             cout << "#";
  40.             for (int j = 0; j <= 20; j++) {
  41.                 if (mapa[i][j] > 0) { mapa[i][j]--; }
  42.  
  43.                 if (i == heady && j == headx) {
  44.                     mapa[i][j] = lengh;
  45.                 }
  46.                 else if (i == fruity && j == fruitx) {
  47.                     mapa[i][j] = -1;
  48.                 }
  49.  
  50.                 if (mapa[i][j] > 0) {
  51.                     if (mapa[i][j] % 2 == 0) { cout << "0"; }
  52.                     if (mapa[i][j] % 2 == 1) { cout << "*"; }
  53.                 }
  54.                 else if (mapa[i][j] == -1) {
  55.                     cout << "o";
  56.                 }
  57.                 else {
  58.                     cout << " ";
  59.                 }
  60.             }
  61.             cout << "#";
  62.             cout << endl;
  63.         }
  64.         cout << "#######################" << endl;
  65.         cout << "score: " << score << endl;
  66.  
  67.         if (_kbhit() != 0) {
  68.             inp = _getch();
  69.             wait(delay / 2);
  70.         }
  71.         else { wait(delay); }
  72.  
  73.         if (inp == 's' && last != 'w') { last = 's'; }
  74.         else if (inp == 'w' && last != 's') { last = 'w'; }
  75.         else if (inp == 'a' && last != 'd') { last = 'a'; }
  76.         else if (inp == 'd' && last != 'a') { last = 'd'; }
  77.  
  78.         if (last == 's') {
  79.             if (heady == 10) {
  80.                 if (mapa[0][headx] > 1) { break; }
  81.                 heady = 0;
  82.             }
  83.             else {
  84.                 if (mapa[heady + 1][headx] > 1) { break; }
  85.                 heady++;
  86.             }
  87.         }
  88.         else if (last == 'w') {
  89.             if (heady == 0) {
  90.                 if (mapa[10][headx] > 1) { break; }
  91.                 heady = 10;
  92.             }
  93.             else {
  94.                 if (mapa[heady - 1][headx] > 1) { break; }
  95.                 heady--;
  96.             }
  97.         }
  98.         else if (last == 'a') {
  99.             if (headx == 0) {
  100.                 if (mapa[heady][20] > 1) { break; }
  101.                 headx = 20;
  102.             }
  103.             else {
  104.                 if (mapa[heady][headx - 1] > 1) { break; }
  105.                 headx--;
  106.             }
  107.         }
  108.         else if (last == 'd') {
  109.             if (headx == 20) {
  110.                 if (mapa[heady][0] > 1) { break; }
  111.                 headx = 0;
  112.             }
  113.             else {
  114.                 if (mapa[heady][headx + 1] > 1) { break; }
  115.                 headx++;
  116.             }
  117.         }
  118.         cout << '\a';
  119.     }
  120.     cout << "you lose";
  121.     char endGame = _getch();
  122. }
  123.  
  124. int main() {
  125.     char input;
  126.     int crs = 0;
  127.     while (true) {
  128.         if (crs == 0) { cout << "Start game! <<" << endl; }
  129.         else { cout << "Start game!" << endl; }
  130.         if (crs == 1) { cout << "Score <<" << endl; }
  131.         else { cout << "Score" << endl; }
  132.         if (crs == 2) { cout << "Exit <<" << endl; }
  133.         else { cout << "Exit" << endl; }
  134.         input = _getch();
  135.         if (input == 'w' && crs > 0) { crs--; }
  136.         if (input == 's' && crs < 2) { crs++; }
  137.         if (input == 'd') {
  138.             if (crs == 0) game();
  139.             if (crs == 2) break;
  140.         }
  141.         system("cls");
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement