Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #include <windows.h>
- #include <time.h>
- #define CAMP "##############################################", \
- "#............................................#", \
- "#............................................#", \
- "#............................................#", \
- "#............................................#", \
- "#.......ooo..................................#", \
- "#............................................#", \
- "#............................................#", \
- "#............................................#", \
- "#............................................#", \
- "#............................................#", \
- "#............................................#", \
- "#............................................#", \
- "#............................................#", \
- "##############################################"
- #define TAMV 15
- #define TAMH 46
- #define MAX 1000000
- #define U 'w'
- #define D 's'
- #define R 'd'
- #define L 'a'
- #define SI 1
- #define NO 0
- #define LIB 0
- #define OBS 1
- #define BIN 2
- void imprimeCampo(char tab[][TAMH]);
- int moveCabeza(char tab[][TAMH], int *y, int *x, char dir);
- void borraCola (char tab[][TAMH], int *b, int *a, char dir[MAX], int *i);
- int comp (char dirt[MAX], int i, char dir);
- void fruta(char tab[][TAMH]);
- int comeFruta(char tab[][TAMH], int y, int x, char dir);
- int main()
- {
- char camp[TAMV][TAMH] = {CAMP};
- char direction[MAX];
- char dir=R;
- int cont = 0;
- int i=1;
- int x = 10;
- int y = 5;
- int a = 8;
- int b = 5;
- int control;
- int come=NO;
- imprimeCampo(camp);
- direction[0]=R;
- direction[1]=R;
- printf(" \nPress any key to start and '0' to stop.\n");
- getch();
- fruta(camp);
- do
- {
- Sleep(200);
- if(kbhit()==1)
- {
- dir=getch();
- if(comp(direction, i, dir)==BIN)
- dir=direction[i];
- }
- ++i;
- direction[i]=dir;
- come = comeFruta(camp, y, x, dir);
- control=moveCabeza(camp, &y, &x, dir);
- if(come==0)
- {
- borraCola(camp, &b, &a, direction, &cont);
- ++cont;
- }
- else
- fruta(camp);
- imprimeCampo(camp);
- printf("Score: %d\n", come);
- }
- while(dir!='0' && control!=OBS);
- printf("\nGame over.\n");
- return 0;
- }
- void imprimeCampo(char tab[][TAMH])
- {
- int i;
- int j;
- system("cls");
- for(j=0;j<TAMV;j++)
- {
- for(i=0;i<TAMH;i++)
- printf("%c", tab[j][i]);
- printf("\n");
- }
- }
- int moveCabeza(char tab[][TAMH], int *y, int *x, char dir)
- {
- int pos=OBS;
- if(tab[(*y)-1][(*x)]!='o' && dir==U && tab[(*y)-1][(*x)]!='#')
- {
- tab[--(*y)][(*x)]='o';
- pos=LIB;
- }
- if(tab[(*y)+1][(*x)]!='o' && dir==D && tab[(*y)+1][(*x)]!='#')
- {
- tab[++(*y)][(*x)]='o';
- pos=LIB;
- }
- if(tab[(*y)][(*x)+1]!='o' && dir==R && tab[(*y)][(*x)+1]!='#')
- {
- tab[(*y)][++(*x)]='o';
- pos=LIB;
- }
- if(tab[(*y)][(*x)-1]!='o' && dir==L && tab[(*y)][(*x)-1]!='#')
- {
- tab[(*y)][--(*x)]='o';
- pos=LIB;
- }
- return pos;
- }
- void borraCola(char tab[][TAMH], int *b, int *a, char dir[MAX], int *i)
- {
- tab[*b][*a] = '.';
- if(dir[*i]==U)
- --(*b);
- if(dir[*i]==D)
- ++(*b);
- if(dir[*i]==R)
- ++(*a);
- if(dir[*i]==L)
- --(*a);
- }
- int comp (char dirt[MAX], int i, char dir)
- {
- int control=LIB;
- if((dir==U && dirt[i]==D) || (dir==D && dirt[i]==U) || (dir==L && dirt[i]==R) || (dir==R && dirt[i]==L))
- control = BIN;
- return control;
- }
- void fruta(char tab[][TAMH])
- {
- int x;
- int y;
- do
- {
- srand(time(NULL));
- x = ( rand() % 44 )+1;
- y = ( rand() % 13 )+1;
- }
- while(tab[y][x]=='o' && tab[y][x]=='#');
- tab[y][x]='x';
- }
- int comeFruta(char tab[][TAMH], int y, int x, char dir)
- {
- int control=1;
- if(tab[--y][x]!='x' && dir==U)
- {
- control=0;
- }
- if(tab[++y][x]!='x' && dir==D);
- {
- control=0;
- }
- if(tab[y][++x]!='x' && dir==R)
- {
- control=0;
- }
- if(tab[y][--x]!='x' && dir==L)
- {
- control=0;
- }
- return control;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement