desdemona

czesc 2a z dobrym poruszaniem elienow

Jan 1st, 2012
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.42 KB | None | 0 0
  1. extern "C" {
  2. #include"SDL.h"
  3. }
  4. #define _USE_MATH_DEFINES
  5. #include<math.h>
  6. #include<stdio.h>
  7. #include<string.h>
  8. #if defined(WINDOWS) || defined(WIN32) || defined(_WIN32)
  9. #include<windows.h>
  10. #endif
  11.  
  12. //The attributes of the screen
  13. const int SCREEN_WIDTH = 640;
  14. const int SCREEN_HEIGHT = 480;
  15. const int SCREEN_BPP = 32;
  16.  
  17. //The surfaces
  18. SDL_Surface *background = NULL;
  19. SDL_Surface *alien = NULL;
  20. SDL_Surface *screen = NULL;
  21. SDL_Surface *charset = NULL;
  22. SDL_Surface *spaceship = NULL;
  23. SDL_Surface *bullet = NULL;
  24.  
  25.  
  26. bool shot=false;
  27. int shipwidth=50;
  28. int y=SCREEN_HEIGHT-shipwidth;
  29. double alienY=0;
  30. double alienX=0;
  31. bool quit=false;
  32.  
  33. int alienstab[6][6];
  34.  
  35. double direction=1;
  36.  
  37. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
  38. {
  39.     //Holds offsets
  40.     SDL_Rect offset;
  41.  
  42.     //Get offsets
  43.     offset.x = x;
  44.     offset.y = y;
  45.  
  46.     //Blit
  47.     SDL_BlitSurface( source, clip, destination, &offset );
  48. }
  49.  
  50. // DrawString teraz jest write_string
  51. // narysowanie napisu txt na powierzchni screen, zaczynając od punktu (x, y)
  52. // charset to bitmapa 128x128 zawierająca znaki
  53. void write_string(SDL_Surface *screen, int x, int y, const char *text, SDL_Surface *charset)
  54. {
  55.     int px, py, c;
  56.     SDL_Rect s, d;
  57.     s.w = 8;
  58.     s.h = 8;
  59.     d.w = 8;
  60.     d.h = 8;
  61.     while(*text)
  62.     {
  63.         c = *text & 255;
  64.         px = (c % 16) * 8;
  65.         py = (c / 16) * 8;
  66.         s.x = px;
  67.         s.y = py;
  68.         d.x = x;
  69.         d.y = y;
  70.         SDL_BlitSurface(charset, &s, screen, &d);
  71.         x += 8;
  72.         text++;
  73.     };
  74. };
  75.  
  76. void write_time(SDL_Surface *screen, SDL_Surface *charset, double totaltime)
  77. {
  78.     int width, height;
  79.     char text[128];
  80.  
  81.     apply_surface(0, 0, background, screen, NULL);
  82.  
  83.     width = screen->w;
  84.     height = screen->h;
  85.  
  86.    
  87.     sprintf(text, "doskonale bawisz sie juz %.1lf sekund", totaltime);
  88.     write_string(screen, width / 2 - strlen(text) * 8 / 2, 10, text, charset);
  89.     sprintf(text, "Esc lub krzyzyk - wyjscie", totaltime);
  90.     write_string(screen, width / 2 - strlen(text) * 8 / 2, 26, text, charset);
  91. };
  92.  
  93. void move_aliens(SDL_Surface *screen, SDL_Surface *alien, double delta)
  94. {
  95.     double alienV=50;
  96.  
  97.     //const int SCREEN_WIDTH = 640;
  98.     //const int SCREEN_HEIGHT = 480;
  99.  
  100.     int alienw=44;
  101.     int alienh=36;
  102.  
  103.     int x=0;
  104.     int y=0;
  105.  
  106.    
  107.     alienX+=alienV*delta*direction;
  108.  
  109.  
  110.     int d=5; //odlegosc miedzy alienami na szerokosc
  111.     int h=3; //odlegolsc miedzy alienami na wysokosc
  112.  
  113.     for(x=0; x<6; x++)
  114.     {
  115.         for(y=0; y<6; y++)
  116.         {
  117.             if(alienstab[x][y]==1)
  118.             {
  119.                 apply_surface(alienX + x*(alienw+d), alienY+(y*alienh), alien, screen, NULL);
  120.             }
  121.         }  
  122.     }
  123.  
  124.     if(alienX + x*(alienw+d)  > SCREEN_WIDTH)
  125.     {
  126.       direction=-1;
  127.         alienY=alienY+alienh;
  128.     }
  129.  
  130.     if(alienX<0)
  131.         {
  132.       direction=1;
  133.       alienY=alienY+alienh;
  134.      
  135.  
  136.     }
  137.  
  138.     if(alienY+(y*alienh)+50 >SCREEN_HEIGHT)
  139.     {
  140.         quit=true;
  141.     }
  142. }
  143.  
  144. void draw_bullet(int x, int y, SDL_Surface *bullet)
  145. {
  146.     if(y>10)
  147.     {
  148.         apply_surface(x, y, bullet, screen, NULL);
  149.     }
  150.     else
  151.     {
  152.         shot=false;
  153.         y=SCREEN_HEIGHT-shipwidth;
  154.     }
  155. }
  156.  
  157. void clean_up()
  158. {
  159.     // zwolnienie powierzchni
  160.     SDL_FreeSurface(background);
  161.     SDL_FreeSurface(alien);
  162.     SDL_FreeSurface(charset);
  163.     SDL_FreeSurface(spaceship);
  164.     SDL_FreeSurface(bullet);
  165.  
  166.     SDL_Quit();
  167. }
  168.  
  169. // WinMain
  170. #if defined(WINDOWS) || defined(WIN32) || defined(_WIN32)
  171. int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR str, int show) {
  172. #else
  173. int main(int argc, const char **argv) {
  174. #endif
  175.  
  176.     int t1, t2;
  177.     double totaltime;
  178.     double angle;
  179.     double delta;
  180.     SDL_Event event;
  181.  
  182.     double shipV = 500;
  183.     int bulletV= 300;
  184.  
  185.     Uint8 *keystates = SDL_GetKeyState(NULL);
  186.  
  187.  
  188.     if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
  189.     {
  190.         printf("error: %s\n", SDL_GetError());
  191.         return 1;
  192.     };
  193.  
  194.     screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
  195.  
  196.  
  197.     if(screen == NULL)
  198.     {
  199.         printf("SerVideoMode error: %s\n", SDL_GetError());
  200.         return 1;
  201.     };
  202.  
  203.     //Set the window caption
  204.     SDL_WM_SetCaption( "spejs inwejders", NULL );
  205.  
  206.     // wyłączenie widoczności kursora myszy
  207.     SDL_ShowCursor(SDL_DISABLE);
  208.  
  209.     // wczytanie obrazka cs8x8.bmp
  210.     charset = SDL_LoadBMP("CS8X8.BMP");
  211.     // ustawienie w powierzchni charset koloru przezroczystego
  212.     // od tej chwili pixele koloru 0x000000 na tej powierzchni (czarne)
  213.     // będą przezroczyste
  214.     SDL_SetColorKey(charset, SDL_SRCCOLORKEY, 0x000000);
  215.  
  216.     alien = SDL_LoadBMP("elien.bmp");
  217.     SDL_SetColorKey(alien, SDL_SRCCOLORKEY, 0x000000);
  218.  
  219.     spaceship = SDL_LoadBMP("mem.bmp");
  220.     SDL_SetColorKey(spaceship, SDL_SRCCOLORKEY, 0xFF0000);
  221.  
  222.     bullet = SDL_LoadBMP("rainbow.bmp");
  223.  
  224.     background = SDL_LoadBMP("chet.bmp");
  225.  
  226.     //alienstab is full of aliens. ;)
  227.     for(int i=0; i<6; i++)
  228.     {
  229.         for(int j=0; j<6; j++)
  230.         {
  231.             alienstab[i][j]=1;
  232.         }
  233.     }
  234.  
  235.     //Time management
  236.     totaltime=0;
  237.     //t1 is constant during whole thing
  238.     t1 = SDL_GetTicks();
  239.  
  240.     //Apply the bg to the screen
  241.     apply_surface( 0, 0, background, screen );
  242.  
  243.     //Update the screen
  244.     if( SDL_Flip( screen ) == -1 )
  245.     {
  246.         return 1;
  247.     }
  248.  
  249.     double shipX=320;
  250.     int x;
  251.  
  252.  
  253.     while(quit==false)
  254.     {
  255.         t2 = SDL_GetTicks();
  256.  
  257.         // w tym momencie t2-t1 to czas w milisekundach,
  258.         // jaki uplynal od ostatniego narysowania ekranu
  259.         // delta to czas ostatniej klatki w sekundach
  260.         delta = (t2 - t1) * 0.001;
  261.         t1 = t2;
  262.         totaltime +=delta;
  263.  
  264.         write_time(screen, charset, totaltime);
  265.         move_aliens(screen, alien, delta);
  266.         apply_surface(shipX, 430, spaceship, screen, NULL);
  267.  
  268.         if(shot==true)
  269.         {
  270.             y-=bulletV*delta;
  271.             draw_bullet(x, y, bullet);
  272.         }
  273.  
  274.         //Update the screen
  275.         if( SDL_Flip( screen ) == -1 )
  276.         {
  277.             return 1;
  278.         }
  279.  
  280.         //While there's events to handle
  281.         while( SDL_PollEvent( &event ) )
  282.         {
  283.             //If the user has Xed out the window
  284.             if( event.type == SDL_QUIT )
  285.             {
  286.                 //Quit the program
  287.                 quit = true;
  288.             }
  289.  
  290.             if( event.type == SDL_KEYDOWN )
  291.             {
  292.                 switch( event.key.keysym.sym )
  293.                 {
  294.                 case SDLK_ESCAPE:
  295.                     {
  296.                         quit = true;
  297.                         break;
  298.                     }
  299.                 case SDLK_SPACE:
  300.                     {
  301.                         if(shot==false)
  302.                         {
  303.                             x=shipX+18;
  304.                             y=430;
  305.                             shot=true;
  306.                         }
  307.                         break;
  308.                     }
  309.                 }
  310.             }
  311.         }
  312.  
  313.         //If left is pressed
  314.         if( keystates[ SDLK_LEFT ] )
  315.         {
  316.             if(shipX>0)
  317.             {
  318.                 shipX-=delta*shipV;
  319.                    
  320.             }
  321.         }
  322.  
  323.         //If right is pressed
  324.         if( keystates[ SDLK_RIGHT ] )
  325.         {
  326.             if(shipX<SCREEN_WIDTH-shipwidth)
  327.             {
  328.                 shipX+=delta*shipV;
  329.             }
  330.         }
  331.     }
  332.  
  333.     clean_up();
  334.     return 0;
  335.  
  336.     };
Advertisement
Add Comment
Please, Sign In to add comment