elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Sigue las noticias más importantes de seguridad informática en el Twitter! de elhacker.NET


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación C/C++ (Moderadores: Eternal Idol, Littlehorse, K-YreX)
| | |-+  Juego SDL
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Juego SDL  (Leído 2,451 veces)
Sir Korsky

Desconectado Desconectado

Mensajes: 6


La vida sin problemas es matar el tiempo a lo bobo


Ver Perfil
Juego SDL
« en: 19 Agosto 2016, 19:32 pm »

Hola comunidad, esta programando el juego de la nave, pero no se me ocurre como aumentar las naves enemigas. Me puse a pensar y creo que puede ser creando una matriz, pero no se. Les dejo el código, talvez me puedan ayudar. Saludos.

Código
  1. #include <SDL/SDL.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. /* ********************************
  6.    *          .:La nave:.         *
  7.    *                              *
  8.    * Author: Joaquin Cortes       *
  9.    * Version: 0.0.1               *
  10.    * Name: La nave                *
  11.    * Language: C/C++              *
  12.    * Library: SDL                 *
  13.    *                              *
  14.    ******************************** */
  15.  
  16. #define WIDTH 800
  17. #define HEIGHT 600
  18. #define BTS 16
  19.  
  20. void iniciarSDL();
  21. void iniciarVideo();
  22. void transparencia();
  23. void imgFondo();
  24. void drawNave();
  25. void moverNave();
  26. void drawEnemiga();
  27. void moverEnemigo();
  28. void Colision();
  29.  
  30. /* Declaracion de variables */
  31. SDL_Surface *screen;
  32. SDL_Surface *nave;
  33. SDL_Surface *enemigo;
  34. SDL_Surface *fondo;
  35. SDL_Rect destino;
  36. SDL_Rect destinoe;
  37. SDL_Rect destinoNave;
  38.  
  39. /* Posicion incial */
  40. int xNave = 320, yNave = 400;
  41. int xEne = 320, yEne = 10;
  42.  
  43. /* Activar las teclas */
  44. Uint8* teclas;
  45.  
  46. int main(int argc, char *argv[])
  47. {
  48.    int terminado = 0;
  49.    SDL_Event suceso;
  50.    /*SDL_Rect clip_rect;*/
  51.  
  52.    iniciarSDL();
  53.  
  54.    /* Imagenes a mostrar */
  55.    fondo = SDL_LoadBMP("fondo.bmp");
  56.    nave = SDL_LoadBMP("nave.bmp");
  57.    enemigo = SDL_LoadBMP("enemigo.bmp");
  58.  
  59.    iniciarVideo();
  60.    transparencia();
  61.  
  62.    /* Cambiar titulo de la ventana (titulo, icono) */
  63.    SDL_WM_SetCaption("Joaquin Cortes", NULL);
  64.  
  65.    /* Gameloop, mientras que terminar sea igual a 0 hacer todo esto */
  66.    while(terminado == 0)
  67.    {
  68.  
  69.        imgFondo();
  70.        /*clip_rect.x = 100;
  71.         clip_rect.y = 100;
  72.         clip_rect.w = 300;
  73.         clip_rect.h = 580;
  74.  
  75.         [Sirve para crear un rectangulo en la pantalla]
  76.         SDL_SetClipRect(screen, &clip_rect);*/
  77.        drawNave();
  78.        drawEnemiga();
  79.  
  80.        Colision();
  81.        /* Actualizamos la pantalla */
  82.        SDL_Flip(screen);
  83.  
  84.        /* Miramos si hay algun suceso pendiente,
  85.             entre ellos, peticion de abandonar el programa
  86.             (pulsar la X de la ventana) o tecla ESC */
  87.        while(SDL_PollEvent(&suceso)){
  88.            if(suceso.type == SDL_QUIT)
  89.                terminado = 1;
  90.            if(suceso.type == SDL_KEYDOWN)
  91.                if(suceso.key.keysym.sym == SDLK_ESCAPE) terminado = 1;
  92.        }
  93.  
  94.        moverNave();
  95.        moverEnemigo();
  96.  
  97.        /* Esperamos 50 ms antes de repetir */
  98.        SDL_Delay(10);
  99.    }
  100.  
  101.    /* Finalmente, preparamos para salir */
  102.    SDL_Quit();
  103.    return 0;
  104. }
  105.  
  106. void iniciarSDL()
  107. {
  108.    /* Inicializamos la Biblioteca SDL */
  109.    if(SDL_Init(SDL_INIT_VIDEO) < 0){
  110.        printf("No se pudo inicializar SDL: %s\n", SDL_GetError());
  111.        exit(1);
  112.    }
  113. }
  114.  
  115. void iniciarVideo()
  116. {
  117.    /* Si todo va bien, hacemos esto:
  118.         entrar a modo grafico y cambiar el titulo de la ventana */
  119.    screen = SDL_SetVideoMode(WIDTH, HEIGHT, BTS, SDL_HWSURFACE);
  120.    if(screen == NULL){
  121.        printf("Error al entrar a modo grafico: %s\n", SDL_GetError());
  122.        SDL_Quit();
  123.    }
  124. }
  125.  
  126. void transparencia()
  127. {
  128.    /* transparencia nave */
  129.    SDL_SetColorKey(nave, SDL_SRCCOLORKEY,
  130.                    SDL_MapRGB(nave->format, 0,0,0));
  131.  
  132.    /* transparencia enemigo */
  133.    SDL_SetColorKey(enemigo, SDL_SRCCOLORKEY,
  134.                    SDL_MapRGB(enemigo->format, 0,0,0));
  135. }
  136.  
  137. void imgFondo()
  138. {
  139.    /*Dibujamos la imagen de fondo
  140.         Como tiene 207x211 pixeles, la repetimos varias veces */
  141.    int i, j;
  142.  
  143.    for(i=0; i<5; i++)
  144.    {
  145.        for(j=0; j<3; j++)
  146.        {
  147.            destino.x=207*i;
  148.            destino.y=211*j;
  149.            SDL_BlitSurface(fondo, NULL, screen, &destino);
  150.        }
  151.    }
  152. }
  153.  
  154. void drawNave()
  155. {
  156.    /* Dibujamos el nave */
  157.    destinoNave.x = xNave;
  158.    destinoNave.y = yNave;
  159.    destinoNave.w = 35;
  160.    destinoNave.h = 58;
  161.    SDL_BlitSurface(nave, NULL, screen, &destinoNave);
  162. }
  163.  
  164. void moverNave()
  165. {
  166.    /* Vemos el estado individual de las demas teclas */
  167.    teclas = SDL_GetKeyState(NULL);
  168.    if(teclas[SDLK_UP] && yNave > 0)
  169.        yNave -= 5;
  170.    if(teclas[SDLK_DOWN] && yNave < HEIGHT-45)
  171.        yNave += 5;
  172.    if(teclas[SDLK_LEFT] && xNave > 0)
  173.        xNave -= 5;
  174.    if(teclas[SDLK_RIGHT] && xNave < WIDTH-25)
  175.        xNave += 5;
  176. }
  177.  
  178. void drawEnemiga()
  179. {
  180.    destinoe.x = xEne;
  181.    destinoe.y = yEne;
  182.    destinoe.w = 35;
  183.    destinoe.h = 58;
  184.    SDL_BlitSurface(enemigo, NULL, screen, &destinoe);
  185. }
  186.  
  187. void moverEnemigo()
  188. {
  189.    yEne += 5;
  190.    if(destinoe.y == HEIGHT)
  191.        yEne = 0;
  192. }
  193.  
  194. void Colision()
  195. {
  196.    if(((destinoNave.x + destinoNave.w) > destinoe.x) && ((destinoNave.y + destinoNave.h) > destinoe.y) && ((destinoe.x + destinoe.w) > destinoNave.x) && ((destinoe.y + destinoe.h) > destinoNave.y))
  197.    {
  198.        yEne = 0;
  199.        yNave = 540;
  200.    }
  201. }
  202.  


En línea

AlbertoBSD
Programador y
Moderador Global
***
Desconectado Desconectado

Mensajes: 3.696


🏴 Libertad!!!!!


Ver Perfil WWW
Re: Juego SDL
« Respuesta #1 en: 19 Agosto 2016, 19:42 pm »

Basicamente solo tienes que repintar mas naves, mas veces, y guardar sus posciones para saber si han colicionado o no

Tu variable nave se mantiene tal cual solo necesitas guardar las posiciones
Código
  1. SDL_Surface *enemigo;

supongo que con crear una matriz como dices, es suficiente:

Código
  1. SDL_Rect destinoe[10];

Claro que tendrias que regraficar todas y cada una de esas poisicones

O lo puedes hacer dinamicamente mediante uso de apuntadores y memoria dinámica.

Saludos!


En línea

Sir Korsky

Desconectado Desconectado

Mensajes: 6


La vida sin problemas es matar el tiempo a lo bobo


Ver Perfil
Re: Juego SDL
« Respuesta #2 en: 20 Agosto 2016, 02:30 am »

Gracias, ahí lo solucione, con crear un par de vectores era suficiente jeje. Les dejo el código si alguien lo necesita.
Código
  1. #include <SDL/SDL.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. /* ********************************
  6.    *          .:La nave:.         *
  7.    *                              *
  8.    * Author: Joaquin Cortes       *
  9.    * Version: 0.0.1               *
  10.    * Name: La nave                *
  11.    * Language: C/C++              *
  12.    * Library: SDL                 *
  13.    *                              *
  14.    ******************************** */
  15.  
  16. #define WIDTH 800
  17. #define HEIGHT 600
  18. #define BTS 16
  19.  
  20. void iniciarSDL();
  21. void iniciarVideo();
  22. void transparencia();
  23. void imgFondo();
  24. void drawNave();
  25. void moverNave();
  26. void drawEnemiga();
  27. void moverEnemigo();
  28. void Colision();
  29.  
  30. /* Declaracion de variables */
  31. SDL_Surface *screen;
  32. SDL_Surface *nave;
  33. SDL_Surface *enemigo;
  34. SDL_Surface *fondo;
  35.  
  36. SDL_Rect destino;
  37. SDL_Rect destinoe[2];
  38. SDL_Rect destinoNave;
  39.  
  40. /* Posicion incial */
  41. int xNave = 320, yNave = 400;
  42. int xEne[2] = {320, 230};
  43. int yEne[2] = {40, 30};
  44.  
  45. /* Activar las teclas */
  46. Uint8* teclas;
  47.  
  48. int main(int argc, char *argv[])
  49. {
  50.    int terminado = 0;
  51.    SDL_Event suceso;
  52.    /*SDL_Rect clip_rect;*/
  53.  
  54.    iniciarSDL();
  55.  
  56.    /* Imagenes a mostrar */
  57.    fondo = SDL_LoadBMP("fondo.bmp");
  58.    nave = SDL_LoadBMP("nave.bmp");
  59.    enemigo = SDL_LoadBMP("enemigo.bmp");
  60.  
  61.    iniciarVideo();
  62.    transparencia();
  63.  
  64.    /* Cambiar titulo de la ventana (titulo, icono) */
  65.    SDL_WM_SetCaption("Joaquin Cortes", NULL);
  66.  
  67.    /* Gameloop, mientras que terminar sea igual a 0 hacer todo esto */
  68.    while(terminado == 0)
  69.    {
  70.  
  71.        imgFondo();
  72.        /*clip_rect.x = 100;
  73.         clip_rect.y = 100;
  74.         clip_rect.w = 300;
  75.         clip_rect.h = 580;
  76.  
  77.         [Sirve para crear un rectangulo en la pantalla]
  78.         SDL_SetClipRect(screen, &clip_rect);*/
  79.        drawNave();
  80.        drawEnemiga();
  81.  
  82.        Colision();
  83.        /* Actualizamos la pantalla */
  84.        SDL_Flip(screen);
  85.  
  86.        /* Miramos si hay algun suceso pendiente,
  87.             entre ellos, peticion de abandonar el programa
  88.             (pulsar la X de la ventana) o tecla ESC */
  89.        while(SDL_PollEvent(&suceso)){
  90.            if(suceso.type == SDL_QUIT)
  91.                terminado = 1;
  92.            if(suceso.type == SDL_KEYDOWN)
  93.                if(suceso.key.keysym.sym == SDLK_ESCAPE) terminado = 1;
  94.        }
  95.  
  96.        moverNave();
  97.        moverEnemigo();
  98.  
  99.        /* Esperamos 50 ms antes de repetir */
  100.        SDL_Delay(10);
  101.    }
  102.  
  103.    /* Finalmente, preparamos para salir */
  104.    SDL_Quit();
  105.    return 0;
  106. }
  107.  
  108. void iniciarSDL()
  109. {
  110.    /* Inicializamos la Biblioteca SDL */
  111.    if(SDL_Init(SDL_INIT_VIDEO) < 0){
  112.        printf("No se pudo inicializar SDL: %s\n", SDL_GetError());
  113.        exit(1);
  114.    }
  115. }
  116.  
  117. void iniciarVideo()
  118. {
  119.    /* Si todo va bien, hacemos esto:
  120.         entrar a modo grafico y cambiar el titulo de la ventana */
  121.    screen = SDL_SetVideoMode(WIDTH, HEIGHT, BTS, SDL_HWSURFACE);
  122.    if(screen == NULL){
  123.        printf("Error al entrar a modo grafico: %s\n", SDL_GetError());
  124.        SDL_Quit();
  125.    }
  126. }
  127.  
  128. void transparencia()
  129. {
  130.    /* transparencia nave */
  131.    SDL_SetColorKey(nave, SDL_SRCCOLORKEY,
  132.                    SDL_MapRGB(nave->format, 0,0,0));
  133.  
  134.    /* transparencia enemigo */
  135.    SDL_SetColorKey(enemigo, SDL_SRCCOLORKEY,
  136.                    SDL_MapRGB(enemigo->format, 0,0,0));
  137. }
  138.  
  139. void imgFondo()
  140. {
  141.    /*Dibujamos la imagen de fondo
  142.         Como tiene 207x211 pixeles, la repetimos varias veces */
  143.    int i, j;
  144.  
  145.    for(i=0; i<5; i++)
  146.    {
  147.        for(j=0; j<3; j++)
  148.        {
  149.            destino.x=207*i;
  150.            destino.y=211*j;
  151.            SDL_BlitSurface(fondo, NULL, screen, &destino);
  152.        }
  153.    }
  154. }
  155.  
  156. void drawNave()
  157. {
  158.    /* Dibujamos el nave */
  159.    destinoNave.x = xNave;
  160.    destinoNave.y = yNave;
  161.    destinoNave.w = 35;
  162.    destinoNave.h = 58;
  163.    SDL_BlitSurface(nave, NULL, screen, &destinoNave);
  164. }
  165.  
  166. void moverNave()
  167. {
  168.    /* Vemos el estado individual de las demas teclas */
  169.    teclas = SDL_GetKeyState(NULL);
  170.    if(teclas[SDLK_UP] && yNave > 0)
  171.        yNave -= 5;
  172.    if(teclas[SDLK_DOWN] && yNave < HEIGHT-45)
  173.        yNave += 5;
  174.    if(teclas[SDLK_LEFT] && xNave > 0)
  175.        xNave -= 5;
  176.    if(teclas[SDLK_RIGHT] && xNave < WIDTH-25)
  177.        xNave += 5;
  178. }
  179.  
  180. void drawEnemiga()
  181. {
  182.    int i;
  183.  
  184.    for(i=0; i<2; i++)
  185.    {
  186.        destinoe[i].w = 35;
  187.        destinoe[i].h = 58;
  188.        destinoe[i].x = xEne[i];
  189.        destinoe[i].y = yEne[i];
  190.        SDL_BlitSurface(enemigo, NULL, screen, &destinoe[i]);
  191.    }
  192. }
  193.  
  194. void moverEnemigo()
  195. {
  196.    int i;
  197.  
  198.    for(i=0; i<2; i++)
  199.    {
  200.        yEne[i] += 5;
  201.        if(destinoe[i].y == HEIGHT)
  202.            yEne[i] = 0;
  203.    }
  204. }
  205.  
  206. void Colision()
  207. {
  208.    int i;
  209.  
  210.    for(i=0; i<2; i++)
  211.    {
  212.        if(((destinoNave.x + destinoNave.w) > destinoe[i].x)
  213.            && ((destinoNave.y + destinoNave.h) > destinoe[i].y)
  214.            && ((destinoe[i].x + destinoe[i].w) > destinoNave.x)
  215.           && ((destinoe[i].y + destinoe[i].h) > destinoNave.y))
  216.        {
  217.            yEne[i] = 0;
  218.            yNave = 540;
  219.        }
  220.    }
  221. }

Otra cosa. ¿Algo que pueda mejorar? ya que arranque hace poco a programar videojuegos en C, y cualquier opinión sirve de ayuda.
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines