Facebook
From me, 3 Years ago, written in C++.
This paste is a reply to snake from me - view diff
Embed
Download Paste or View Raw
Hits: 209
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <time.h>
  4.  
  5. bool gameOver;
  6. const int width = 20;
  7. const int height = 20;
  8. void startSetup(int* score, int* cordX, int* cordY, int* fruitX, int* fruitY)
  9. {
  10.     score = 0;
  11.     gameOver = false;
  12.     *cordX = width / 2 - 1;
  13.     *cordY = height / 2 - 1;
  14.     *fruitX = rand() % width;
  15.     *fruitY = rand() % height;
  16.  
  17. }
  18. void drawAll(int* cordX, int* cordY, int* fruitX, int* fruitY)
  19. {
  20.     system("cls");
  21.     for (int i = 0; i < width + 1; i++)
  22.     {
  23.         std::cout << "#";
  24.     }
  25.     std::cout << '\n';
  26.  
  27.     for (int i = 0; i < height; i++)
  28.     {
  29.         for (int j = 0; j < width; j++)
  30.         {
  31.             if (j == 0 or j == width - 1)
  32.             {
  33.                 std::cout << "#" ;
  34.             }
  35.             if (*cordY == i  and *cordX == j )
  36.             {
  37.                 std::cout << "0";
  38.             }
  39.             else if (*fruitX == j and *fruitY == i)
  40.             {
  41.                 std::cout << "F";
  42.             }
  43.             else
  44.             {
  45.                 std::cout << ' ';
  46.             }
  47.         }
  48.         std::cout << '\n';
  49.     }
  50.  
  51.     for (int i = 0; i < width + 1; i++)
  52.     {
  53.         std::cout << "#";
  54.     }
  55.     std::cout << '\n';
  56. }
  57.  
  58. void control(int* x, int* y)
  59. {
  60.     if (_kbhit())
  61.     {
  62.         auto key{ _getch() };
  63.         if (key == 'w' or key == 'W')
  64.         {
  65.             y--;
  66.         }
  67.         if (key == 's' or key == 'S')
  68.         {
  69.             y++;
  70.         }
  71.         if (key == 'a' or key == 'A')
  72.         {
  73.             x--;
  74.         }
  75.         if (key == 'd' or key == 'D')
  76.         {
  77.             x++;
  78.         }
  79.     }
  80. }
  81.  
  82.  
  83. int main()
  84. {
  85.     int x;
  86.     int y;
  87.     int fruitX;
  88.     int fruitY;
  89.     int* ptx = &x;
  90.     int* pty = &y;
  91.     int* ptfruitX = &fruitX;
  92.     int* ptfruitY = &fruitY;
  93.     int score;
  94.     int* pscore = &score;
  95.     startSetup(pscore, ptx, pty, ptfruitX, ptfruitY);
  96.     while (gameOver != 1)
  97.     {
  98.         drawAll(ptx, pty, ptfruitX, ptfruitY);
  99.         control (ptx, pty);
  100.        
  101.     }
  102. }

Replies to Re: snake rss

Title Name Language When
Re: Re: snake me cpp 3 Years ago.