Advertisement
patrikb42

Untitled

Feb 6th, 2018
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.35 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <stdio.h>
  3. #include <SDL_mixer.h>
  4.  
  5. Mix_Music* music = NULL;
  6. Mix_Chunk* seffect = NULL;
  7. SDL_Window* window = NULL;
  8. SDL_Surface* windowsurface = NULL;
  9. SDL_Surface* character = NULL;
  10. SDL_Surface* character2 = NULL;
  11. SDL_Surface* bullet1 = NULL;
  12. SDL_Surface* background = NULL;
  13. SDL_GameController* controller = NULL;
  14. int main(int argc, char* args[]) {
  15.     const Uint8* currentKeyStates = SDL_GetKeyboardState(NULL);
  16.     int bullet1isstoppable = 0;
  17.     bool bullet1exists = false;
  18.     bool y1 = false;
  19.     bool y2 = false;
  20.     bool x1 = false;
  21.     bool x2 = false;
  22.     bool y21 = false;
  23.     bool y22 = false;
  24.     bool x21 = false;
  25.     bool x22 = false;
  26.     int speed = 5;
  27.     int speed2 = 10;
  28.     SDL_Init(SDL_INIT_VIDEO);
  29.     SDL_Init(SDL_INIT_AUDIO);
  30.     Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
  31.     bool pollevent = false;
  32.     SDL_Event movementevent;
  33.     int windowx = 500;
  34.     int windowy = 500;
  35.     int windoww = 400;
  36.     int windowh = 500;
  37.     SDL_Rect movement;
  38.     movement.x = 25;
  39.     movement.y = 25;
  40.     SDL_Rect movement2;
  41.     SDL_Rect bullet1movement;
  42.     bullet1movement.x = 50;
  43.     bullet1movement.y = 50;
  44.     controller = SDL_GameControllerOpen(0);
  45.     window = SDL_CreateWindow("BMPs/movement game", windowx, windowy, windoww, windowh, SDL_WINDOW_FULLSCREEN);
  46.     windowsurface = SDL_GetWindowSurface(window);
  47.     music = Mix_LoadMUS("F777_darkangel.wav");
  48.     character2 = SDL_LoadBMP("BMPs/character2.bmp");
  49.     character = SDL_LoadBMP("BMPs/character.bmp");
  50.     bullet1 = SDL_LoadBMP("BMPs/bullet.bmp");
  51.     background = SDL_LoadBMP("BMPs/background.bmp");
  52.     SDL_SetColorKey(character, SDL_TRUE, 0xFF00FF);
  53.     SDL_SetColorKey(bullet1, SDL_TRUE, 0xFF00FF);
  54.     if (character == NULL) {
  55.     }
  56.     bool quit = false;
  57.     while (quit == false) {
  58.         if (Mix_PlayingMusic() == 0)
  59.         {
  60.             Mix_PlayMusic(music, -1);
  61.         }
  62.         if (Mix_PausedMusic() == 1)
  63.         {
  64.             Mix_ResumeMusic();
  65.         }
  66.         SDL_BlitSurface(background, NULL, windowsurface, NULL);
  67.         SDL_BlitSurface(character2, NULL, windowsurface, &movement2);
  68.         SDL_BlitSurface(character, NULL, windowsurface, &movement);
  69.         SDL_BlitSurface(bullet1, NULL, windowsurface, &bullet1movement);
  70.  
  71.         while (SDL_PollEvent(&movementevent) != 0) {
  72.             SDL_BlitSurface(character2, NULL, windowsurface, &movement2);
  73.             SDL_BlitSurface(character, NULL, windowsurface, &movement);
  74.             SDL_BlitSurface(bullet1, NULL, windowsurface, &bullet1movement);
  75.  
  76.             if (movementevent.type == SDL_QUIT) {
  77.                 quit = true;
  78.             }
  79.             if (movementevent.type == SDL_KEYDOWN) {
  80.                 if (currentKeyStates[SDL_SCANCODE_SPACE]) {
  81.                     bullet1exists = true;
  82.                 }
  83.                 if (currentKeyStates[SDL_SCANCODE_W])
  84.                 {
  85.                     y1 = true;
  86.                 }
  87.                 if (currentKeyStates[SDL_SCANCODE_A]) {
  88.                     x1 = true;
  89.                 }
  90.                 if (currentKeyStates[SDL_SCANCODE_S]) {
  91.                     y2 = true;
  92.                 }
  93.                 if (currentKeyStates[SDL_SCANCODE_D]) {
  94.                     x2 = true;
  95.                 }
  96.                 if (currentKeyStates[SDL_SCANCODE_UP]) {
  97.                     y21 = true;
  98.                 }
  99.                 if (currentKeyStates[SDL_SCANCODE_LEFT]) {
  100.                     x21 = true;
  101.                 }
  102.                 if (currentKeyStates[SDL_SCANCODE_DOWN]) {
  103.                     y22 = true;
  104.                 }
  105.                 if (currentKeyStates[SDL_SCANCODE_RIGHT]) {
  106.                     x22 = true;
  107.                 }
  108.             }
  109.         }
  110.  
  111.         if (y1 == true) {
  112.             movement.y -= speed;
  113.         }
  114.         if (x1 == true) {
  115.             movement.x -= speed;
  116.         }
  117.         if (y2 == true) {
  118.             movement.y += speed;
  119.         }
  120.         if (x2 == true) {
  121.             movement.x += speed;
  122.         }
  123.         if (y21 == true) {
  124.             movement2.y -= speed2;
  125.         }
  126.         if (x21 == true) {
  127.             movement2.x -= speed2;
  128.         }
  129.         if (y22 == true) {
  130.             movement2.y += speed2;
  131.         }
  132.         if (x22 == true) {
  133.             movement2.x += speed2;
  134.         }
  135.         SDL_UpdateWindowSurface(window);
  136.         if (movementevent.type == SDL_KEYUP) {
  137.             if (!currentKeyStates[SDL_SCANCODE_SPACE]) {
  138.                 bullet1exists = false;
  139.             }
  140.             if (!currentKeyStates[SDL_SCANCODE_W])
  141.             {
  142.                 y1 = false;
  143.             }
  144.             if (!currentKeyStates[SDL_SCANCODE_A]) {
  145.                 x1 = false;
  146.             }
  147.             if (!currentKeyStates[SDL_SCANCODE_S]) {
  148.                 y2 = false;
  149.             }
  150.             if (!currentKeyStates[SDL_SCANCODE_D]) {
  151.                 x2 = false;
  152.             }
  153.             if (!currentKeyStates[SDL_SCANCODE_UP]) {
  154.                 y21 = false;
  155.             }
  156.             if (!currentKeyStates[SDL_SCANCODE_LEFT]) {
  157.                 x21 = false;
  158.             }
  159.             if (!currentKeyStates[SDL_SCANCODE_DOWN]) {
  160.                 y22 = false;
  161.             }
  162.             if (!currentKeyStates[SDL_SCANCODE_RIGHT]) {
  163.                 x22 = false;
  164.             }
  165.         }
  166.         if (bullet1exists) {
  167.             bullet1movement.x += 40;
  168.         }
  169.         if (!bullet1exists && bullet1movement.x > 50) {
  170.             bullet1movement.x -= 40;
  171.         }
  172.     }
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement