From e3114667bcc61303e28e633094490bd47d67e442 Mon Sep 17 00:00:00 2001 From: Brendan Coles Date: Tue, 15 Dec 2020 11:59:20 +0000 Subject: [PATCH] Breakout: randomize ball start trajectory and velocity --- Games/Breakout/Game.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Games/Breakout/Game.cpp b/Games/Breakout/Game.cpp index e69b2dab6a..7ec8c3a975 100644 --- a/Games/Breakout/Game.cpp +++ b/Games/Breakout/Game.cpp @@ -168,9 +168,18 @@ void Game::mousemove_event(GUI::MouseEvent& event) void Game::reset_ball() { + int position_x_min = (game_width / 2) - 50; + int position_x_max = (game_width / 2) + 50; + int position_x = arc4random() % (position_x_max - position_x_min + 1) + position_x_min; + int position_y = 200; + int velocity_x = arc4random() % 3 + 1; + int velocity_y = 3 + (3 - velocity_x); + if (arc4random() % 2) + velocity_x = velocity_x * -1; + m_ball = {}; - m_ball.position = { 150, 200 }; - m_ball.velocity = { 3, 3 }; + m_ball.position = { position_x, position_y }; + m_ball.velocity = { velocity_x, velocity_y }; } void Game::hurt()