rod1231234

nat

Oct 31st, 2011
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.12 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 q,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.         if(head.x!=80 && head.x!=1 && head.y!=1 && head.y!=25)
  60.         {
  61.         r=rand() %20;
  62.         switch (r)
  63.         {
  64.             case 0: dir++; break;
  65.             case 1: dir--; break;
  66.         }
  67.  
  68.         if(dir==-1) dir=3;
  69.         if(dir==4) dir=0;
  70.  
  71.         switch(dir)
  72.         {
  73.             case 0: head.y++; break; //baixo
  74.             case 1: head.x--; break; //esquerda
  75.             case 2: head.y--; break; //cima
  76.             case 3: head.x++; break; //direita
  77.         }
  78.         }
  79.         else
  80.         {
  81.             if(head.x==1)
  82.             {
  83.                 if(dir==1)
  84.                 {
  85.                 q=rand() %2;
  86.                 switch (q)
  87.                 {
  88.                     case 0:dir++;break;
  89.                     case 1:dir--;break;
  90.                 }
  91.                 }
  92.                 else
  93.                 {
  94.                     q=rand() %5;
  95.                     switch (q)
  96.                     {
  97.                         case 0:dir=3;break;
  98.                     }
  99.                 }
  100.             }
  101.             if(head.x==80)
  102.             {
  103.                 if(dir==3)
  104.                 {
  105.                     q=rand() %2;
  106.                     switch (q)
  107.                     {
  108.                         case 0:dir++;break;
  109.                         case 1:dir--;break;
  110.                     }
  111.                 }
  112.                 else
  113.                 {
  114.                     q=rand() %5;
  115.                     switch (q)
  116.                     {
  117.                         case 0:dir=1;break;
  118.                     }
  119.                 }
  120.             }
  121.             if(head.y==1)
  122.             {
  123.                 if(dir==2)
  124.                 {
  125.                     q=rand() %2;
  126.                     switch (q)
  127.                     {
  128.                         case 0:dir++;break;
  129.                         case 1:dir--;break;
  130.                     }
  131.                 }
  132.                 else
  133.                 {
  134.                     q=rand() %5;
  135.                     switch (q)
  136.                     {
  137.                         case 0:dir=2;break;
  138.                     }
  139.                 }
  140.             }
  141.             if(head.y==25)
  142.             {
  143.                 if(dir==0)
  144.                 {
  145.                     q=rand() %2;
  146.                     switch (q)
  147.                     {
  148.                         case 0:dir++;break;
  149.                         case 1:dir--;break;
  150.                     }
  151.                 }
  152.                 else
  153.                 {
  154.                     q=rand() %5;
  155.                     switch (q)
  156.                     {
  157.                         case 0:dir=2;break;
  158.                     }
  159.                 }
  160.             }
  161.         if(dir==-1) dir=3;
  162.         if(dir==4) dir=0;
  163.  
  164.         switch(dir)
  165.         {
  166.             case 0: head.y++; break;
  167.             case 1: head.x--; break;
  168.             case 2: head.y--; break;
  169.             case 3: head.x++; break;
  170.         }
  171.         }
  172.         _sleep(40);
  173.     }
  174.  
  175.         return 0;
  176. }
  177.  
  178.  
Advertisement
Add Comment
Please, Sign In to add comment