Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include <SFML/Audio.hpp>
- #include <cmath>
- #include <ctime>
- #include <cstdlib>
- #include <vector>
- #include <iostream>
- #define M_PI 3.14159265358979323846
- const int width = 1280, height = 960;
- class Ship
- {
- private:
- float spd = 600.f;
- public:
- sf::RectangleShape shipRect;
- sf::Texture shipTexture;
- bool thrusting = false;
- Ship()
- {
- shipRect.setSize(sf::Vector2f(40.f, 53.f));
- shipTexture.loadFromFile("./images/ship.png");
- shipRect.setTexture(&shipTexture);
- }
- void setPos(int x, int y)
- {
- shipRect.setPosition(x, y);
- }
- float getSpd()
- {
- return spd;
- }
- };
- class Bullet
- {
- public:
- float x, y, angle;
- const float spd = 400.f;
- bool life;
- sf::RectangleShape bulletRect;
- sf::Texture bulletTexture;
- Bullet(float x, float y, float angle)
- {
- this->x = x;
- this->y = y;
- this->angle = angle;
- life = true;
- bulletRect.setSize(sf::Vector2f(16.f, 16.f));
- bulletRect.setOrigin(0.f, 0.f);
- bulletRect.setPosition(x, y);
- bulletTexture.loadFromFile("./images/bullet.png");
- bulletRect.setTexture(&bulletTexture);
- }
- void shoot(float time)
- {
- float dx = cos(angle * M_PI / 180) * spd * time;
- float dy = sin(angle * M_PI / 180) * spd * time;
- x += dx;
- y += dy;
- bulletRect.setPosition(x, y);
- if (x > width || x < 0 || y > height || y < 0)
- life = false;
- }
- };
- class Asteroid
- {
- public:
- sf::RectangleShape asteroidRect;
- sf::Texture bigAsteroidTexture;
- Asteroid()
- {
- asteroidRect.setSize(sf::Vector2f(160.f, 122.f));
- bigAsteroidTexture.loadFromFile("./images/bigAsteroid.png");
- asteroidRect.setTexture(&bigAsteroidTexture);
- }
- void floatAround(float spd)
- {
- asteroidRect.move(spd, spd);
- }
- };
- void borders(sf::RectangleShape&);
- bool rectCollision(float x, float y, float w, float h, float x2, float y2, float w2, float h2);
- int main()
- {
- sf::RenderWindow window(sf::VideoMode(width, height), "Asteroids");
- sf::Texture bgTexture;
- if (!bgTexture.loadFromFile("./spacebg.jpg"))
- return -1;
- sf::Sprite bg;
- bg.setTexture(bgTexture);
- Ship ship;
- ship.setPos(-100, 100);
- ship.shipRect.setOrigin(30.f, 30.f);
- Asteroid ba1, ba2, ba3, ba4;
- std::vector<Asteroid> asts{ba1, ba2, ba3, ba4};
- // Asteroid asts[4] = { ba1, ba2, ba3, ba4 };
- int astsLength = asts.size();
- srand(time(NULL));
- for (int i = 0; i < astsLength; i++)
- asts[i].asteroidRect.setPosition(rand() % 1000, rand() % 900);
- sf::SoundBuffer fireBuffer;
- if (!fireBuffer.loadFromFile("./sounds/fire.wav"))
- return -1;
- sf::Sound fireSound;
- fireSound.setBuffer(fireBuffer);
- sf::SoundBuffer bigExplosionBuffer;
- if (!bigExplosionBuffer.loadFromFile("./sounds/bigExplosion.wav"))
- return -1;
- sf::Sound bigExplosionSound;
- bigExplosionSound.setBuffer(bigExplosionBuffer);
- std::vector<Bullet*> bullets;
- sf::Clock clck;
- bool held;
- bool notDead;
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- window.close();
- }
- float elapsed = clck.restart().asSeconds();
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- ship.shipRect.rotate(ship.getSpd() * elapsed * 0.8f);
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- ship.shipRect.rotate(-ship.getSpd() * elapsed * 0.8f);
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- ship.thrusting = true;
- else if (!sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- ship.thrusting = false;
- float rotation = ship.shipRect.getRotation();
- float x = sin(rotation * M_PI / 180) * ship.getSpd() * elapsed;
- float y = cos(rotation * M_PI / 180) * ship.getSpd() * elapsed;
- if (ship.thrusting)
- y = -y;
- else
- {
- x *= 0.05;
- y *= -0.05;
- }
- ship.shipRect.move(x, y);
- borders(ship.shipRect);
- window.clear();
- window.draw(bg);
- for (int i = 0; i < astsLength; i++)
- {
- bool shipCollideWithAsteroid = rectCollision(ship.shipRect.getPosition().x,
- ship.shipRect.getPosition().y, ship.shipRect.getSize().x, ship.shipRect.getSize().y,
- asts[i].asteroidRect.getPosition().x, asts[i].asteroidRect.getPosition().y,
- asts[i].asteroidRect.getSize().x, asts[i].asteroidRect.getSize().y);
- if (shipCollideWithAsteroid)
- bigExplosionSound.play();
- asts[i].floatAround(40.f * elapsed);
- borders(asts[i].asteroidRect);
- window.draw(asts[i].asteroidRect);
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
- {
- if (!held)
- {
- Bullet* b = new Bullet(ship.shipRect.getPosition().x, ship.shipRect.getPosition().y,
- ship.shipRect.getRotation() - 90.f);
- bullets.push_back(b);
- fireSound.play();
- held = true;
- }
- }
- else held = false;
- for (auto i = bullets.begin(); i != bullets.end();)
- {
- Bullet* b = *i;
- for (int i = 0; i < astsLength; i++)
- {
- bool bulletCollideWithAsteroid = rectCollision(b->x, b->y, b->bulletRect.getSize().x, b->bulletRect.getSize().y,
- asts[i].asteroidRect.getPosition().x, asts[i].asteroidRect.getPosition().y,
- asts[i].asteroidRect.getSize().x, asts[i].asteroidRect.getSize().y);
- if (bulletCollideWithAsteroid)
- {
- asts.erase(asts.begin() + i);
- astsLength--;
- bigExplosionSound.play();
- b->life = false;
- }
- b->shoot(elapsed);
- window.draw(b->bulletRect);
- }
- if (!b->life)
- {
- i = bullets.erase(i);
- delete b;
- }
- else i++;
- }
- window.draw(ship.shipRect);
- window.display();
- }
- return 0;
- }
- void borders(sf::RectangleShape& shape)
- {
- sf::Vector2f pos = shape.getPosition();
- float w = shape.getSize().x;
- float h = shape.getSize().y;
- if (pos.y < 0)
- shape.setPosition(pos.x, height - h);
- else if (pos.y > height - h)
- shape.setPosition(pos.x, 0);
- else if (pos.x < 0)
- shape.setPosition(width - w, pos.y);
- else if (pos.x > width - w)
- shape.setPosition(0, pos.y);
- }
- bool rectCollision(float x, float y, float w, float h, float x2, float y2, float w2, float h2)
- {
- return x < x2 + w2 && x + w > x2 && y < y2 + h2 && h + y > y2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement