Advertisement
Guest User

SFML Space Shooter

a guest
Jan 27th, 2011
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.36 KB | None | 0 0
  1.  
  2. //
  3. // An SFML-based Space Shooter
  4. //
  5.  
  6.  
  7. #include "SFML/Graphics.hpp"
  8. #include "SFML/Window.hpp"
  9. #include <iostream> // Included in case I wanted to print data into the console (for debugging purposes)
  10.  
  11. // Avoiding magic numbers
  12.  
  13. #define RIGHT_BORDER 725
  14. #define LEFT_BORDER 25
  15. #define TOP_BORDER 0
  16. #define BOTTOM_BORDER 600
  17. #define SPAWN_POS1 350
  18.  
  19. using namespace std;    // For ease of use of iostream
  20.  
  21. //
  22. // Global Variables
  23. //
  24.  
  25. sf::RenderWindow window(sf::VideoMode(800,600,32), "Space Shooter");
  26. sf::Event event;
  27. sf::Clock Clock;
  28. sf::Clock bulletClock[3];
  29. bool bulletExists[3] = {false,false,false};
  30.  
  31. //
  32. // Forward declaration of functions (all functions are at the last portion of the code)
  33. //
  34.  
  35. void EndProgram();
  36. void Move();
  37. void Shoot();
  38. void MoveBullet(int i);
  39. void MoveNme();
  40.  
  41. //
  42. // Data Structures (I use structs because I want everything to be public)
  43. //
  44.  
  45.  
  46. // Game Data (only images for now)
  47.  
  48. struct Data {
  49.  
  50.     sf::Image bullet1;
  51.     sf::Image bullet2;
  52.     sf::Image Nme1;
  53.     sf::Image Nme2;
  54.     sf::Image player1;
  55.     sf::Image player2;
  56.  
  57.     Data()
  58.     {
  59.         bullet1.LoadFromFile("bala1.png");
  60.         bullet2.LoadFromFile("bala2.png");
  61.         Nme1.LoadFromFile("malo1.png");
  62.         Nme2.LoadFromFile("malo2.png");
  63.         player1.LoadFromFile("nave.png");
  64.         player2.LoadFromFile("nave2.png");
  65.     }
  66.  
  67. }data;
  68.  
  69.  
  70.  
  71. // Generic struct for all image objects
  72.  
  73. struct Image
  74. {
  75.  
  76.     sf::Sprite sprite;
  77.  
  78.     // The following are the ints that hold the coordinates for each image. In this case, I think there are no better variable names than
  79.     // 'x', 'y', 'x2' and 'y2'. Just like in geometry :)
  80.  
  81.     int x;
  82.     int y;
  83.     int x2;
  84.     int y2;
  85.  
  86.     int speed;
  87. };
  88.  
  89.  
  90. //
  91. // In the following structs, each object is derived from the image struct. They are assigned a sprite, a position and a speed.
  92. //
  93.  
  94.  
  95. // The player's ship
  96.  
  97. struct PlayerShip: Image
  98. {
  99.  
  100.     PlayerShip()
  101.     {
  102.  
  103.         sprite.SetImage(data.player1);
  104.         sprite.SetPosition(350,475);    // I don't know if using plain numbers here is really that bad. It's just a position.
  105.         x = 350;
  106.         y = 475;
  107.         speed = 6;
  108.     }
  109.  
  110. }player;
  111.  
  112.  
  113. // The enemy's ship
  114.  
  115. struct Nme: Image
  116. {
  117.  
  118.     Nme()
  119.     {
  120.         sprite.SetImage(data.Nme1);
  121.         sprite.SetPosition(SPAWN_POS1,0);
  122.         x = 350;
  123.         y = 0;
  124.         speed = 2;
  125.     }
  126.  
  127. }enemy;
  128.  
  129.  
  130. // The "bullets" fired by the player
  131.  
  132. struct BulletStruct: Image
  133. {
  134.  
  135.     BulletStruct()
  136.     {
  137.         speed = 10;
  138.         sprite.SetImage(data.bullet1);
  139.         sprite.SetPosition( (player.x + 15),(player.y) );   // Positioned at the center of the player's ship.
  140.         y = player.y;
  141.     }
  142.  
  143. }*bullet[3]; /* Maybe there was no need for bullets to be pointers, but I wanted to practice using them. The reason why there are only 3 bullets is                 personal preference. Remember the NES Mega Man games? You could only fire 3 shots at a time. */
  144.  
  145.  
  146. //
  147. // Main Function
  148. //
  149.  
  150. int main()
  151. {
  152.     window.SetFramerateLimit(60);
  153.     Clock.Reset();
  154.  
  155.     while (window.IsOpened())
  156.     {
  157.         EndProgram();
  158.         window.Clear();
  159.         window.Draw(player.sprite);
  160.         Shoot();
  161.         Move();
  162.         MoveNme();
  163.  
  164.         // Because the number of bullets on the screen is variable, the functions related to them must be controlled.
  165.  
  166.         for (int i = 0; i < 3; i++)
  167.         {
  168.             if (bulletExists[i]) MoveBullet(i);
  169.             if (bulletExists[i]) window.Draw(bullet[i]->sprite);
  170.             if (!bulletExists[i]) bulletClock[i].Reset();
  171.         }
  172.  
  173.         window.Display();
  174.     }
  175.     return EXIT_SUCCESS;
  176. }
  177.  
  178.  
  179. // Exit Function
  180.  
  181. void EndProgram()
  182. {
  183.     while(window.GetEvent(event))
  184.     {
  185.         if (event.Type == sf::Event::Closed)
  186.         window.Close();
  187.     }
  188. }
  189.  
  190.  
  191. // Moving the player
  192.  
  193. void Move()
  194. {
  195.  
  196.     // The player moves the ship by pressing the left or right arrow keys. It stops when it reaches the predefined borders of the screen.
  197.  
  198.     if ( window.GetInput().IsKeyDown(sf::Key::Left) && (player.x > LEFT_BORDER) ) {player.sprite.Move(-player.speed,0); player.x -= player.speed;}
  199.     if ( window.GetInput().IsKeyDown(sf::Key::Right) && (player.x < RIGHT_BORDER) ) {player.sprite.Move(player.speed,0); player.x += player.speed;}
  200.  
  201.     // The following code controls the animation of the sprite. Every tenth of a second, the sprite will change. I haven't figured out a way to
  202.     // simplify this process. (I know of the SetSubRect function of SFML, but I didn't feel like making a "sprite map" kind of image)
  203.  
  204.     if ((Clock.GetElapsedTime() > 0.0) && (Clock.GetElapsedTime() < 0.1)) player.sprite.SetImage(data.player1);
  205.     if ((Clock.GetElapsedTime() > 0.1) && (Clock.GetElapsedTime() < 0.2)) player.sprite.SetImage(data.player2);
  206.     if (Clock.GetElapsedTime() > 0.2) Clock.Reset();
  207. }
  208.  
  209.  
  210. // Shooting
  211.  
  212. /*
  213.     The player shoots by pressing the Space key.
  214.  
  215.     The shooting function needs to check for three main states. First, which bullet are we going to fire? That's the reason behind the for loop. Second,        does this bullet already exists? That's the reason behind the (!bulletExists[i]). And third, has the last bullet moved up enough for the next shot to       be fired (has moved beyond the 370 'y' value.
  216. */
  217.  
  218. void Shoot()
  219. {
  220.     for (int i = 0; i < 3; i++)
  221.     {
  222.         if ( (i != 0) && (!bulletExists[i]) && (bulletExists[i-1]) )
  223.         {
  224.             if ( (window.GetInput().IsKeyDown(sf::Key::Space)) && (bullet[i-1]->y < 370) )
  225.             {
  226.                 bullet[i] = new BulletStruct;
  227.                 bulletExists[i] = true;
  228.             }
  229.         }
  230.         else if ( (i == 0) && (window.GetInput().IsKeyDown(sf::Key::Space)) && (bulletExists[i] == false) )
  231.         {
  232.             bullet[i] = new BulletStruct;
  233.             bulletExists[i] = true;
  234.         }
  235.     }
  236. }
  237.  
  238.  
  239. // Moving the bullet sprite
  240.  
  241. void MoveBullet(int i)
  242. {
  243.     bullet[i]->sprite.Move(0,-bullet[i]->speed);
  244.     bullet[i]->y -= bullet[i]->speed;
  245.  
  246.     if ((bulletClock[i].GetElapsedTime() > 0.0) && (bulletClock[i].GetElapsedTime() < 0.1)) bullet[i]->sprite.SetImage(data.bullet1);
  247.     if ((bulletClock[i].GetElapsedTime() > 0.1) && (bulletClock[i].GetElapsedTime() < 0.2)) bullet[i]->sprite.SetImage(data.bullet2);
  248.     if (bulletClock[i].GetElapsedTime() > 0.2) bulletClock[i].Reset();
  249.    
  250.     window.Draw(bullet[i]->sprite);
  251.  
  252.     if (bullet[i]->y < 0) {delete bullet[i]; bulletExists[i] = false;}
  253. }
  254.  
  255.  
  256. // Moving the enemy sprite
  257.  
  258. void MoveNme()
  259. {
  260.     enemy.sprite.Move(0,enemy.speed);
  261.     enemy.y += enemy.speed;
  262.  
  263.     if (enemy.y > BOTTOM_BORDER) {enemy.sprite.SetPosition(SPAWN_POS1,TOP_BORDER); enemy.y = 0;}
  264.  
  265.     if ((Clock.GetElapsedTime() > 0.0) && (Clock.GetElapsedTime() < 0.1)) enemy.sprite.SetImage(data.Nme1);
  266.     if ((Clock.GetElapsedTime() > 0.1) && (Clock.GetElapsedTime() < 0.2)) enemy.sprite.SetImage(data.Nme2);
  267.     window.Draw(enemy.sprite);
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement