Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.56 KB | None | 0 0
  1. struct Pos { int x, int y; };
  2.  
  3. class Snake {
  4.     list<Pos> body;
  5.     Direction dir;
  6.     void move();
  7.    
  8. public:
  9.     void mainLoop();
  10. };
  11.  
  12. void Snake::mainLoop(){
  13.     while (true){
  14.         sleep(10);
  15.         move();
  16.     }
  17. }
  18.  
  19. void Snake::move(){
  20.     if (dir == stop){ return; }
  21.    
  22.     body.pop_back();
  23.    
  24.     Pos oldHead = *(body.begin()); // get the first element of the list
  25.     Pos newHead = oldHead;
  26.    
  27.     switch (dir){
  28.         case left: newHead.x--; break;
  29.         case right: newHead.x++; break;
  30.         case up: newHead.y--; break;
  31.         case down: newHead.y++; break;
  32.     };
  33.    
  34.     body.push_front(newHead});
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement