Advertisement
patrikb42

Untitled

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