desdemona

00:22 alieny leca w dol

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