Base juego en C (consola)

Iniciado por v4char, 18 Noviembre 2015, 22:14 PM

0 Miembros y 1 Visitante están viendo este tema.

v4char

Esto es una base de juego para dar ideas de como desarroyar un juego en C obiamente no soy un experto en C pero puede servir a la gente que esta empezando y quiere hacer juegos

Código (cpp) [Seleccionar]
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#ifdef _WIN32
  #include <windows.h>
#else
  #include <sys/ioctl.h>
#endif

int getch(void)
{
  struct termios oldattr, newattr;
  int ch;
  tcgetattr( STDIN_FILENO, &oldattr );
  newattr = oldattr;
  newattr.c_lflag &= ~( ICANON | ECHO );
  tcsetattr( STDIN_FILENO, TCSANOW, &newattr );
  ch = getchar();
  tcsetattr( STDIN_FILENO, TCSANOW, &oldattr );
  return ch;
}

void dibujar(int filas,int columnas,char *texto,int pos_x,int pos_y)
{
  int i,j;
  for(i = 0;i < filas;i++)
  {
     for (j = 0;j < columnas;j++)
     {
        if(j == 0 || j == columnas-1)
        {
           printf("|");
        }
        else if(i == 0 || i == filas-1)
        {
           printf("=");
        }
        else if(i==pos_x && j==pos_y)
        {
           printf("%s",texto);
           j = j-1 + strlen(texto);
        }
        else
        {
           printf(" ");
        }
     }
  }  
}
   
int main ()
{
  int columnas,filas,x=5,y=5,tecla;
           
  #ifdef _WIN32
     CONSOLE_SCREEN_BUFFER_INFO csbi;
     GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
     columnas = csbi.srWindow.Right - csbi.srWindow.Left + 1;
     filas = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
  #else
     struct winsize w;
     ioctl(0, TIOCGWINSZ, &w);
     filas=w.ws_row;
     columnas=w.ws_col;
   #endif

  while (1)
  {
     dibujar(filas,columnas,"@" ,x,y);
     tecla = getch();
     if (tecla == 66)
     {
        x++;
     } else if(tecla == 65)
     {
        x--;
     }

     if (tecla == 67)
     {
        y++;
     } else if(tecla == 68)
     {
        y--;
     }
  }
 
  return 0;
}


Por ahora solo funciona en Linux pero estoy intentado hacer la portabilidad, se mueve con las flechas