Skip to content

create playerClass#1

Merged
zhusim222 merged 4 commits intozhusim222:mainfrom
lexlam1524:PlayerClass
Aug 29, 2024
Merged

create playerClass#1
zhusim222 merged 4 commits intozhusim222:mainfrom
lexlam1524:PlayerClass

Conversation

@lexlam1524
Copy link
Contributor

@lexlam1524 lexlam1524 commented Aug 10, 2024

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

create the move function inside the playerclass
Copy link

@MHLoppy MHLoppy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. Use constants to define numbers instead of having the numbers in-line. For example, the 880 mentioned above can be replaced with const 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.
  2. 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.

@MHLoppy
Copy link

MHLoppy commented Aug 25, 2024

One thing I only just realized is that your code for spawn_number will always result in 0, since the divisor is 1 (and dividing any number by 1 doesn't leave a remainder!). Based on your comment I think you meant to set this to int spawn_number = (rand() % 2);?

@zhusim222 zhusim222 merged commit d840997 into zhusim222:main Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants