create playerClass#1
Conversation
create the move function inside the playerclass
There was a problem hiding this comment.
Right now the Player object is determining the edges of the moveable area. This kind of logic should be handled somewhere else instead. For example, in program.cpp you could replace
if (key_down(RIGHT_KEY)) {with:
if (key_down(RIGHT_KEY) && player.get_x() <= 880) {This helps to maintain separation of concerns, with the game logic maintaining control over game objects, rather than the game objects basically giving themselves orders.
That's the big one, but here a couple more suggestions that will improve readability:
- Use constants to define numbers instead of having the numbers in-line. For example, the
880mentioned above can be replaced withconst int RIGHT_BOUNDARY = 880, which is much more descriptive and makes the code easier to understand. Similarly, values such as the player's initialized speed could be turned into constants. This will become increasingly relevant as the size of the program increases. - Generally we recommend commenting functions in header files to make it easier for other developers to understand how to use them in the future. You can see a great example of this in Lost in Space from the recent Hackathon that Olivia mentioned. That level of function-describing would be ideal, but even a simpler implementation would be good to have.
Lastly, you can optionally replace the header guards in the player.h file (#ifndef PLAYER_H, #define PLAYER_H, and #endif) with #pragma once. You may find that this makes the header files easier to read and less error-prone, but it's up to you - we don't currently have this choice standardized so either way is fine as long as you're consistent within the project.
|
One thing I only just realized is that your code for |
create the move_right(), move_left(), get_x(), get_y() inside the player class.
and call it inside the program.cpp
and it is work