Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.27 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/Audio.hpp>
  3. #include <cmath>
  4. #include <ctime>
  5. #include <cstdlib>
  6. #include <vector>
  7. #include <iostream>
  8.  
  9. #define M_PI 3.14159265358979323846
  10.  
  11. const int width = 1280, height = 960;
  12.  
  13. class Ship
  14. {
  15. private:
  16.     float spd = 600.f;
  17. public:
  18.     sf::RectangleShape shipRect;
  19.     sf::Texture shipTexture;
  20.  
  21.     bool thrusting = false;
  22.  
  23.     Ship()
  24.     {
  25.         shipRect.setSize(sf::Vector2f(40.f, 53.f));
  26.         shipTexture.loadFromFile("./images/ship.png");
  27.         shipRect.setTexture(&shipTexture);
  28.     }
  29.  
  30.     void setPos(int x, int y)
  31.     {
  32.         shipRect.setPosition(x, y);
  33.     }
  34.  
  35.     float getSpd()
  36.     {
  37.         return spd;
  38.     }
  39. };
  40.  
  41. class Bullet
  42. {
  43. public:
  44.     float x, y, angle;
  45.     const float spd = 400.f;
  46.     bool life;
  47.  
  48.     sf::RectangleShape bulletRect;
  49.     sf::Texture bulletTexture;
  50.  
  51.     Bullet(float x, float y, float angle)
  52.     {
  53.         this->x = x;
  54.         this->y = y;
  55.         this->angle = angle;
  56.         life = true;
  57.         bulletRect.setSize(sf::Vector2f(16.f, 16.f));
  58.         bulletRect.setOrigin(0.f, 0.f);
  59.         bulletRect.setPosition(x, y);
  60.         bulletTexture.loadFromFile("./images/bullet.png");
  61.         bulletRect.setTexture(&bulletTexture);
  62.     }
  63.  
  64.     void shoot(float time)
  65.     {
  66.         float dx = cos(angle * M_PI / 180) * spd * time;
  67.         float dy = sin(angle * M_PI / 180) * spd * time;
  68.         x += dx;
  69.         y += dy;
  70.         bulletRect.setPosition(x, y);
  71.         if (x > width || x < 0 || y > height || y < 0)
  72.             life = false;
  73.     }
  74. };
  75.  
  76. class Asteroid
  77. {
  78. public:
  79.     sf::RectangleShape asteroidRect;
  80.     sf::Texture bigAsteroidTexture;
  81.  
  82.     Asteroid()
  83.     {
  84.         asteroidRect.setSize(sf::Vector2f(160.f, 122.f));
  85.         bigAsteroidTexture.loadFromFile("./images/bigAsteroid.png");
  86.         asteroidRect.setTexture(&bigAsteroidTexture);
  87.     }
  88.  
  89.     void floatAround(float spd)
  90.     {
  91.         asteroidRect.move(spd, spd);
  92.     }
  93. };
  94.  
  95. void borders(sf::RectangleShape&);
  96. bool rectCollision(float x, float y, float w, float h, float x2, float y2, float w2, float h2);
  97.  
  98. int main()
  99. {
  100.     sf::RenderWindow window(sf::VideoMode(width, height), "Asteroids");
  101.  
  102.     sf::Texture bgTexture;
  103.     if (!bgTexture.loadFromFile("./spacebg.jpg"))
  104.         return -1;
  105.     sf::Sprite bg;
  106.     bg.setTexture(bgTexture);
  107.  
  108.     Ship ship;
  109.     ship.setPos(-100, 100);
  110.     ship.shipRect.setOrigin(30.f, 30.f);
  111.  
  112.     Asteroid ba1, ba2, ba3, ba4;
  113.     std::vector<Asteroid> asts{ba1, ba2, ba3, ba4};
  114.     // Asteroid asts[4] = { ba1, ba2, ba3, ba4 };
  115.     int astsLength = asts.size();
  116.  
  117.     srand(time(NULL));
  118.     for (int i = 0; i < astsLength; i++)
  119.         asts[i].asteroidRect.setPosition(rand() % 1000, rand() % 900);
  120.  
  121.     sf::SoundBuffer fireBuffer;
  122.     if (!fireBuffer.loadFromFile("./sounds/fire.wav"))
  123.         return -1;
  124.     sf::Sound fireSound;
  125.     fireSound.setBuffer(fireBuffer);
  126.  
  127.     sf::SoundBuffer bigExplosionBuffer;
  128.     if (!bigExplosionBuffer.loadFromFile("./sounds/bigExplosion.wav"))
  129.         return -1;
  130.     sf::Sound bigExplosionSound;
  131.     bigExplosionSound.setBuffer(bigExplosionBuffer);
  132.  
  133.     std::vector<Bullet*> bullets;
  134.  
  135.     sf::Clock clck;
  136.  
  137.     bool held;
  138.     bool notDead;
  139.  
  140.     while (window.isOpen())
  141.     {
  142.         sf::Event event;
  143.         while (window.pollEvent(event))
  144.         {
  145.             if (event.type == sf::Event::Closed)
  146.                 window.close();
  147.         }
  148.  
  149.         float elapsed = clck.restart().asSeconds();
  150.  
  151.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  152.             ship.shipRect.rotate(ship.getSpd() * elapsed * 0.8f);
  153.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  154.             ship.shipRect.rotate(-ship.getSpd() * elapsed * 0.8f);
  155.  
  156.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  157.             ship.thrusting = true;
  158.         else if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  159.             ship.thrusting = false;
  160.  
  161.         float rotation = ship.shipRect.getRotation();
  162.         float x = sin(rotation * M_PI / 180) * ship.getSpd() * elapsed;
  163.         float y = cos(rotation * M_PI / 180) * ship.getSpd() * elapsed;
  164.  
  165.         if (ship.thrusting)
  166.             y = -y;
  167.         else
  168.         {
  169.             x *= 0.05;
  170.             y *= -0.05;
  171.         }
  172.         ship.shipRect.move(x, y);
  173.  
  174.         borders(ship.shipRect);
  175.  
  176.         window.clear();
  177.         window.draw(bg);
  178.  
  179.         for (int i = 0; i < astsLength; i++)
  180.         {
  181.             bool shipCollideWithAsteroid = rectCollision(ship.shipRect.getPosition().x,
  182.                                            ship.shipRect.getPosition().y, ship.shipRect.getSize().x, ship.shipRect.getSize().y,
  183.                                            asts[i].asteroidRect.getPosition().x, asts[i].asteroidRect.getPosition().y,
  184.                                            asts[i].asteroidRect.getSize().x, asts[i].asteroidRect.getSize().y);
  185.             if (shipCollideWithAsteroid)
  186.                 bigExplosionSound.play();
  187.  
  188.             asts[i].floatAround(40.f * elapsed);
  189.             borders(asts[i].asteroidRect);
  190.             window.draw(asts[i].asteroidRect);
  191.         }
  192.  
  193.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
  194.         {
  195.             if (!held)
  196.             {
  197.                 Bullet* b = new Bullet(ship.shipRect.getPosition().x, ship.shipRect.getPosition().y,
  198.                                        ship.shipRect.getRotation() - 90.f);
  199.                 bullets.push_back(b);
  200.                 fireSound.play();
  201.                 held = true;
  202.             }
  203.         }
  204.         else held = false;
  205.  
  206.         for (auto i = bullets.begin(); i != bullets.end();)
  207.         {
  208.             Bullet* b = *i;
  209.  
  210.             for (int i = 0; i < astsLength; i++)
  211.             {
  212.                 bool bulletCollideWithAsteroid = rectCollision(b->x, b->y, b->bulletRect.getSize().x, b->bulletRect.getSize().y,
  213.                                                  asts[i].asteroidRect.getPosition().x, asts[i].asteroidRect.getPosition().y,
  214.                                                  asts[i].asteroidRect.getSize().x, asts[i].asteroidRect.getSize().y);
  215.  
  216.                 if (bulletCollideWithAsteroid)
  217.                 {
  218.                     asts.erase(asts.begin() + i);
  219.                     astsLength--;
  220.                     bigExplosionSound.play();
  221.                     b->life = false;
  222.                 }
  223.                 b->shoot(elapsed);
  224.                 window.draw(b->bulletRect);
  225.             }
  226.  
  227.             if (!b->life)
  228.             {
  229.                 i = bullets.erase(i);
  230.                 delete b;
  231.             }
  232.             else i++;
  233.         }
  234.  
  235.  
  236.         window.draw(ship.shipRect);
  237.         window.display();
  238.     }
  239.  
  240.     return 0;
  241. }
  242.  
  243. void borders(sf::RectangleShape& shape)
  244. {
  245.     sf::Vector2f pos = shape.getPosition();
  246.     float w = shape.getSize().x;
  247.     float h = shape.getSize().y;
  248.  
  249.     if (pos.y < 0)
  250.         shape.setPosition(pos.x, height - h);
  251.     else if (pos.y > height - h)
  252.         shape.setPosition(pos.x, 0);
  253.     else if (pos.x < 0)
  254.         shape.setPosition(width - w, pos.y);
  255.     else if (pos.x > width - w)
  256.         shape.setPosition(0, pos.y);
  257. }
  258.  
  259. bool rectCollision(float x, float y, float w, float h, float x2, float y2, float w2, float h2)
  260. {
  261.     return x < x2 + w2 && x + w > x2 && y < y2 + h2 && h + y > y2;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement