pong: start ball at side
Stefan Schuermans

Stefan Schuermans commited on 2019-06-16 13:39:19
Showing 1 changed files, with 14 additions and 4 deletions.

... ...
@@ -243,6 +243,7 @@ void Pong::reinitialize()
243 243
   m_scoreRight = 0;
244 244
 
245 245
   // start first ball
246
+  m_ballDirX = 0; // start at random side
246 247
   startBall();
247 248
 }
248 249
 
... ...
@@ -602,13 +603,22 @@ void Pong::planTimeCall()
602 603
 /// start ball
603 604
 void Pong::startBall()
604 605
 {
605
-  // ball starts horizontally at middle of field, vertically random
606
-  m_ballPosX = (m_width - (rand() & 1)) / 2;
607
-  m_ballPosY = rand() % m_height;
608
-  // random diagonal direction
606
+  // horizontal start direction opposite as before goal or random
607
+  if (m_ballDirX > 0) {
608
+    m_ballDirX = -1;
609
+  } else if (m_ballDirX < 0) {
610
+    m_ballDirX = 1;
611
+  } else {
609 612
     m_ballDirX = (rand() & 1) * 2 - 1;
613
+  }
614
+  // random vertical start direction
610 615
   m_ballDirY = (rand() & 1) * 2 - 1;
611 616
 
617
+  // horizontal start postion at side of field (depending on direction)
618
+  m_ballPosX = m_ballDirX > 0 ? 0 : m_width - 1;
619
+  // random vertical start position
620
+  m_ballPosY = rand() % m_height;
621
+
612 622
   // no delays, ball has not bounced at pad
613 623
   m_leftDelay = 0;
614 624
   m_rightDelay = 0;
615 625