From 680f27dcea26ca9ae7a2cd1742ea463484639437 Mon Sep 17 00:00:00 2001 From: Andrew Ebling Date: Mon, 19 Sep 2022 15:42:47 +0100 Subject: [PATCH 1/2] Solution Exercise 1.1 - add right paddle for 2 player game. --- .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++ Chapter01/Game.cpp | 101 ++++++++++++------ Chapter01/Game.h | 12 ++- 3 files changed, 85 insertions(+), 36 deletions(-) create mode 100644 Chapter01/Chapter01-mac.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/Chapter01/Chapter01-mac.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/Chapter01/Chapter01-mac.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 00000000..18d98100 --- /dev/null +++ b/Chapter01/Chapter01-mac.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + diff --git a/Chapter01/Game.cpp b/Chapter01/Game.cpp index 3b18199e..2f21662c 100644 --- a/Chapter01/Game.cpp +++ b/Chapter01/Game.cpp @@ -16,7 +16,8 @@ Game::Game() ,mRenderer(nullptr) ,mTicksCount(0) ,mIsRunning(true) -,mPaddleDir(0) +,mPaddleLeftDir(0) +,mPaddleRightDir(0) { } @@ -60,11 +61,15 @@ bool Game::Initialize() return false; } // - mPaddlePos.x = 10.0f; - mPaddlePos.y = 768.0f/2.0f; + mPaddleLeftPos.x = 10.0f; + mPaddleLeftPos.y = 768.0f/2.0f; + + mPaddleRightPos.x = 1024.0f - 10.0 - thickness; + mPaddleRightPos.y = mPaddleLeftPos.y; + mBallPos.x = 1024.0f/2.0f; mBallPos.y = 768.0f/2.0f; - mBallVel.x = -200.0f; + mBallVel.x = 200.0f; mBallVel.y = 235.0f; return true; } @@ -102,15 +107,26 @@ void Game::ProcessInput() } // Update paddle direction based on W/S keys - mPaddleDir = 0; + mPaddleLeftDir = 0; if (state[SDL_SCANCODE_W]) { - mPaddleDir -= 1; + mPaddleLeftDir -= 1; } if (state[SDL_SCANCODE_S]) { - mPaddleDir += 1; + mPaddleLeftDir += 1; } + + // Update paddle direction based on I/K keys + mPaddleRightDir = 0; + if (state[SDL_SCANCODE_I]) + { + mPaddleRightDir -= 1; + } + if (state[SDL_SCANCODE_K]) + { + mPaddleRightDir += 1; + } } void Game::UpdateGame() @@ -133,19 +149,32 @@ void Game::UpdateGame() mTicksCount = SDL_GetTicks(); // Update paddle position based on direction - if (mPaddleDir != 0) + if (mPaddleLeftDir != 0) { - mPaddlePos.y += mPaddleDir * 300.0f * deltaTime; + mPaddleLeftPos.y += mPaddleLeftDir * 300.0f * deltaTime; // Make sure paddle doesn't move off screen! - if (mPaddlePos.y < (paddleH/2.0f + thickness)) + if (mPaddleLeftPos.y < (paddleH/2.0f + thickness)) { - mPaddlePos.y = paddleH/2.0f + thickness; + mPaddleLeftPos.y = paddleH/2.0f + thickness; } - else if (mPaddlePos.y > (768.0f - paddleH/2.0f - thickness)) + else if (mPaddleLeftPos.y > (768.0f - paddleH/2.0f - thickness)) { - mPaddlePos.y = 768.0f - paddleH/2.0f - thickness; + mPaddleLeftPos.y = 768.0f - paddleH/2.0f - thickness; } } + + if (mPaddleRightDir != 0) + { + mPaddleRightPos.y += mPaddleRightDir * 300.0f * deltaTime; + if (mPaddleRightPos.y < (paddleH/2.0f + thickness)) + { + mPaddleRightPos.y = paddleH/2.0f + thickness; + } + else if (mPaddleRightPos.y > (768.0f - paddleH/2.0f - thickness)) + { + mPaddleRightPos.y = 768.0f - paddleH/2.0f - thickness; + } + } // Update ball position based on ball velocity mBallPos.x += mBallVel.x * deltaTime; @@ -153,12 +182,15 @@ void Game::UpdateGame() // Bounce if needed // Did we intersect with the paddle? - float diff = mPaddlePos.y - mBallPos.y; + float diffL = mPaddleLeftPos.y - mBallPos.y; + float diffR = mPaddleRightPos.y - mBallPos.y; + // Take absolute value of difference - diff = (diff > 0.0f) ? diff : -diff; + diffL = (diffL > 0.0f) ? diffL : -diffL; + diffR = (diffR > 0.0f) ? diffR : -diffR; if ( // Our y-difference is small enough - diff <= paddleH / 2.0f && + diffL <= paddleH / 2.0f && // We are in the correct x-position mBallPos.x <= 25.0f && mBallPos.x >= 20.0f && // The ball is moving to the left @@ -166,16 +198,18 @@ void Game::UpdateGame() { mBallVel.x *= -1.0f; } + else if ( + diffR <= paddleH / 2.0f && + mBallPos.x >= 1024.0f - 25.0f && mBallPos.x >= 1024.0f - 20.0f && + mBallVel.x > 0.0f) + { + mBallVel.x *= -1.0f; + } // Did the ball go off the screen? (if so, end game) - else if (mBallPos.x <= 0.0f) + else if (mBallPos.x <= 0.0f || mBallPos.x >= 1024.0f) { mIsRunning = false; } - // Did the ball collide with the right wall? - else if (mBallPos.x >= (1024.0f - thickness) && mBallVel.x > 0.0f) - { - mBallVel.x *= -1.0f; - } // Did the ball collide with the top wall? if (mBallPos.y <= thickness && mBallVel.y < 0.0f) @@ -220,21 +254,22 @@ void Game::GenerateOutput() wall.y = 768 - thickness; SDL_RenderFillRect(mRenderer, &wall); - // Draw right wall - wall.x = 1024 - thickness; - wall.y = 0; - wall.w = thickness; - wall.h = 1024; - SDL_RenderFillRect(mRenderer, &wall); - // Draw paddle - SDL_Rect paddle{ - static_cast(mPaddlePos.x), - static_cast(mPaddlePos.y - paddleH/2), + SDL_Rect paddleL{ + static_cast(mPaddleLeftPos.x), + static_cast(mPaddleLeftPos.y - paddleH/2), thickness, static_cast(paddleH) }; - SDL_RenderFillRect(mRenderer, &paddle); + SDL_RenderFillRect(mRenderer, &paddleL); + + SDL_Rect paddleR{ + static_cast(mPaddleRightPos.x), + static_cast(mPaddleRightPos.y - paddleH/2), + thickness, + static_cast(paddleH) + }; + SDL_RenderFillRect(mRenderer, &paddleR); // Draw ball SDL_Rect ball{ diff --git a/Chapter01/Game.h b/Chapter01/Game.h index 3d59a468..7710c861 100644 --- a/Chapter01/Game.h +++ b/Chapter01/Game.h @@ -44,12 +44,18 @@ class Game bool mIsRunning; // Pong specific - // Direction of paddle - int mPaddleDir; + // Direction of left player paddle + int mPaddleLeftDir; + // Position of paddle - Vector2 mPaddlePos; + Vector2 mPaddleLeftPos; // Position of ball Vector2 mBallPos; // Velocity of ball Vector2 mBallVel; + + + // Direction of right player paddle + int mPaddleRightDir; + Vector2 mPaddleRightPos; }; From 3e138b5ca1bf6511305ae1980fe903e999edd83d Mon Sep 17 00:00:00 2001 From: Andrew Ebling Date: Mon, 19 Sep 2022 17:39:20 +0100 Subject: [PATCH 2/2] Exercise 1.2 - support playing with multiple balls. --- Chapter01/Game.cpp | 130 +++++++++++++++++++++++++-------------------- Chapter01/Game.h | 15 +++--- 2 files changed, 82 insertions(+), 63 deletions(-) diff --git a/Chapter01/Game.cpp b/Chapter01/Game.cpp index 2f21662c..e9bad05f 100644 --- a/Chapter01/Game.cpp +++ b/Chapter01/Game.cpp @@ -10,6 +10,7 @@ const int thickness = 15; const float paddleH = 100.0f; +const int numBalls = 2; Game::Game() :mWindow(nullptr) @@ -67,10 +68,21 @@ bool Game::Initialize() mPaddleRightPos.x = 1024.0f - 10.0 - thickness; mPaddleRightPos.y = mPaddleLeftPos.y; - mBallPos.x = 1024.0f/2.0f; - mBallPos.y = 768.0f/2.0f; - mBallVel.x = 200.0f; - mBallVel.y = 235.0f; + mBalls = std::vector(numBalls); + + for(int i=0; i 0.0f) ? diffL : -diffL; - diffR = (diffR > 0.0f) ? diffR : -diffR; - if ( - // Our y-difference is small enough - diffL <= paddleH / 2.0f && - // We are in the correct x-position - mBallPos.x <= 25.0f && mBallPos.x >= 20.0f && - // The ball is moving to the left - mBallVel.x < 0.0f) - { - mBallVel.x *= -1.0f; - } - else if ( - diffR <= paddleH / 2.0f && - mBallPos.x >= 1024.0f - 25.0f && mBallPos.x >= 1024.0f - 20.0f && - mBallVel.x > 0.0f) - { - mBallVel.x *= -1.0f; + for(auto &ball : mBalls) { + // Update ball position based on ball velocity + ball.position.x += ball.velocity.x * deltaTime; + ball.position.y += ball.velocity.y * deltaTime; + + // Bounce if needed + // Did we intersect with the paddle? + float diffL = mPaddleLeftPos.y - ball.position.y; + float diffR = mPaddleRightPos.y - ball.position.y; + + // Take absolute value of difference + diffL = (diffL > 0.0f) ? diffL : -diffL; + diffR = (diffR > 0.0f) ? diffR : -diffR; + if ( + // Our y-difference is small enough + diffL <= paddleH / 2.0f && + // We are in the correct x-position + ball.position.x <= 25.0f && ball.position.x >= 20.0f && + // The ball is moving to the left + ball.velocity.x < 0.0f) + { + ball.velocity.x *= -1.0f; + } + else if ( + diffR <= paddleH / 2.0f && + ball.position.x >= 1024.0f - 25.0f && ball.position.x >= 1024.0f - 20.0f && + ball.velocity.x > 0.0f) + { + ball.velocity.x *= -1.0f; + } + // Did the ball go off the screen? (if so, end game) + else if (ball.position.x <= 0.0f || ball.position.x >= 1024.0f) + { + mIsRunning = false; + } + + // Did the ball collide with the top wall? + if (ball.position.y <= thickness && ball.velocity.y < 0.0f) + { + ball.velocity.y *= -1; + } + // Did the ball collide with the bottom wall? + else if (ball.position.y >= (768 - thickness) && + ball.velocity.y > 0.0f) + { + ball.velocity.y *= -1; + } } - // Did the ball go off the screen? (if so, end game) - else if (mBallPos.x <= 0.0f || mBallPos.x >= 1024.0f) - { - mIsRunning = false; - } - - // Did the ball collide with the top wall? - if (mBallPos.y <= thickness && mBallVel.y < 0.0f) - { - mBallVel.y *= -1; - } - // Did the ball collide with the bottom wall? - else if (mBallPos.y >= (768 - thickness) && - mBallVel.y > 0.0f) - { - mBallVel.y *= -1; - } } void Game::GenerateOutput() @@ -271,14 +285,16 @@ void Game::GenerateOutput() }; SDL_RenderFillRect(mRenderer, &paddleR); - // Draw ball - SDL_Rect ball{ - static_cast(mBallPos.x - thickness/2), - static_cast(mBallPos.y - thickness/2), - thickness, - thickness - }; - SDL_RenderFillRect(mRenderer, &ball); + for(const auto &ball : mBalls) { + // Draw ball + SDL_Rect ballRect{ + static_cast(ball.position.x - thickness/2), + static_cast(ball.position.y - thickness/2), + thickness, + thickness + }; + SDL_RenderFillRect(mRenderer, &ballRect); + } // Swap front buffer and back buffer SDL_RenderPresent(mRenderer); diff --git a/Chapter01/Game.h b/Chapter01/Game.h index 7710c861..29962c6d 100644 --- a/Chapter01/Game.h +++ b/Chapter01/Game.h @@ -8,6 +8,7 @@ #pragma once #include "SDL/SDL.h" +#include // Vector2 struct just stores x/y coordinates // (for now) @@ -17,6 +18,12 @@ struct Vector2 float y; }; +struct Ball +{ + Vector2 position; + Vector2 velocity; +}; + // Game class class Game { @@ -46,16 +53,12 @@ class Game // Pong specific // Direction of left player paddle int mPaddleLeftDir; - // Position of paddle Vector2 mPaddleLeftPos; - // Position of ball - Vector2 mBallPos; - // Velocity of ball - Vector2 mBallVel; - // Direction of right player paddle int mPaddleRightDir; Vector2 mPaddleRightPos; + + std::vector mBalls; };