Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <conio.h>
- #include <ctype.h>
- #include <list>
- struct TPoint;
- struct EDirection;
- class CSnakeShow;
- class CSnake;
- struct TPoint
- {
- unsigned x;
- unsigned y;
- TPoint() : x(0), y(0) { }
- TPoint(unsigned _x, unsigned _y) : x(_x), y(_y) { }
- };
- struct EDirection
- {
- enum Type
- {
- Left = 0,
- Right = 1,
- Top = 2,
- Down = 3
- };
- };
- class CSnake
- {
- friend class CSnakeShow;
- private:
- EDirection::Type m_direction;
- std::list<TPoint> m_points; // od ogona do glowy
- public:
- CSnake()
- {
- m_direction = EDirection::Right;
- m_points.push_back(TPoint(10, 10));
- m_points.push_back(TPoint(11, 10));
- m_points.push_back(TPoint(12, 10));
- }
- ~CSnake()
- {
- m_points.clear();
- }
- void handle(bool isChangedDirection, EDirection::Type newDirection)
- {
- m_points.pop_front();
- TPoint newHead = getHeadPos();
- if(isChangedDirection)
- m_direction = newDirection;
- if(m_direction == EDirection::Right)
- ++newHead.x;
- else if(m_direction == EDirection::Left)
- --newHead.x;
- else if(m_direction == EDirection::Top)
- --newHead.y;
- else if(m_direction == EDirection::Down)
- ++newHead.y;
- m_points.push_back(newHead);
- }
- TPoint getHeadPos()
- {
- return m_points.back();
- }
- };
- const unsigned g_width = 40;
- const unsigned g_height = 20;
- class CSnakeShow
- {
- private:
- CSnake * m_snake;
- bool m_points[g_height][g_width];
- public:
- CSnakeShow(CSnake * snake)
- {
- m_snake = snake;
- clear();
- }
- ~CSnakeShow()
- {
- }
- void clear()
- {
- for(int i = 0; i < g_height; ++i)
- {
- for(int j = 0; j < g_width; ++j)
- {
- m_points[i][j] = false;
- }
- }
- }
- void calculate()
- {
- clear();
- for(std::list<TPoint>::iterator i = m_snake->m_points.begin(); i != m_snake->m_points.end(); ++i)
- {
- m_points[(*i).y][(*i).x] = true;
- }
- }
- void draw()
- {
- calculate();
- system("cls"); // czyszczenie konsoli
- for(int i = 0; i < g_height; ++i)
- {
- for(int j = 0; j < g_width; ++j)
- {
- printf(m_points[i][j] ? "O" : " ");
- }
- printf("\n");
- }
- }
- };
- int main(int, char**)
- {
- CSnake snake;
- CSnakeShow snakeDisplay(&snake);
- EDirection::Type currentDirection = EDirection::Right;
- bool isDirectionChanged = false;
- bool isWorking = true;
- int key = 0;
- while(isWorking)
- {
- isDirectionChanged = false;
- if(_kbhit())
- {
- key = toupper(_getch());
- if(key == 'Q')
- isWorking = false;
- else if(key == 'D')
- {
- currentDirection = EDirection::Right;
- isDirectionChanged = true;
- }
- else if(key == 'A')
- {
- currentDirection = EDirection::Left;
- isDirectionChanged = true;
- }
- else if(key == 'W')
- {
- currentDirection = EDirection::Top;
- isDirectionChanged = true;
- }
- else if(key == 'S')
- {
- currentDirection = EDirection::Down;
- isDirectionChanged = true;
- }
- }
- snake.handle(isDirectionChanged, currentDirection);
- snakeDisplay.draw();
- _sleep(50);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement