Home > Articles

  • Print
  • + Share This
This chapter is from the book

Exercises

Both of this chapter’s exercises focus on modifying your version of Pong. The first exercise involves adding a second player, and the second exercise involves adding support for multiple balls.

Exercise 1.1

The original version of Pong supported two players. Remove the right wall onscreen and replace that wall with a second paddle for player 2. For this second paddle, use the I and K keys to move the paddle up and down. Supporting a second paddle requires duplicating all the functionality of the first paddle: a member variable for the paddle’s position, the direction, code to process input for player 2, code that draws the paddle, and code that updates the paddle. Finally, make sure to update the ball collision code so that the ball correctly collides with both paddles.

Exercise 1.2

Many pinball games support “multiball,” where multiple balls are in play at once. It turns out multiball is also fun for Pong! To support multiple balls, create a Ball struct that contains two Vector2s: one for the position and one for the velocity. Next, create a std::vector<Ball> member variable for Game to store these different balls. Then change the code in Game::Initialize to initialize the positions and velocities of several balls. In Game::UpdateGame, change the ball update code so that rather than using the individual mBallVel and mBallPos variables, the code loops over the std::vector for all the balls.

  • + Share This
  • 🔖 Save To Your Account