Jumping line game4/6/2023 ![]() Your code is only emulating gravity in your game world. Not all game engines require this much interaction with gravity, but Pygame isn't designed exclusively for platformers (you could write a top-down game instead, for example) so gravity isn't managed by the engine. When you have gravity on all the time (in Pygame), you risk getting a bounce-effect on your hero sprite as gravity constantly tries to force the hero down while the collision with the ground resists. In your game, though, gravity only needs to be "on" when the hero sprite isn't standing on solid ground. In the real world, jumping is an act of moving against gravity. The other variable ( is_falling) is also set to True because the hero does indeed need to descend to the ground. In other words, this is a small initial lie that serves the program, so set it to True. It also evokes classic platformers, and gives the player the sense of "jumping into" the game world. However, I find it easier to let gravity help the hero find the ground rather than having to hard code a spawn position every level. This is theoretically an abuse of this Boolean value, and it is admittedly "cleaner" code to have True and False statements that actually reflect reality. This is a little counter-intuitive, because the hero isn't actually jumping. The first variable ( is_jumping) is set to True because I'm spawing the hero in the sky and need it to fall immediately to the ground, as if it were in mid-jump. In this case, the hero sprite can either be falling or not falling, and it can be jumping or not jumping. In programming, this is a special data type indicating that a variable is either "on" or "off". These new values are called Boolean values, which is a term (named after mathematician George Boole) meaning either true or false. In the following code, the lines above the comment are for context, so just add the final two lines: One to bring the player back down to the groundĪdd these variables to your Player class.One to track whether your player is jumping or not, determined by whether or not your player sprite is standing on solid ground.You must add two new variables to your Player class: Once the player sprite is jumping, then gravity is applied to the player sprite again, pulling it back down to the nearest object. First, you must establish variables for the player sprite so that Python can track whether or not the sprite is jumping. But once you hit the peak of your jump, gravity kicks in again and pulls you back down to earth. For a few moments, you jump up instead of falling down, the way gravity is pulling you. In the previous article in this series, you simulated gravity, but now you need to give your player a way to fight against gravity by jumping.Ī jump is a temporary reprieve from gravity.
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |