Autor
|
Tema: Juego SDL (Leído 2,468 veces)
|
Sir Korsky
Desconectado
Mensajes: 6
La vida sin problemas es matar el tiempo a lo bobo
|
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. #include <SDL/SDL.h> #include <stdio.h> #include <stdlib.h> /* ******************************** * .:La nave:. * * * * Author: Joaquin Cortes * * Version: 0.0.1 * * Name: La nave * * Language: C/C++ * * Library: SDL * * * ******************************** */ #define WIDTH 800 #define HEIGHT 600 #define BTS 16 void iniciarSDL(); void iniciarVideo(); void transparencia(); void imgFondo(); void drawNave(); void moverNave(); void drawEnemiga(); void moverEnemigo(); void Colision(); /* Declaracion de variables */ SDL_Surface *screen; SDL_Surface *nave; SDL_Surface *enemigo; SDL_Surface *fondo; SDL_Rect destino; SDL_Rect destinoe; SDL_Rect destinoNave; /* Posicion incial */ int xNave = 320, yNave = 400; int xEne = 320, yEne = 10; /* Activar las teclas */ Uint8* teclas; int main(int argc, char *argv[]) { int terminado = 0; SDL_Event suceso; /*SDL_Rect clip_rect;*/ iniciarSDL(); /* Imagenes a mostrar */ fondo = SDL_LoadBMP("fondo.bmp"); nave = SDL_LoadBMP("nave.bmp"); enemigo = SDL_LoadBMP("enemigo.bmp"); iniciarVideo(); transparencia(); /* Cambiar titulo de la ventana (titulo, icono) */ SDL_WM_SetCaption("Joaquin Cortes", NULL); /* Gameloop, mientras que terminar sea igual a 0 hacer todo esto */ while(terminado == 0) { imgFondo(); /*clip_rect.x = 100; clip_rect.y = 100; clip_rect.w = 300; clip_rect.h = 580; [Sirve para crear un rectangulo en la pantalla] SDL_SetClipRect(screen, &clip_rect);*/ drawNave(); drawEnemiga(); Colision(); /* Actualizamos la pantalla */ SDL_Flip(screen); /* Miramos si hay algun suceso pendiente, entre ellos, peticion de abandonar el programa (pulsar la X de la ventana) o tecla ESC */ while(SDL_PollEvent(&suceso)){ if(suceso.type == SDL_QUIT) terminado = 1; if(suceso.type == SDL_KEYDOWN) if(suceso.key.keysym.sym == SDLK_ESCAPE) terminado = 1; } moverNave(); moverEnemigo(); /* Esperamos 50 ms antes de repetir */ SDL_Delay(10); } /* Finalmente, preparamos para salir */ SDL_Quit(); return 0; } void iniciarSDL() { /* Inicializamos la Biblioteca SDL */ if(SDL_Init(SDL_INIT_VIDEO) < 0){ printf("No se pudo inicializar SDL: %s\n", SDL_GetError ()); } } void iniciarVideo() { /* Si todo va bien, hacemos esto: entrar a modo grafico y cambiar el titulo de la ventana */ screen = SDL_SetVideoMode(WIDTH, HEIGHT, BTS, SDL_HWSURFACE); if(screen == NULL){ printf("Error al entrar a modo grafico: %s\n", SDL_GetError ()); SDL_Quit(); } } void transparencia() { /* transparencia nave */ SDL_SetColorKey(nave, SDL_SRCCOLORKEY, SDL_MapRGB(nave->format, 0,0,0)); /* transparencia enemigo */ SDL_SetColorKey(enemigo, SDL_SRCCOLORKEY, SDL_MapRGB(enemigo->format, 0,0,0)); } void imgFondo() { /*Dibujamos la imagen de fondo Como tiene 207x211 pixeles, la repetimos varias veces */ int i, j; for(i=0; i<5; i++) { for(j=0; j<3; j++) { destino.x=207*i; destino.y=211*j; SDL_BlitSurface(fondo, NULL, screen, &destino); } } } void drawNave() { /* Dibujamos el nave */ destinoNave.x = xNave; destinoNave.y = yNave; destinoNave.w = 35; destinoNave.h = 58; SDL_BlitSurface(nave, NULL, screen, &destinoNave); } void moverNave() { /* Vemos el estado individual de las demas teclas */ teclas = SDL_GetKeyState(NULL); if(teclas[SDLK_UP] && yNave > 0) yNave -= 5; if(teclas[SDLK_DOWN] && yNave < HEIGHT-45) yNave += 5; if(teclas[SDLK_LEFT] && xNave > 0) xNave -= 5; if(teclas[SDLK_RIGHT] && xNave < WIDTH-25) xNave += 5; } void drawEnemiga() { destinoe.x = xEne; destinoe.y = yEne; destinoe.w = 35; destinoe.h = 58; SDL_BlitSurface(enemigo, NULL, screen, &destinoe); } void moverEnemigo() { yEne += 5; if(destinoe.y == HEIGHT) yEne = 0; } void Colision() { 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)) { yEne = 0; yNave = 540; } }
|
|
|
En línea
|
|
|
|
AlbertoBSD
Programador y
Moderador Global
Desconectado
Mensajes: 3.697
🏴 Libertad!!!!!
|
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 SDL_Surface *enemigo;
supongo que con crear una matriz como dices, es suficiente: 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
Mensajes: 6
La vida sin problemas es matar el tiempo a lo bobo
|
Gracias, ahí lo solucione, con crear un par de vectores era suficiente jeje. Les dejo el código si alguien lo necesita. #include <SDL/SDL.h> #include <stdio.h> #include <stdlib.h> /* ******************************** * .:La nave:. * * * * Author: Joaquin Cortes * * Version: 0.0.1 * * Name: La nave * * Language: C/C++ * * Library: SDL * * * ******************************** */ #define WIDTH 800 #define HEIGHT 600 #define BTS 16 void iniciarSDL(); void iniciarVideo(); void transparencia(); void imgFondo(); void drawNave(); void moverNave(); void drawEnemiga(); void moverEnemigo(); void Colision(); /* Declaracion de variables */ SDL_Surface *screen; SDL_Surface *nave; SDL_Surface *enemigo; SDL_Surface *fondo; SDL_Rect destino; SDL_Rect destinoe[2]; SDL_Rect destinoNave; /* Posicion incial */ int xNave = 320, yNave = 400; int xEne[2] = {320, 230}; int yEne[2] = {40, 30}; /* Activar las teclas */ Uint8* teclas; int main(int argc, char *argv[]) { int terminado = 0; SDL_Event suceso; /*SDL_Rect clip_rect;*/ iniciarSDL(); /* Imagenes a mostrar */ fondo = SDL_LoadBMP("fondo.bmp"); nave = SDL_LoadBMP("nave.bmp"); enemigo = SDL_LoadBMP("enemigo.bmp"); iniciarVideo(); transparencia(); /* Cambiar titulo de la ventana (titulo, icono) */ SDL_WM_SetCaption("Joaquin Cortes", NULL); /* Gameloop, mientras que terminar sea igual a 0 hacer todo esto */ while(terminado == 0) { imgFondo(); /*clip_rect.x = 100; clip_rect.y = 100; clip_rect.w = 300; clip_rect.h = 580; [Sirve para crear un rectangulo en la pantalla] SDL_SetClipRect(screen, &clip_rect);*/ drawNave(); drawEnemiga(); Colision(); /* Actualizamos la pantalla */ SDL_Flip(screen); /* Miramos si hay algun suceso pendiente, entre ellos, peticion de abandonar el programa (pulsar la X de la ventana) o tecla ESC */ while(SDL_PollEvent(&suceso)){ if(suceso.type == SDL_QUIT) terminado = 1; if(suceso.type == SDL_KEYDOWN) if(suceso.key.keysym.sym == SDLK_ESCAPE) terminado = 1; } moverNave(); moverEnemigo(); /* Esperamos 50 ms antes de repetir */ SDL_Delay(10); } /* Finalmente, preparamos para salir */ SDL_Quit(); return 0; } void iniciarSDL() { /* Inicializamos la Biblioteca SDL */ if(SDL_Init(SDL_INIT_VIDEO) < 0){ printf("No se pudo inicializar SDL: %s\n", SDL_GetError ()); } } void iniciarVideo() { /* Si todo va bien, hacemos esto: entrar a modo grafico y cambiar el titulo de la ventana */ screen = SDL_SetVideoMode(WIDTH, HEIGHT, BTS, SDL_HWSURFACE); if(screen == NULL){ printf("Error al entrar a modo grafico: %s\n", SDL_GetError ()); SDL_Quit(); } } void transparencia() { /* transparencia nave */ SDL_SetColorKey(nave, SDL_SRCCOLORKEY, SDL_MapRGB(nave->format, 0,0,0)); /* transparencia enemigo */ SDL_SetColorKey(enemigo, SDL_SRCCOLORKEY, SDL_MapRGB(enemigo->format, 0,0,0)); } void imgFondo() { /*Dibujamos la imagen de fondo Como tiene 207x211 pixeles, la repetimos varias veces */ int i, j; for(i=0; i<5; i++) { for(j=0; j<3; j++) { destino.x=207*i; destino.y=211*j; SDL_BlitSurface(fondo, NULL, screen, &destino); } } } void drawNave() { /* Dibujamos el nave */ destinoNave.x = xNave; destinoNave.y = yNave; destinoNave.w = 35; destinoNave.h = 58; SDL_BlitSurface(nave, NULL, screen, &destinoNave); } void moverNave() { /* Vemos el estado individual de las demas teclas */ teclas = SDL_GetKeyState(NULL); if(teclas[SDLK_UP] && yNave > 0) yNave -= 5; if(teclas[SDLK_DOWN] && yNave < HEIGHT-45) yNave += 5; if(teclas[SDLK_LEFT] && xNave > 0) xNave -= 5; if(teclas[SDLK_RIGHT] && xNave < WIDTH-25) xNave += 5; } void drawEnemiga() { int i; for(i=0; i<2; i++) { destinoe[i].w = 35; destinoe[i].h = 58; destinoe[i].x = xEne[i]; destinoe[i].y = yEne[i]; SDL_BlitSurface(enemigo, NULL, screen, &destinoe[i]); } } void moverEnemigo() { int i; for(i=0; i<2; i++) { yEne[i] += 5; if(destinoe[i].y == HEIGHT) yEne[i] = 0; } } void Colision() { int i; for(i=0; i<2; i++) { if(((destinoNave.x + destinoNave.w) > destinoe[i].x) && ((destinoNave.y + destinoNave.h) > destinoe[i].y) && ((destinoe[i].x + destinoe[i].w) > destinoNave.x) && ((destinoe[i].y + destinoe[i].h) > destinoNave.y)) { yEne[i] = 0; yNave = 540; } } }
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
|
|
|
|
|
Mensajes similares |
|
Asunto |
Iniciado por |
Respuestas |
Vistas |
Último mensaje |
|
|
Un juego MMORPG Asiatico, Juego Gratuito
Juegos y Consolas
|
enrikesz
|
0
|
2,821
|
6 Octubre 2009, 05:46 am
por enrikesz
|
|
|
[Juego] Juego muy adictivo que arme, les juro que es divertido.
Programación Visual Basic
|
79137913
|
5
|
4,312
|
1 Octubre 2010, 15:30 pm
por 7omer
|
|
|
Conoces este juego? A que nivel has llegado? El Juego mas dificil del mundo
« 1 2 »
Juegos y Consolas
|
jdc
|
12
|
16,760
|
28 Septiembre 2012, 22:46 pm
por Bomb-P
|
|
|
[duda] Sobre un juego online de juego flash
Juegos y Consolas
|
Senior++
|
6
|
5,996
|
28 Junio 2011, 08:49 am
por тαптяα
|
|
|
Cinematicas de juego viejo, son comicas, no se como se llama el juego
Juegos y Consolas
|
engel lex
|
0
|
2,980
|
24 Julio 2015, 22:32 pm
por engel lex
|
|