rod1231234

nat

Oct 31st, 2011
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.77 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <windows.h>
  5. #include <conio.h>
  6. struct place
  7. {
  8.     int x;
  9.     int y;
  10. };
  11. void gotoxy(int x, int y)
  12. {
  13.     COORD c;
  14.     c.X = x - 1;
  15.     c.Y = y - 1;
  16.     SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c);
  17. }
  18. int main(void)
  19. {
  20.     struct place head, body1, body2, body3, body4, tail;
  21.     head.x=40;
  22.     head.y=12;
  23.     body1.x=39;
  24.     body1.y=12;
  25.     body2.x=38;
  26.     body2.y=12;
  27.     body3.x=37;
  28.     body3.y=12;
  29.     body4.x=36;
  30.     body4.y=12;
  31.     tail.x=35;
  32.     tail.y=12;
  33.     int r,lag=50,dir=1;
  34.     srand(time(NULL));
  35.     printf("tempo de delay: ");
  36.     scanf("%d",&lag);
  37.     system("cls");
  38.     fflush(stdin);
  39.     while(kbhit()==0)
  40.     {
  41.         gotoxy(tail.x,tail.y);
  42.         printf(" ");
  43.         gotoxy(body4.x,body4.y);
  44.         printf("%c",176);
  45.         gotoxy(body3.x,body3.y);
  46.         printf("%c",177);
  47.         gotoxy(body2.x,body2.y);
  48.         printf("%c",177);
  49.         gotoxy(body1.x,body1.y);
  50.         printf("%c",178);
  51.         gotoxy(head.x,head.y);
  52.         printf("%c",178);
  53.  
  54.         tail=body4;
  55.         body4=body3;
  56.         body3=body2;
  57.         body2=body1;
  58.         body1=head;
  59.  
  60.         r=rand() %20;
  61.         switch (r)
  62.         {
  63.             case 0: dir++; break;
  64.             case 1: dir--; break;
  65.         }
  66.  
  67.         if(dir==-1) dir=3;
  68.         if(dir==4) dir=0;
  69.  
  70.         switch(dir)
  71.         {
  72.             case 0: head.y++; break; //baixo
  73.             case 1: head.x--; break; //esquerda
  74.             case 2: head.y--; break; // cima
  75.             case 3: head.x++; break; //direita
  76.         }
  77.         int q;
  78.         if(head.y==1)
  79.         {
  80.             if(dir==2)
  81.             q=rand() %2;
  82.             if(q==0)
  83.             dir++;
  84.             else
  85.             dir--;
  86.         }
  87.         else
  88.         {
  89.             q=rand() %5;
  90.             if(q==0)
  91.             dir=0;
  92.         }
  93.         if(head.y==25)
  94.         {
  95.             if(dir==0)
  96.             q=rand() %2;
  97.             if(q==0)
  98.             dir++;
  99.             else
  100.             dir--;
  101.         }
  102.         else
  103.         {
  104.             q=rand() %5;
  105.             if(q==0)
  106.             dir=2;
  107.         }
  108.         if(head.x==1)
  109.         {
  110.             if(dir==1)
  111.             q=rand() %2;
  112.             if(q==0)
  113.             dir++;
  114.             else
  115.             dir--;
  116.         }
  117.         else
  118.         {
  119.             q=rand() %5;
  120.             if(q==0)
  121.             dir=3;
  122.         }
  123.         if(head.x==80)
  124.         {
  125.             if(dir==3)
  126.             q=rand() %2;
  127.             if(q==0)
  128.             dir++;
  129.             else
  130.             dir--;
  131.         }
  132.         else
  133.         {
  134.             q=rand() %5;
  135.             if(q==0)
  136.             dir=1;
  137.         }
  138.  
  139.         _sleep(40);
  140.     }
  141.  
  142.         return 0;
  143. }
  144.  
  145.  
Advertisement
Add Comment
Please, Sign In to add comment