Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.01 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. #include <ctype.h>
  5. #include <list>
  6.  
  7. struct TPoint;
  8. struct EDirection;
  9. class CSnakeShow;
  10. class CSnake;
  11.  
  12. struct TPoint
  13. {
  14.     unsigned x;
  15.     unsigned y;
  16.  
  17.     TPoint() : x(0), y(0) { }
  18.     TPoint(unsigned _x, unsigned _y) : x(_x), y(_y) { }
  19. };
  20.  
  21. struct EDirection
  22. {
  23.     enum Type
  24.     {
  25.         Left = 0,
  26.         Right = 1,
  27.         Top = 2,
  28.         Down = 3
  29.     };
  30. };
  31.  
  32. class CSnake
  33. {
  34.     friend class CSnakeShow;
  35. private:
  36.     EDirection::Type m_direction;
  37.  
  38.     std::list<TPoint> m_points; // od ogona do glowy
  39.  
  40. public:
  41.     CSnake()
  42.     {
  43.         m_direction = EDirection::Right;
  44.         m_points.push_back(TPoint(10, 10));
  45.         m_points.push_back(TPoint(11, 10));
  46.         m_points.push_back(TPoint(12, 10));
  47.     }
  48.  
  49.     ~CSnake()
  50.     {
  51.         m_points.clear();
  52.     }
  53.  
  54.     void handle(bool isChangedDirection, EDirection::Type newDirection)
  55.     {
  56.         m_points.pop_front();
  57.  
  58.         TPoint newHead = getHeadPos();
  59.  
  60.         if(isChangedDirection)
  61.             m_direction = newDirection;
  62.  
  63.         if(m_direction == EDirection::Right)
  64.             ++newHead.x;
  65.         else if(m_direction == EDirection::Left)
  66.             --newHead.x;
  67.         else if(m_direction == EDirection::Top)
  68.             --newHead.y;
  69.         else if(m_direction == EDirection::Down)
  70.             ++newHead.y;
  71.  
  72.         m_points.push_back(newHead);
  73.     }
  74.  
  75.     TPoint getHeadPos()
  76.     {
  77.         return m_points.back();
  78.     }
  79. };
  80.  
  81. const unsigned g_width = 40;
  82. const unsigned g_height = 20;
  83.  
  84. class CSnakeShow
  85. {
  86. private:
  87.     CSnake * m_snake;
  88.  
  89.     bool m_points[g_height][g_width];
  90. public:
  91.     CSnakeShow(CSnake * snake)
  92.     {
  93.         m_snake = snake;
  94.         clear();
  95.     }
  96.  
  97.     ~CSnakeShow()
  98.     {
  99.  
  100.     }
  101.  
  102.     void clear()
  103.     {
  104.         for(int i = 0; i < g_height; ++i)
  105.         {
  106.             for(int j = 0; j < g_width; ++j)
  107.             {
  108.                 m_points[i][j] = false;
  109.             }
  110.         }
  111.     }
  112.  
  113.     void calculate()
  114.     {
  115.         clear();
  116.  
  117.         for(std::list<TPoint>::iterator i = m_snake->m_points.begin(); i != m_snake->m_points.end(); ++i)
  118.         {
  119.             m_points[(*i).y][(*i).x] = true;
  120.         }
  121.     }
  122.  
  123.     void draw()
  124.     {
  125.         calculate();
  126.  
  127.         system("cls"); // czyszczenie konsoli
  128.  
  129.         for(int i = 0; i < g_height; ++i)
  130.         {
  131.             for(int j = 0; j < g_width; ++j)
  132.             {
  133.                 printf(m_points[i][j] ? "O" : " ");
  134.             }
  135.             printf("\n");
  136.         }
  137.     }
  138. };
  139.  
  140. int main(int, char**)
  141. {
  142.     CSnake snake;
  143.     CSnakeShow snakeDisplay(&snake);
  144.     EDirection::Type currentDirection = EDirection::Right;
  145.     bool isDirectionChanged = false;
  146.  
  147.     bool isWorking = true;
  148.  
  149.     int key = 0;
  150.     while(isWorking)
  151.     {
  152.         isDirectionChanged = false;
  153.         if(_kbhit())
  154.         {
  155.             key = toupper(_getch());
  156.  
  157.             if(key == 'Q')
  158.                 isWorking = false;
  159.  
  160.             else if(key == 'D')
  161.             {
  162.                 currentDirection = EDirection::Right;
  163.                 isDirectionChanged = true;
  164.             }
  165.             else if(key == 'A')
  166.             {
  167.                 currentDirection = EDirection::Left;
  168.                 isDirectionChanged = true;
  169.             }
  170.             else if(key == 'W')
  171.             {
  172.                 currentDirection = EDirection::Top;
  173.                 isDirectionChanged = true;
  174.             }
  175.             else if(key == 'S')
  176.             {
  177.                 currentDirection = EDirection::Down;
  178.                 isDirectionChanged = true;
  179.             }
  180.         }
  181.         snake.handle(isDirectionChanged, currentDirection);
  182.  
  183.         snakeDisplay.draw();
  184.  
  185.         _sleep(50);
  186.     }
  187.  
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement