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