Advertisement
benaryorg

Windows Snake.c

Feb 5th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <conio.h>
  5. #include <windows.h>
  6.  
  7. #define SIZEX 50
  8. #define SIZEY 30
  9.  
  10. #define UP 0
  11. #define LEFT 1
  12. #define DOWN 2
  13. #define RIGHT 3
  14. #define STAND 4
  15.  
  16. typedef struct
  17. {
  18.     int x;
  19.     int y;
  20.     int direction;
  21.     char ch;
  22.     int exists;
  23. } KOERPER;
  24.  
  25. typedef struct
  26. {
  27.     int x;
  28.     int y;
  29. } ITEM;
  30.  
  31. int gueltig(int x,int y)
  32. {
  33.     return x>0 && x<SIZEX && y>0 && y<SIZEY;
  34. }
  35.  
  36. void disablecursor(void) ///Selbst geschrieben!
  37. {
  38.     CONSOLE_CURSOR_INFO cursor;
  39.     GetConsoleCursorInfo(GetCurrentThread(),&cursor);
  40.     cursor.bVisible=0;
  41.     SetConsoleCursorInfo(GetCurrentThread(),&cursor);
  42. }
  43.  
  44. void gotoxy(int x,int y) ///Selbst geschrieben!
  45. {
  46.     COORD xy;
  47.     xy.X=(short)x;
  48.     xy.Y=(short)y;
  49.     SetConsoleCursorPosition(GetCurrentThread(),xy);
  50. }
  51.  
  52. void putxy(int x,int y,char ch)
  53. {
  54.     gotoxy(x,y);
  55.     putchar(ch);
  56. }
  57.  
  58. int main(void)
  59. {
  60.     srand((unsigned)time(0));
  61.     disablecursor();
  62.  
  63.     ITEM item;
  64.     item.x=rand()%(SIZEX-1)+1;
  65.     item.y=rand()%(SIZEY-1)+1;
  66.  
  67.     KOERPER koerper[SIZEX*SIZEY]={};
  68.     koerper[0].x=SIZEX/2;
  69.     koerper[0].y=SIZEY/2;
  70.     koerper[0].exists=1;
  71.     koerper[0].ch=2;
  72.     koerper[0].direction=STAND;
  73.  
  74.     int x;
  75.     int tot=0;
  76.     int punkte=0;
  77.  
  78.     for(x=0;x<SIZEX;x++)
  79.     {
  80.         putxy(x,0,'X');
  81.         putxy(x,SIZEY,'X');
  82.         if(x<=SIZEY)
  83.         {
  84.             putxy(0,x,'X');
  85.             putxy(SIZEX,x,'X');
  86.         }
  87.     }
  88.     while(!tot)
  89.     {
  90.         if(kbhit())
  91.         {
  92.             switch(getch())
  93.             {
  94.                 case 'w':
  95.                     if(koerper[0].direction!=DOWN)
  96.                     {
  97.                         koerper[0].direction=UP;
  98.                     }
  99.                     break;
  100.                 case 'a':
  101.                     if(koerper[0].direction!=RIGHT)
  102.                     {
  103.                         koerper[0].direction=LEFT;
  104.                     }
  105.                     break;
  106.                 case 's':
  107.                     if(koerper[0].direction!=UP)
  108.                     {
  109.                         koerper[0].direction=DOWN;
  110.                     }
  111.                     break;
  112.                 case 'd':
  113.                     if(koerper[0].direction!=LEFT)
  114.                     {
  115.                         koerper[0].direction=RIGHT;
  116.                     }
  117.                     break;
  118.                 case ' ':
  119.                     if(getch()=='q')
  120.                     {
  121.                         tot=1;
  122.                     }
  123.                     break;
  124. #ifdef CHEATS
  125.                 case 'g':
  126.                     putxy(item.x,item.y,' ');
  127.                     switch(koerper[0].direction)
  128.                     {
  129.                         case UP:
  130.                             item.x=koerper[0].x;
  131.                             item.y=koerper[0].y-1;
  132.                             break;
  133.                         case LEFT:
  134.                             item.x=koerper[0].x-1;
  135.                             item.y=koerper[0].y;
  136.                             break;
  137.                         case DOWN:
  138.                             item.x=koerper[0].x;
  139.                             item.y=koerper[0].y+1;
  140.                             break;
  141.                         case RIGHT:
  142.                             item.x=koerper[0].x+1;
  143.                             item.y=koerper[0].y;
  144.                             break;
  145.                     }
  146.                     if(!gueltig(item.x,item.y))
  147.                     {
  148.                         item.x=SIZEX/2;
  149.                         item.y=SIZEY/2;
  150.                     }
  151.                     break;
  152. #endif
  153.             }
  154.         }
  155.  
  156.         for(x=0;x<SIZEX*SIZEY && koerper[x].exists;x++);
  157.         for(;x>=0;x--)
  158.         {
  159.             if(koerper[x].exists)
  160.             {
  161.                 if(!koerper[x+1].exists)
  162.                 {
  163.                     putxy(koerper[x].x,koerper[x].y,' ');
  164.                 }
  165.                 switch(koerper[x].direction)
  166.                 {
  167.                     case UP:
  168.                         if(gueltig(koerper[x].x,koerper[x].y-1))
  169.                         {
  170.                             koerper[x].y--;
  171.                         }
  172.                         else
  173.                         {
  174.                             tot=1;
  175.                         }
  176.                         break;
  177.                     case LEFT:
  178.                         if(gueltig(koerper[x].x-1,koerper[x].y))
  179.                         {
  180.                             koerper[x].x--;
  181.                         }
  182.                         else
  183.                         {
  184.                             tot=1;
  185.                         }
  186.                         break;
  187.                     case DOWN:
  188.                         if(gueltig(koerper[x].x,koerper[x].y+1))
  189.                         {
  190.                             koerper[x].y++;
  191.                         }
  192.                         else
  193.                         {
  194.                             tot=1;
  195.                         }
  196.                         break;
  197.                     case RIGHT:
  198.                         if(gueltig(koerper[x].x+1,koerper[x].y))
  199.                         {
  200.                             koerper[x].x++;
  201.                         }
  202.                         else
  203.                         {
  204.                             tot=1;
  205.                         }
  206.                         break;
  207.                 }
  208.                 if(x>0)
  209.                 {
  210.                     koerper[x].direction=koerper[x-1].direction;
  211.                 }
  212.                 putxy(koerper[x].x,koerper[x].y,koerper[x].ch);
  213.             }
  214.         }
  215.  
  216.         for(x=1;x<SIZEX*SIZEY && koerper[x].exists;x++)
  217.         {
  218.             if(koerper[x].x==koerper[0].x && koerper[x].y==koerper[0].y && koerper[x].direction!=STAND)
  219.             {
  220.                 tot=1;
  221.             }
  222.         }
  223.  
  224.         if(koerper[0].x==item.x && koerper[0].y==item.y)
  225.         {
  226.             for(x=1;koerper[x].exists && x<SIZEX*SIZEY;x++);
  227.             koerper[x].direction=STAND;
  228.             koerper[x].x=koerper[x-1].x;
  229.             koerper[x].y=koerper[x-1].y;
  230.             koerper[x].ch='O';
  231.             koerper[x].exists=1;
  232.             item.x=rand()%(SIZEX-1)+1;
  233.             item.y=rand()%(SIZEY-1)+1;
  234.             punkte++;
  235.         }
  236.         putxy(item.x,item.y,'+');
  237.  
  238.         gotoxy(0,32);
  239.         printf("Punkte: %d",punkte);
  240.         Sleep(100);
  241.     }
  242.     gotoxy(0,100);
  243.     printf("Sie haben %d Punkt%c!\n",punkte,punkte==1?0:'e');
  244.     return 0;
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement