/
Kotopeska
/
RiseUpGame
Обзор
Документация
Войти
/
Kotopeska
/
RiseUpGame
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
model/Background.cpp
65 строк
2 KB
Kotopeska
init
10 май 2025, 17:31
10 май 2025, 17:31
dd8efb4
Код
Авторство
О чём код?
#include "Background.h" #include <cstdlib> #include <ctime> namespace Model { Background::Background(int width, int height) : GameObject(0, 0, width, height, "Background"), m_scrollSpeed(0.5f) { std::srand(std::time(nullptr)); const int numStars = 100; for (int i = 0; i < numStars; i++) { Star star; star.x = static_cast<float>(std::rand() % width); star.y = static_cast<float>(std::rand() % height); star.size = 1.0f + static_cast<float>(std::rand() % 3); star.alpha = 0.1f + static_cast<float>(std::rand() % 10) / 10.0f; star.alphaChange = 0.002f + static_cast<float>(std::rand() % 5) / 1000.0f; if (std::rand() % 2 == 0) { star.alphaChange = -star.alphaChange; } m_stars.push_back(star); } } Background::~Background() { } void Background::update() { for (auto &star : m_stars) { star.alpha += star.alphaChange; if (star.alpha < 0.1f || star.alpha > 1.0f) { star.alphaChange = -star.alphaChange; } star.y += m_scrollSpeed; if (star.y > getHeight()) { star.y = 0; star.x = static_cast<float>(std::rand() % static_cast<int>(getWidth())); } } } void Background::reset() { for (auto &star : m_stars) { star.x = static_cast<float>(std::rand() % static_cast<int>(getWidth())); star.y = static_cast<float>(std::rand() % static_cast<int>(getHeight())); } } } // namespace Model