Guest User

SFML Space Shooter

a guest
Feb 12th, 2011
3,668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 KB | None | 0 0
  1. //////
  2. //
  3. // nave_v2.cpp:
  4. //
  5. //////
  6.  
  7. #include <iostream>
  8. #include "SFML/Graphics.hpp"
  9. #include "nave_v2.h"
  10.  
  11. int main()
  12. {
  13.  
  14.     app.window.Create(sf::VideoMode(800,600,32),"Window");
  15.     app.window.SetFramerateLimit(60);
  16.  
  17.  
  18.  
  19.     // Forward declaration of functions
  20.  
  21.     void ProcessInput();
  22.     void EndProgram();
  23.     void DrawBullet(int numOfBullets);
  24.     void MoveBullet(int numOfBullets);
  25.  
  26.  
  27.  
  28.     // Main game loop
  29.  
  30.     while (app.window.IsOpened())
  31.     {
  32.         app.window.Clear();         //Clear the screen
  33.  
  34.         app.window.Draw(player.sprite);     //Draw the player
  35.  
  36.         DrawBullet(app.totalBullets);       //Draw the existing bullets
  37.  
  38.         app.window.Display();           //Display the sprites
  39.  
  40.         ProcessInput();             //Process the user's input
  41.  
  42.         MoveBullet(app.totalBullets);       //Move the existing bullets
  43.  
  44.         if (player.canShoot == false) app.CheckBulletClock(); //The player cannot shoot at any given time. There should
  45.                                     // a "recharging" time between shots. This function checks
  46.                                     // if that time has elapsed.
  47.  
  48.         EndProgram();       //Check if the user wishes to exit (it's separated from ProcessInput
  49.                     //because it uses sf::event, instead of sf::RenderWindow::GetInput() ).
  50.     }
  51.  
  52.     return 0;
  53. }
  54.  
  55.  
  56. /////  Functions
  57.  
  58.  
  59.  
  60. /* ProcessInput receives real-time input from the user through the sf::RenderWindow::GetInput() function.
  61.  
  62. The player can move left or right with the arrow keys, and shoot with the space bar. When shooting, the
  63. program must determine if the bullet to be shot doesn't already exist.
  64.  
  65. */
  66.  
  67. void ProcessInput()
  68. {
  69.     if (app.window.GetInput().IsKeyDown(sf::Key::Left)) player.Move(-6,0);
  70.     if (app.window.GetInput().IsKeyDown(sf::Key::Right)) player.Move(6,0);
  71.     if (app.window.GetInput().IsKeyDown(sf::Key::Space))
  72.     {
  73.         if (!bullet[bData.nextBullet].exists) bullet[bData.nextBullet].Shoot();
  74.     }
  75. }
  76.  
  77.  
  78.  
  79. //EndProgram awaits for the user to produce an event. If that event is clicking on the X button
  80. //or pressing alt-f4 (sf::Event::Closed) the window closes.
  81.  
  82. void EndProgram()
  83. {
  84.     while(app.window.GetEvent(app.event))
  85.     {
  86.         if (app.event.Type == sf::Event::Closed)
  87.         app.window.Close();
  88.     }
  89. }
  90.  
  91.  
  92.  
  93. //DrawBullet simply draws all existing bullet in the screen. It receives the total number of
  94. //bullets and cycles through them to check if they exist at the time.
  95.  
  96. void DrawBullet(int numOfBullets)
  97. {
  98.     for (int i = 0; i < numOfBullets; i++)
  99.     {
  100.         if (bullet[i].exists) app.window.Draw(bullet[i].sprite);
  101.     }
  102. }
  103.  
  104.  
  105.  
  106. //Same concept as before, but this functions moves the bullets up the screen.
  107.  
  108. void MoveBullet(int numOfBullets)
  109. {
  110.     for (int i = 0; i < numOfBullets; i++)
  111.     {
  112.         if (bullet[i].exists) bullet[i].Move(0,-10);
  113.     }
  114. }
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. ////////
  125. //
  126. // nave_v2.h
  127. //
  128. ////////
  129.  
  130. #ifndef CLASSES
  131. #define CLASSES
  132.  
  133.  
  134. // The Image class contains all the basic data an image should have.
  135.  
  136. class Image
  137. {
  138.  
  139.     public:
  140.  
  141.     sf::Image image1;   //An image file to load from
  142.     sf::Sprite sprite;  //A sprite to draw on screen
  143.  
  144.     int x;  //Coordinates that can be used to check position and collisions
  145.     int y;
  146.     int x2;
  147.     int y2;
  148.  
  149.     int speed;  //An int that determines how fast sprites should move
  150.  
  151.     void Move(float x, float y) // A simple, generic Move() function.
  152.                                 // With this function you can type
  153.                                 // Object.Move(x,y), instead of
  154.                                 // Object.sprite.Move(x,y).
  155.     {
  156.         sprite.Move(x,y);
  157.     }
  158. };
  159.  
  160.  
  161.  
  162. // PlayerShip class derives from Image and adds the canShoot variable.
  163.  
  164. class PlayerShip: public Image
  165. {
  166.  
  167.     public:
  168.  
  169.     bool canShoot;  //this variable helps control the rate at which the player shoots.
  170.                     //It becomes true when a certain time after the last shot has elapsed.
  171.  
  172.     PlayerShip()
  173.     {
  174.         image1.LoadFromFile("nave.png");
  175.         sprite.SetImage(image1);
  176.         sprite.SetPosition(350,475);
  177.         x = 350;
  178.         y = 475;
  179.         speed = 6;
  180.         canShoot = true;    //At the beginning of the game the player is ready to shoot.
  181.     }
  182.  
  183.     void Move(float a, float b)
  184.     {
  185.         sprite.Move(a,b);
  186.         x += a;
  187.         y += b;
  188.     }
  189.  
  190. }player;
  191.  
  192.  
  193.  
  194. // This class contains data that concerns all bullets in the game.
  195.  
  196. class BulletData
  197. {
  198.     public:
  199.  
  200.         int nextBullet; // Holds the number for the next available bullet.
  201.        
  202.         BulletData()
  203.         {
  204.             nextBullet = 0;
  205.         }
  206.  
  207.         void Next(int totalBullets) //This function selects the next available bullet.
  208.         {
  209.             if (nextBullet < totalBullets) nextBullet += 1;
  210.             if (nextBullet == totalBullets) nextBullet = 0; // If the total has been
  211.                                     // reached, go back to the
  212.                                     // first bullet.
  213.         }
  214. }bData;
  215.  
  216.  
  217.  
  218. // The main application class. It handles all "global" variables and functions.
  219.  
  220. class App
  221. {
  222.     public:
  223.  
  224.         sf::RenderWindow window;
  225.         sf::Event event;
  226.         sf::Clock bulletClock;
  227.        
  228.         int totalBullets;
  229.  
  230.         App()
  231.         {
  232.             totalBullets = 7;
  233.         }
  234.  
  235.        
  236.  
  237.         // The following function checks if 0.1 seconds have passed since the last
  238.         // shot was fired.
  239.  
  240.         void CheckBulletClock()
  241.         {
  242.             if (bulletClock.GetElapsedTime() > 0.1)
  243.             {
  244.                 bData.Next(totalBullets);
  245.                 player.canShoot = true;
  246.                 bulletClock.Reset();
  247.             }
  248.         }
  249.  
  250. }app;
  251.  
  252.  
  253.  
  254. class Bullet: public Image
  255. {
  256.  
  257.     public:
  258.  
  259.         bool exists;
  260.  
  261.         Bullet()
  262.         {
  263.             image1.LoadFromFile("bala1.png");
  264.             sprite.SetImage(image1);
  265.             exists = false;
  266.         }
  267.        
  268.         void Shoot()
  269.         {
  270.             exists = true;
  271.             sprite.SetPosition((player.x + 20),(player.y + 5));
  272.             y = (player.y + 5);
  273.  
  274.             player.canShoot = false;
  275.  
  276.             app.bulletClock.Reset();
  277.         }
  278.  
  279.         void Move(float a, float b)
  280.         {
  281.             sprite.Move(a,b);
  282.             y += b;
  283.  
  284.             if (y < (player.y - 480)) //This part of the function destroys the bullet when
  285.                         // it reaches a certain point.
  286.             {
  287.                 exists = false;
  288.             }
  289.         }
  290.  
  291. }bullet[7];
  292.  
  293. #endif
Advertisement
Add Comment
Please, Sign In to add comment