Kaktusen

Shmup kaktus

Jun 13th, 2011
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.41 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <ctime>
  3.  
  4. #include "SDL/SDL.h"
  5. #include "classes.h"
  6.  
  7. SDL_Surface *screen = NULL;
  8. SDL_Surface *background = NULL;
  9. SDL_Surface *ship = NULL;
  10. SDL_Surface *astorider = NULL;
  11.  
  12. void quit();
  13. void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination);
  14. void move();
  15. void game();
  16. void astroider();
  17.  
  18. SDL_Event event;
  19.  
  20. //variabler
  21. Uint8 *key;
  22.  
  23. int main(int argc, char *args []) {
  24.     player.down = false;
  25.     player.up = false;
  26.     player.mid = true;
  27.     player.Y = 240;
  28.  
  29.     SDL_Init(SDL_INIT_EVERYTHING);
  30.     SDL_WM_SetCaption("Yeda - SHMUP", NULL);
  31.  
  32.     screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
  33.     background = SDL_LoadBMP("image/background.bmp");
  34.     ship = SDL_LoadBMP("image/ship.bmp");
  35.     astorider = SDL_LoadBMP("image/asteroids.bmp");
  36.  
  37.     apply_surface(0, 0, background, screen);
  38.     apply_surface(0, player.Y, ship, screen);
  39.  
  40.     SDL_Flip(screen);
  41.  
  42.     game();
  43.  
  44.     SDL_Delay(2000);
  45.  
  46.     quit();
  47. }
  48.  
  49. void quit() {
  50.     SDL_FreeSurface(background);
  51.     SDL_FreeSurface(ship);
  52.  
  53.     SDL_Quit();
  54. }
  55.  
  56. void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination) {
  57.     SDL_Rect offset;
  58.  
  59.     offset.x = x;
  60.     offset.y = y;
  61.  
  62.     SDL_BlitSurface(source, NULL, destination, &offset);
  63. }
  64.  
  65. void move() {
  66.     if( SDL_PollEvent (&event)) {
  67.  
  68.             if(event.type == SDL_KEYDOWN) {
  69.  
  70.                 switch(event.key.keysym.sym) {
  71.  
  72.                     case SDLK_DOWN:
  73.                         //up
  74.                         if(player.up == true) {
  75.                             player.up = false;
  76.                             player.mid = true;
  77.                             player.Y = 240;
  78.                         }
  79.  
  80.                         //mid
  81.                         else if(player.mid == true) {
  82.                             player.mid = false;
  83.                             player.down = true;
  84.                             player.Y = 280;
  85.                         }
  86.                         break;
  87.  
  88.                     case SDLK_UP:
  89.                         //down
  90.                         if(player.down == true) {
  91.                             player.down = false;
  92.                             player.mid = true;
  93.                             player.Y = 240;
  94.                         }
  95.  
  96.                         //mid
  97.                         else if(player.mid == true) {
  98.                             player.mid = false;
  99.                             player.up = true;
  100.                             player.Y = 200;
  101.                         }
  102.                         break;
  103.  
  104.                     case SDLK_ESCAPE:
  105.                         quit();
  106.                 }
  107.             }
  108.         }
  109. }
  110.  
  111. void game() {
  112.     while(true) {
  113.         apply_surface(0, 0, background, screen);
  114.         apply_surface(0, player.Y, ship, screen);
  115.         astroider();
  116.         move();
  117.         SDL_Flip(screen);
  118.     }
  119. }
  120.  
  121. void astroider() {
  122.     srand(time(NULL));
  123.     ast.X = 640;
  124.  
  125.     while(true) {
  126.         ast.Y = rand() % ast.down + ast.mid + ast.up;
  127.         ast.speed = rand() % 10 + 6;
  128.  
  129.         switch (ast.Y) {
  130.             case ast.down:
  131.                 ast.Y = 280;
  132.                 break;
  133.  
  134.             case ast.mid:
  135.                 ast.y = 240;
  136.                 break;
  137.  
  138.             case ast.up:
  139.                 ast.Y = 200;
  140.                 break;
  141.         }
  142.  
  143.         apply_surface(ast.X, ast.Y, astroider, screen);
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment