AntonioVillanueva

Moviendo un objeto en ncurses ...

Feb 25th, 2019
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. //instalar las ncurses antes      sudo apt-get install libncurses-dev
  2. //compilacion  g++ -std=c++11 -o juego  juego.cpp -lncurses
  3. //Antonio Villanueva Segura
  4.  
  5. #include <iostream>
  6. #include <stdio.h>
  7. #include <ncurses.h>
  8. #include <string>
  9. #include <unistd.h> //usleep
  10. using namespace std;
  11. /***********************************************************************/
  12. struct maximo{int max_x;int max_y;} maximos;//Para guardar los limites del terminal
  13. /***********************************************************************/
  14. void inicializaGraficos (){//inicializa ncurses un sistema grafico basico
  15.  
  16.         WINDOW * win = initscr();
  17.         clear();  /* Borra la pantalla entera bajo ncurses */
  18.         refresh(); /* Actualiza la ventana con los cambios */
  19.         curs_set(FALSE); // Don't display a cursor
  20.         noecho();
  21.         cbreak();
  22.         timeout(1);
  23.         keypad(win, TRUE);
  24.         getmaxyx(win,maximos.max_y,maximos.max_x);      /* obtiene el numero de colummnas y lineas  */  
  25.         //Correccion para el interior del terminal
  26.         maximos.max_x-=1;
  27.         maximos.max_y-=1;                
  28. }
  29. /***********************************************************************/
  30. void dentroLimites(int &x,int &y){//Controla que el objeto este en los limites ,cuadro del juego
  31.     if (x<1){ x=maximos.max_x-1;}
  32.     if (x>maximos.max_x){ x=1;}
  33.    
  34.     if (y<1){ y=maximos.max_y-1;}
  35.     if (y>maximos.max_y-1){ y=1;}    
  36. }
  37.  
  38. /***********************************************************************/
  39. void gotoxy(int x,int y,string objeto ="*",bool limpia=false){//Mueve un objeto por defecto "*" a una coordenada x,y y puede borrar lo anterior
  40.     if (limpia) {erase();}//Si se necesita limpiar el fondo ...true
  41.         //move(x,y);
  42.         mvprintw(y,x,objeto.c_str());  //Imprime objeto en coordenada y,x    
  43.         refresh();
  44.     }
  45.    
  46. /***********************************************************************/    
  47.  void dibujaLados(){//Marco del juego .Se dibuja cada vez
  48.          
  49.     for(int x=1; x<=maximos.max_x; x++){// desplazo  En X
  50.         gotoxy(x,1);//Linea superior de * en y=1
  51.         gotoxy(x,maximos.max_y);//Linea inferior
  52.     }
  53.  
  54.     for(int y=1; y<=maximos.max_y; y++){//desplazo En Y
  55.         gotoxy(1,y);//Lateral Izq. *      
  56.         gotoxy(maximos.max_x,y);//Lateral Derch. *
  57.     }
  58.  }
  59. /***********************************************************************/
  60. int main(){
  61.     string objeto("X");//Objeto figura que se desplaza
  62.     int juego(0);//lectura de tecla
  63.    
  64.     inicializaGraficos();//Inicializar el sistema grafico ncurses
  65.     int x( maximos.max_x/2),y( maximos.max_y/2);
  66.    
  67.     while(juego!='z'){//Bucle principal del juego
  68.  
  69.         gotoxy(x,y,objeto,true);//Mueve el "objeto" en este caso la letra o , borrando el trazado anterior
  70.         dibujaLados();//Dibuja la pantalla general , fondo del juego        
  71.      
  72.         juego=getch();//Lee una tecla para desplazar nuestro objeto , la letra o aunque podemos cambiar
  73.        
  74.         switch(juego) {//Mueve el objeto en x e y en funcion de las flechas y cambia la forma del obj. en funcion de la direccion
  75.             case KEY_LEFT: x--;objeto="<";   break;
  76.             case KEY_RIGHT: x++;objeto=">";   break;
  77.             case KEY_UP: y--;objeto="^";   break;
  78.             case KEY_DOWN: y++;objeto="v" ;  break;      
  79.         }
  80.  
  81.         dentroLimites(x,y);//Controla que el objeto este en los limites ,cuadro del juego
  82.         usleep(10000);
  83.     }
  84. return 0;
  85. }
Add Comment
Please, Sign In to add comment