Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 11th, 2012  |  syntax: C  |  size: 3.63 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <SDL/SDL.h>
  2. #include <SDL.h>
  3. #include <windows.h>
  4.  
  5. #define ARRIBA          243
  6. #define IZQUIERDA       120
  7. #define DERECHA         360
  8. #define ABAJO           2
  9.  
  10. int main ( int argc, char** argv )
  11. {
  12.     if (SDL_Init(SDL_INIT_VIDEO)<0)
  13.     {
  14.         return 1;
  15.     }
  16.  
  17.     atexit(SDL_Quit);
  18.  
  19.     SDL_Surface * screen = SDL_SetVideoMode(640,480,16,SDL_HWSURFACE|SDL_DOUBLEBUF);
  20.  
  21.     SDL_Surface * MiNave = SDL_LoadBMP("P1.BMP");       // nuestro personaje
  22.     SDL_Surface * Casa1 = SDL_LoadBMP("Casa1.BMP");     // Casa tipo 1
  23.  
  24.     SDL_Rect pMiNave;  // posicion de nuestro personaje
  25.     pMiNave.x = 0;
  26.     pMiNave.y = 0;
  27.  
  28.     SDL_Rect pCasa1;    // posicion de la casa
  29.     pCasa1.x = 100;
  30.     pCasa1.y = 0;
  31.  
  32.     SDL_SetColorKey(MiNave, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(MiNave->format,255,0,0));
  33.     SDL_SetColorKey(Casa1, SDL_SRCCOLORKEY|SDL_RLEACCEL, SDL_MapRGB(Casa1->format,255,0,0));
  34.  
  35.     SDL_BlitSurface(MiNave, 0, screen, &pMiNave);
  36.     SDL_BlitSurface(Casa1, 0, screen, &pCasa1);
  37.  
  38.     SDL_Flip(screen);
  39.  
  40.     int CicloPrincipal = 1;
  41.     Uint8 * Teclas;
  42.     SDL_Event event;
  43.     SDL_Rect Trozo;
  44.  
  45.     int Izquierda = 0,Derecha = 0,Abajo = 0,Arriba = 0;
  46.     int IO = 0,DO = 0,AO,ARO = 0;
  47.  
  48.     Trozo.x = 0;
  49.  
  50.     while(CicloPrincipal)
  51.     {
  52.         Teclas = SDL_GetKeyState(NULL);
  53.  
  54.         if (Teclas[SDLK_UP])
  55.         {
  56.             pMiNave.y-=2;
  57.  
  58.             IO = 0;
  59.             DO = 0;
  60.             AO = 0;
  61.             ARO = 1;
  62.  
  63.             if (Arriba == 0)
  64.             {
  65.                 Arriba++;
  66.                 Trozo.x = ARRIBA+40;
  67.             }else if(Arriba == 1){
  68.                 Arriba--;
  69.                 Trozo.x = ARRIBA+80;
  70.             }
  71.             goto NoParado;
  72.         }
  73.         if (Teclas[SDLK_DOWN])
  74.         {
  75.             pMiNave.y+=2;
  76.  
  77.             IO = 0;
  78.             DO = 0;
  79.             AO = 1;
  80.             ARO = 0;
  81.  
  82.             if (Abajo == 0)
  83.             {
  84.                 Abajo++;
  85.                 Trozo.x = ABAJO+40;
  86.             }else if(Abajo == 1){
  87.                 Abajo--;
  88.                 Trozo.x = ABAJO+80;
  89.             }
  90.             goto NoParado;
  91.  
  92.         }
  93.         if (Teclas[SDLK_LEFT])
  94.         {
  95.             pMiNave.x-=2;
  96.  
  97.             IO = 1;
  98.             DO = 0;
  99.             AO = 0;
  100.             ARO = 0;
  101.  
  102.             if (Izquierda == 0)
  103.             {
  104.                 Izquierda++;
  105.                 Trozo.x = IZQUIERDA+40;
  106.             }else if(Izquierda == 1){
  107.                 Izquierda--;
  108.                 Trozo.x = IZQUIERDA+80;
  109.             }
  110.             goto NoParado;
  111.         }
  112.         if (Teclas[SDLK_RIGHT])
  113.         {
  114.             pMiNave.x+=2;
  115.  
  116.             IO = 0;
  117.             DO = 1;
  118.             AO = 0;
  119.             ARO = 0;
  120.  
  121.             if (Derecha == 0)
  122.             {
  123.                 Derecha++;
  124.                 Trozo.x = DERECHA+40;
  125.             }else if(Derecha == 1){
  126.                 Derecha--;
  127.                 Trozo.x = DERECHA+80;
  128.             }
  129.             goto NoParado;
  130.         }
  131.  
  132.         if (IO == 1) {Trozo.x = IZQUIERDA ;}
  133.         if (DO == 1) {Trozo.x = DERECHA;}
  134.         if (AO == 1) {Trozo.x = ABAJO;}
  135.         if (ARO == 1) {Trozo.x = ARRIBA;}
  136.  
  137.         NoParado:
  138.         SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format,0,240,100));
  139.  
  140.  
  141.         Trozo.w = 40;
  142.         Trozo.y = 0;
  143.         Trozo.h = 60;
  144.  
  145.  
  146.         SDL_BlitSurface(Casa1, 0, screen, &pCasa1);
  147.         SDL_BlitSurface(MiNave, &Trozo, screen, &pMiNave);
  148.  
  149.         SDL_Flip(screen);
  150.  
  151.         if(SDL_PollEvent(&event))
  152.         {
  153.             if (event.type == SDL_QUIT) { CicloPrincipal = 0; } // Boton salir.
  154.         }
  155.     }
  156.  
  157.     return 0;
  158. }