I'm making a simple Pong clone game and have been having an issue deciding on how my classes should handle the initial placement of an object.
For instance, my
Ball class's constructor is:
Ball (std::string imageName, float screenWidth, float screenHeight)
The constructor then automatically centers the ball in the middle of the screen, since it knows the ball's width and height the dimensions the image it loads, and the screen width and height by the arguments it is passed.
I would rather be able to place the ball manually, and just take the exact
y locations in the constructor:
Ball (std::string imageName, float x, float y)
But that would require the calling function to already know the ball's
height in order to center the ball and when it creates a ball it'd have to write something like:
Ball ball("ball.png", ((SCREEN_WIDTH / 2) - (BALL_WIDTH / 2)), ((SCREEN_HEIGHT / 2) - (BALL_HEIGHT / 2)))
I know this may be personal preference, but I'm trying to figure out which way is generally accepted.
A similar issue occurs with my
Paddle class. Its constructor argument list looks like this:
Paddle (std::string imageName, float screenWidth, float screenHeight, int player)
Since I set the location inside of the constructor, it's forced me to have a
player flag so I know if the paddle is supposed to line up with the left or right hand side of the screen. If I took the exact
y location in the constructor's argument list, I could figure out which player is which internally using math.
I'm thinking that if the calling function kept consts of all of their objects' widths and heights they could use those in all of their objects' constructors in order to manually set their starting position... but is this the best way to do this?
Now I'm thinking that it would be realistic to expect the calling function to know the center x and center y that it wants the object at, and the object itself can use those x and y coordinates to center itself using its own width and height.
I'm starting to think this may have been a silly question since it really depends on the type of game you're making and how you're using the specific classes. I appreciate any input!