I am new to C++ programming, and I am making Pong in Visual C++. My game will have two game modes, multiplayer and singleplayer. Right now, I am planning on having a separate class for the singleplayer paddles and the multiplayer paddles. (So I’ll have PaddleMP RpaddleMP, LpaddleMP; and PaddleSP RpaddleSP, LpaddleSP;, where the R and L represent the right and left paddles respectively). However, the code for the multiplayer paddle and singleplayer paddle will be very similar. Would it be better to use one class for them both and simply have an initializer function that allows me to pick whether it is a singleplayer or multiplayer paddle, or should I just make a new class for both?
I am new to C++ programming, and I am making Pong in Visual C++.
Share
IMO, you have two problems here. First, I think the names you’ve chosen (single player vs. multiplayer) are poor. Second, I think the paddle itself should be independent of the input that moves the paddle.
Therefore, there should really only be one paddle type. You can tell it to move up or down and it’ll move the direction you’ve told it, but the paddle itself knows nothing about the source of the movement command(s).
Then I’d have something that creates/sends the input to the paddle. It seems to me that rather than single player vs. multi-player the relevant distinction here is between a player input and an AI input.
So, we have a player input that reads relevant data from the mouse, keyboard, joystick, etc., and turns it into the relevant commands to send to the paddle. We also have an AI input that produces similar commands, but based on its own computation rather than input from the user.
This is where you want the inheritance — the AI input and the player input both inherit from generic input type that can produce commands. Depending on your taste, you might not want an actual player input either — rather, you might have a keyboard input, joystick input and mouse input separate from each other. This will, for example, probably make it a bit easier if you decide to add support for something like a multi-touch screen, which will probably be at least somewhat different from any of the above (but still follow the same protocol/implement the same interface).