Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <conio.h>
- #include <windows.h>
- #define SIZEX 50
- #define SIZEY 30
- #define UP 0
- #define LEFT 1
- #define DOWN 2
- #define RIGHT 3
- #define STAND 4
- typedef struct
- {
- int x;
- int y;
- int direction;
- char ch;
- int exists;
- } KOERPER;
- typedef struct
- {
- int x;
- int y;
- } ITEM;
- int gueltig(int x,int y)
- {
- return x>0 && x<SIZEX && y>0 && y<SIZEY;
- }
- void disablecursor(void) ///Selbst geschrieben!
- {
- CONSOLE_CURSOR_INFO cursor;
- GetConsoleCursorInfo(GetCurrentThread(),&cursor);
- cursor.bVisible=0;
- SetConsoleCursorInfo(GetCurrentThread(),&cursor);
- }
- void gotoxy(int x,int y) ///Selbst geschrieben!
- {
- COORD xy;
- xy.X=(short)x;
- xy.Y=(short)y;
- SetConsoleCursorPosition(GetCurrentThread(),xy);
- }
- void putxy(int x,int y,char ch)
- {
- gotoxy(x,y);
- putchar(ch);
- }
- int main(void)
- {
- srand((unsigned)time(0));
- disablecursor();
- ITEM item;
- item.x=rand()%(SIZEX-1)+1;
- item.y=rand()%(SIZEY-1)+1;
- KOERPER koerper[SIZEX*SIZEY]={};
- koerper[0].x=SIZEX/2;
- koerper[0].y=SIZEY/2;
- koerper[0].exists=1;
- koerper[0].ch=2;
- koerper[0].direction=STAND;
- int x;
- int tot=0;
- int punkte=0;
- for(x=0;x<SIZEX;x++)
- {
- putxy(x,0,'X');
- putxy(x,SIZEY,'X');
- if(x<=SIZEY)
- {
- putxy(0,x,'X');
- putxy(SIZEX,x,'X');
- }
- }
- while(!tot)
- {
- if(kbhit())
- {
- switch(getch())
- {
- case 'w':
- if(koerper[0].direction!=DOWN)
- {
- koerper[0].direction=UP;
- }
- break;
- case 'a':
- if(koerper[0].direction!=RIGHT)
- {
- koerper[0].direction=LEFT;
- }
- break;
- case 's':
- if(koerper[0].direction!=UP)
- {
- koerper[0].direction=DOWN;
- }
- break;
- case 'd':
- if(koerper[0].direction!=LEFT)
- {
- koerper[0].direction=RIGHT;
- }
- break;
- case ' ':
- if(getch()=='q')
- {
- tot=1;
- }
- break;
- #ifdef CHEATS
- case 'g':
- putxy(item.x,item.y,' ');
- switch(koerper[0].direction)
- {
- case UP:
- item.x=koerper[0].x;
- item.y=koerper[0].y-1;
- break;
- case LEFT:
- item.x=koerper[0].x-1;
- item.y=koerper[0].y;
- break;
- case DOWN:
- item.x=koerper[0].x;
- item.y=koerper[0].y+1;
- break;
- case RIGHT:
- item.x=koerper[0].x+1;
- item.y=koerper[0].y;
- break;
- }
- if(!gueltig(item.x,item.y))
- {
- item.x=SIZEX/2;
- item.y=SIZEY/2;
- }
- break;
- #endif
- }
- }
- for(x=0;x<SIZEX*SIZEY && koerper[x].exists;x++);
- for(;x>=0;x--)
- {
- if(koerper[x].exists)
- {
- if(!koerper[x+1].exists)
- {
- putxy(koerper[x].x,koerper[x].y,' ');
- }
- switch(koerper[x].direction)
- {
- case UP:
- if(gueltig(koerper[x].x,koerper[x].y-1))
- {
- koerper[x].y--;
- }
- else
- {
- tot=1;
- }
- break;
- case LEFT:
- if(gueltig(koerper[x].x-1,koerper[x].y))
- {
- koerper[x].x--;
- }
- else
- {
- tot=1;
- }
- break;
- case DOWN:
- if(gueltig(koerper[x].x,koerper[x].y+1))
- {
- koerper[x].y++;
- }
- else
- {
- tot=1;
- }
- break;
- case RIGHT:
- if(gueltig(koerper[x].x+1,koerper[x].y))
- {
- koerper[x].x++;
- }
- else
- {
- tot=1;
- }
- break;
- }
- if(x>0)
- {
- koerper[x].direction=koerper[x-1].direction;
- }
- putxy(koerper[x].x,koerper[x].y,koerper[x].ch);
- }
- }
- for(x=1;x<SIZEX*SIZEY && koerper[x].exists;x++)
- {
- if(koerper[x].x==koerper[0].x && koerper[x].y==koerper[0].y && koerper[x].direction!=STAND)
- {
- tot=1;
- }
- }
- if(koerper[0].x==item.x && koerper[0].y==item.y)
- {
- for(x=1;koerper[x].exists && x<SIZEX*SIZEY;x++);
- koerper[x].direction=STAND;
- koerper[x].x=koerper[x-1].x;
- koerper[x].y=koerper[x-1].y;
- koerper[x].ch='O';
- koerper[x].exists=1;
- item.x=rand()%(SIZEX-1)+1;
- item.y=rand()%(SIZEY-1)+1;
- punkte++;
- }
- putxy(item.x,item.y,'+');
- gotoxy(0,32);
- printf("Punkte: %d",punkte);
- Sleep(100);
- }
- gotoxy(0,100);
- printf("Sie haben %d Punkt%c!\n",punkte,punkte==1?0:'e');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement