Różne meteoryty

This commit is contained in:
2024-11-22 09:38:13 +01:00
parent 94a6d081a1
commit 0c4cdf35b1
4 changed files with 14 additions and 12 deletions

View File

@@ -20,8 +20,8 @@ public:
private:
std::vector<Meteor> meteors;
Size size;
sf::Texture meteorTexture;
RandomNumberGenerator random;
sf::Texture meteorTexture1;
sf::Texture meteorTexture2;
sf::Clock spawnClock;
};

View File

@@ -4,15 +4,11 @@
#include <random>
class RandomNumberGenerator {
private:
std::random_device rd;
std::mt19937 gen;
std::uniform_int_distribution<> dis;
public:
RandomNumberGenerator() : gen(rd()), dis(0, 499) {}
int getRandomNumber() {
static int getRandomNumber(int min, int max) {
static std::random_device rd;
static std::mt19937 gen(rd());
static std::uniform_int_distribution<> dis(min, max);
return dis(gen);
}
};

View File

@@ -23,6 +23,7 @@ int main()
ship.setFirerate(200);
while (window.isOpen()) {
// std::cout << "Liczba: " << RandomNumberGenerator::getRandomNumber(0,499) << std::endl;
window.clear();
window.draw(backgroundSprite); // narysuj tło

View File

@@ -5,14 +5,19 @@
Plansza::Plansza(unsigned int windowHeight, unsigned int windowWidth) {
size.height = windowHeight;
size.width = windowWidth;
meteorTexture.loadFromFile("../assets/img/meteor-1.png");
meteorTexture1.loadFromFile("../assets/img/meteor-1.png");
meteorTexture2.loadFromFile("../assets/img/meteor-2.png");
spawnClock.restart();
}
void Plansza::spawn_meteor() {
if (spawnClock.getElapsedTime().asSeconds() > rand() % 10 + 1) { // randomowy spawn meteorytów od 10 do 1 sekundy
if (meteors.size() < 5) { // jeśli jest mniej niż 5 meteorów na planszy
meteors.emplace_back(random.getRandomNumber(), -100, meteorTexture);
if(rand() % 2 == 1) {
meteors.emplace_back(RandomNumberGenerator::getRandomNumber(0,499), -100, meteorTexture2);
} else {
meteors.emplace_back(RandomNumberGenerator::getRandomNumber(0,499), -100, meteorTexture1);
}
}
spawnClock.restart();
}