mirror of
https://github.com/SFML/SFML.git
synced 2024-11-28 22:31:09 +08:00
Use <random>
header
This commit is contained in:
parent
65fef85b30
commit
757cb36d30
@ -4,9 +4,15 @@
|
||||
////////////////////////////////////////////////////////////
|
||||
#include "Effect.hpp"
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <random>
|
||||
|
||||
|
||||
namespace
|
||||
{
|
||||
std::random_device rd;
|
||||
std::mt19937 rng(rd());
|
||||
}
|
||||
|
||||
const sf::Font* Effect::s_font = nullptr;
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
@ -135,15 +141,19 @@ public:
|
||||
|
||||
bool onLoad() override
|
||||
{
|
||||
std::uniform_real_distribution<float> x_distribution(0, 800);
|
||||
std::uniform_real_distribution<float> y_distribution(0, 600);
|
||||
std::uniform_int_distribution<sf::Uint16> color_distribution(0, 255);
|
||||
|
||||
// Create the points
|
||||
m_points.setPrimitiveType(sf::Points);
|
||||
for (int i = 0; i < 40000; ++i)
|
||||
{
|
||||
auto x = static_cast<float>(std::rand() % 800);
|
||||
auto y = static_cast<float>(std::rand() % 600);
|
||||
auto r = static_cast<sf::Uint8>(std::rand() % 255);
|
||||
auto g = static_cast<sf::Uint8>(std::rand() % 255);
|
||||
auto b = static_cast<sf::Uint8>(std::rand() % 255);
|
||||
auto x = x_distribution(rng);
|
||||
auto y = y_distribution(rng);
|
||||
auto r = static_cast<sf::Uint8>(color_distribution(rng));
|
||||
auto g = static_cast<sf::Uint8>(color_distribution(rng));
|
||||
auto b = static_cast<sf::Uint8>(color_distribution(rng));
|
||||
m_points.append(sf::Vertex(sf::Vector2f(x, y), sf::Color(r, g, b)));
|
||||
}
|
||||
|
||||
|
@ -4,9 +4,7 @@
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <SFML/Audio.hpp>
|
||||
#include <cmath>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <random>
|
||||
|
||||
#ifdef SFML_SYSTEM_IOS
|
||||
#include <SFML/Main.hpp>
|
||||
@ -29,7 +27,8 @@ std::filesystem::path resourcesDir()
|
||||
////////////////////////////////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
std::srand(static_cast<unsigned int>(std::time(nullptr)));
|
||||
std::random_device rd;
|
||||
std::mt19937 rng(rd());
|
||||
|
||||
// Define some constants
|
||||
const float gameWidth = 800;
|
||||
@ -140,7 +139,7 @@ int main()
|
||||
do
|
||||
{
|
||||
// Make sure the ball initial angle is not too much vertical
|
||||
ballAngle = sf::degrees(static_cast<float>(std::rand() % 360));
|
||||
ballAngle = sf::degrees(std::uniform_real_distribution<float>(0, 360)(rng));
|
||||
}
|
||||
while (std::abs(std::cos(ballAngle.asRadians())) < 0.7f);
|
||||
}
|
||||
@ -231,6 +230,8 @@ int main()
|
||||
ball.setPosition({ball.getPosition().x, gameHeight - ballRadius - 0.1f});
|
||||
}
|
||||
|
||||
std::uniform_real_distribution<float> dist(0, 20);
|
||||
|
||||
// Check the collisions between the ball and the paddles
|
||||
// Left Paddle
|
||||
if (ball.getPosition().x - ballRadius < leftPaddle.getPosition().x + paddleSize.x / 2 &&
|
||||
@ -239,9 +240,9 @@ int main()
|
||||
ball.getPosition().y - ballRadius <= leftPaddle.getPosition().y + paddleSize.y / 2)
|
||||
{
|
||||
if (ball.getPosition().y > leftPaddle.getPosition().y)
|
||||
ballAngle = sf::degrees(180) - ballAngle + sf::degrees(static_cast<float>(std::rand() % 20));
|
||||
ballAngle = sf::degrees(180) - ballAngle + sf::degrees(dist(rng));
|
||||
else
|
||||
ballAngle = sf::degrees(180) - ballAngle - sf::degrees(static_cast<float>(std::rand() % 20));
|
||||
ballAngle = sf::degrees(180) - ballAngle - sf::degrees(dist(rng));
|
||||
|
||||
ballSound.play();
|
||||
ball.setPosition({leftPaddle.getPosition().x + ballRadius + paddleSize.x / 2 + 0.1f, ball.getPosition().y});
|
||||
@ -254,9 +255,9 @@ int main()
|
||||
ball.getPosition().y - ballRadius <= rightPaddle.getPosition().y + paddleSize.y / 2)
|
||||
{
|
||||
if (ball.getPosition().y > rightPaddle.getPosition().y)
|
||||
ballAngle = sf::degrees(180) - ballAngle + sf::degrees(static_cast<float>(std::rand() % 20));
|
||||
ballAngle = sf::degrees(180) - ballAngle + sf::degrees(dist(rng));
|
||||
else
|
||||
ballAngle = sf::degrees(180) - ballAngle - sf::degrees(static_cast<float>(std::rand() % 20));
|
||||
ballAngle = sf::degrees(180) - ballAngle - sf::degrees(dist(rng));
|
||||
|
||||
ballSound.play();
|
||||
ball.setPosition({rightPaddle.getPosition().x - ballRadius - paddleSize.x / 2 - 0.1f, ball.getPosition().y});
|
||||
|
Loading…
Reference in New Issue
Block a user