Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Juego ultra simple del clasico de la viborita (Snake)
- * Controles: w arriba, a izquierda, s abajo, d derecha, p pausa
- * ----------------------------------------------------------------------------
- * "NO LICENSE" (no version)
- * afperrens@gmail.com wrote this file, feel free to do whatever you
- * want with this code - sientase libre de hacer lo que quiera con el codigo.
- * Para sistemas unix-like: gcc -Wall -lncurses -o viborita viborita.c
- * Requisitos: ncurses (en algunos tambien requiriran ncurses-devel)
- * ----------------------------------------------------------------------------
- */
- #include <stdio.h>
- #include <sys/ioctl.h>
- #include <ncurses.h>
- #include <stdlib.h>
- #include <time.h>
- #include <unistd.h>
- #define BORDE 3
- struct cViborita{
- int x, y;
- struct cViborita *sig; };
- int drawCuadradoFULL(){
- struct winsize w;
- ioctl(0, TIOCGWINSZ, &w);
- int x = BORDE, y = BORDE;
- for( ; x < w.ws_col-BORDE; x++) mvaddch(y,x,ACS_CKBOARD);
- for( ; y < w.ws_row-BORDE; y++) mvaddch(y,x,ACS_CKBOARD);;
- for( ; x > BORDE; x--) mvaddch(y,x,ACS_CKBOARD);
- for( ; y > BORDE; y--) mvaddch(y,x,ACS_CKBOARD);
- return 0; }
- void agregarCuerpo(struct cViborita *v){
- struct cViborita * nuevo = (struct cViborita *) malloc(sizeof(struct cViborita) * 1);
- while(v->sig != NULL) v = v->sig;
- nuevo->x = v->x;
- nuevo->y = v->y;
- v->sig = nuevo;
- nuevo->sig = NULL; }
- struct cViborita * moverViborita(struct cViborita *v, int direccion){
- struct cViborita *ant, *aux;
- ant = v;
- aux = v->sig;
- while(aux->sig != NULL){
- ant = aux;
- aux = aux->sig; }
- ant->sig = NULL; // pongo este elemento como la cola
- aux->sig = v; // aux ahora apunta a la cabeza
- aux->x = v->x;
- aux->y = v->y;
- switch(direccion){
- case 1: aux->y--;
- break;
- case 2: aux->x++;
- break;
- case 3: aux->y++;
- break;
- case 4: aux->x--;
- break; }
- ant = aux->sig; // reutilizo ant
- while(ant != NULL){ // controlo que no se choque con sigo mismo
- if(ant->x == aux->x && ant->y == aux->y) return NULL;
- else ant = ant->sig; }
- return aux; }
- void dibujarVivorita(struct cViborita *v){
- struct cViborita *aux = v;
- move(aux->y,aux->x);
- printw("@");
- aux = aux->sig;
- while(aux != NULL){
- mvprintw(aux->y,aux->x,"#");
- aux = aux->sig; }
- }
- int obtenerDireccion(int *direccion){
- switch(getch()){
- case 'w': if(*direccion!=3) *direccion = 1; return 1;
- break;
- case 'd': if(*direccion!=4) *direccion = 2; return 1;
- break;
- case 's': if(*direccion!=1) *direccion = 3; return 1;
- break;
- case 'a': if(*direccion!=2) *direccion = 4; return 1;
- break;
- case 'p': return 2; // lanzo pausa
- break; }
- return 0; }
- int verificarMuerte(struct cViborita *v){
- struct winsize w;
- ioctl(0, TIOCGWINSZ, &w);
- if(v == NULL) return 1; // se choco con si misma
- if((BORDE >= v->x)||(v->x >= w.ws_col-BORDE)||
- (BORDE >= v->y)||(v->y >= w.ws_row-BORDE)) return 1;
- return 0; }
- void comida(int *x, int *y, struct cViborita *v){
- // le paso la viborita para que no ponga la comida en el cuerpo de la viborita
- agregarCuerpo(v); // comio! le agrego un cuerpo a vivorita
- struct winsize w;
- ioctl(0, TIOCGWINSZ, &w);
- struct cViborita * aux;
- int flag;
- srand(time(NULL)); // si no siempre da el mismo lugar
- do {
- *x = rand() % ((w.ws_col-BORDE-3)-BORDE+2) + BORDE+1;
- *y = rand() % ((w.ws_row-BORDE-3)-BORDE+2) + BORDE+1;
- aux = v;
- flag = 0;
- while(aux != NULL && !flag){
- if(aux->x == *x && aux->y == *y) flag = 1;
- aux = aux->sig; }
- } while(flag); }
- int main(int argc, char * argv[]){
- initscr(); noecho(); curs_set(0); cbreak(); timeout(0); // ncurses
- struct cViborita *v = (struct cViborita *) malloc(sizeof(struct cViborita));
- v->x = 10; v->y = 10; v->sig = NULL; // inicializo la viborita
- for(int cuerpo = 0; cuerpo < 2; cuerpo++) agregarCuerpo(v);
- int direccion = 2, puntos = 0;
- int comidaX, comidaY;
- comida(&comidaX,&comidaY,v);
- while(1){
- clear();
- move(0,0);
- printw("puntaje: %d", puntos);
- drawCuadradoFULL();
- if(obtenerDireccion(&direccion) == 2){
- move(1,0);
- printw("PAUSA - presione 'p'w'a's'd' para volver o Ctrl+C para salir");
- while(!obtenerDireccion(&direccion)); } // continuo con la pausa
- v = moverViborita(v,direccion);
- if(verificarMuerte(v)){
- endwin();
- printf("MURIO! puntaje: %d\n", puntos);
- return 0; }
- mvaddch(comidaY,comidaX,ACS_DIAMOND); // dibujo la comida
- if(comidaX == v->x && comidaY == v->y){ // la comio?
- puntos++;
- comida(&comidaX,&comidaY,v); }
- dibujarVivorita(v);
- refresh();
- usleep(100000); }
- // no hay necesidad de endwin aqui
- // no hay necesidad de free por que libero la memoria al terminar el juego
- // y las alocaciones siempre crecen, no varian ni se eliminan
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement