From 0c4cdf35b1ea46af5e6169e734494b9248faaea9 Mon Sep 17 00:00:00 2001 From: Andrii Solianyk Date: Fri, 22 Nov 2024 09:38:13 +0100 Subject: [PATCH] =?UTF-8?q?R=C3=B3=C5=BCne=20meteoryty?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- headers/Plansza.h | 4 ++-- headers/RandomNumberGenerator.h | 12 ++++-------- main.cpp | 1 + sources/Plansza.cpp | 9 +++++++-- 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/headers/Plansza.h b/headers/Plansza.h index c8e0915..3aae063 100644 --- a/headers/Plansza.h +++ b/headers/Plansza.h @@ -20,8 +20,8 @@ public: private: std::vector meteors; Size size; - sf::Texture meteorTexture; - RandomNumberGenerator random; + sf::Texture meteorTexture1; + sf::Texture meteorTexture2; sf::Clock spawnClock; }; diff --git a/headers/RandomNumberGenerator.h b/headers/RandomNumberGenerator.h index a88dbaa..49b3acb 100644 --- a/headers/RandomNumberGenerator.h +++ b/headers/RandomNumberGenerator.h @@ -4,15 +4,11 @@ #include 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); } }; diff --git a/main.cpp b/main.cpp index d4435b2..ecfc07b 100644 --- a/main.cpp +++ b/main.cpp @@ -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 diff --git a/sources/Plansza.cpp b/sources/Plansza.cpp index 3024c89..6057327 100644 --- a/sources/Plansza.cpp +++ b/sources/Plansza.cpp @@ -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(); }