Fix score
authorwatkinsr <ryanwatkins54@gmail.com>
Thu, 18 Jun 2020 14:10:14 +0000 (15:10 +0100)
committerwatkinsr <ryanwatkins54@gmail.com>
Thu, 18 Jun 2020 14:10:14 +0000 (15:10 +0100)
game.cpp

index ec72d6ff5d4b61b9c21ed60ed6f525828125a5b8..e41c8ac3ec5432c2696043ac9e9cde815fdbc35f 100755 (executable)
--- a/game.cpp
+++ b/game.cpp
@@ -370,8 +370,23 @@ void Dot::move( SDL_Rect& paddle1)
   //If the dot collided or went too far to the left or right
   if( ( mPosX - mCollider.r < 0 ) || ( mPosX + mCollider.r > SCREEN_WIDTH ) )
   {
-    printf("Score\n"); 
-    reset();
+    printf("Score\n");
+    if (mPosX - mCollider.r < 0) { // left side
+      player2Score++;
+    }
+    else {
+      player1Score++;
+    }
+
+    // Render text
+    SDL_Color textColor = { 0xFF, 0xFF, 0xFF };
+    if( !tPlayerOneScore.loadFromRenderedText( std::to_string(player1Score), textColor ) || 
+               !tPlayerTwoScore.loadFromRenderedText( std::to_string(player2Score), textColor ))
+    {
+      printf( "Failed to render text texture!\n" );
+    }
+
+    reset(); // spawn new ball
   }
 
   //If the dot collided or went too far up or down
@@ -702,6 +717,7 @@ int main( int argc, char* args[] )
 
                                // Try to render score text
                                tPlayerOneScore.render(SCREEN_WIDTH/4, 40);
+                               tPlayerTwoScore.render(SCREEN_WIDTH - SCREEN_WIDTH/4, 40);
 
                                //Render middle line
                                SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );