Código
#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){ } } 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){ 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; } }