![]() The problem with gravity is, you can't fight it unless you have something to push off of. What your player really needs is a way to fight gravity. Now your player bounces at the bottom of the screen, just behind your ground sprites. ![]() An easy fix is to make your player sprite bounce higher by adding another -ty to its new Y position after it hits the bottom of the game world: def gravity(self): You may not be able to see your sprite behind the ground layer, though. Your sprite still falls, but it stops at the bottom of the screen. Make your gravity function look like this: def gravity(self): To keep things simple, set the position of the player sprite to 20 pixels above the bottom edge. If your player falls so far that it reaches the bottom of the screen, then you can do something. You must check to see if your player is falling and how far your player has fallen. In Python, to check for a condition, you can use an if statement. Whatever you want to happen when a player falls off the world, you have to be able to detect when the player disappears offscreen. In other games, the player loses points or a life. In some games, if a player falls off the world, the sprite is deleted and respawned somewhere new. The problem with your character falling off the world is that there's no way for your game to detect it. Your gravity simulation is working, but maybe too well.Īs an experiment, try changing the rate at which your player falls. Look sharp, because it happens fast: your player falls out of the sky, right off your game screen. In this code, add the first line to your loop: avity() # check gravity This way, Python applies the falling motion to your player once every clock tick. That's basically gravity.įor the gravity function to have an effect, you must call it in your main loop. In other words, you have programmed your player to always be falling. First, you set your player in vertical motion, whether your player wants to be in motion or not. Self.movey += 3.2 # how fast player falls ![]() Instead, you add gravity just to your player and enemy sprites.įirst, add a gravity function in your Player class: def gravity(self): In platformers, however, gravity is selective-if you add gravity to your entire game world, all of your platforms would fall to the ground. In the real world, gravity affects everything. In Pygame, higher numbers are closer to the bottom edge of the screen. Use this property to pull the player sprite toward the bottom of the screen. Remember that your player already has a property to determine motion. ![]() In video game physics, you don't have to create objects with mass great enough to justify a gravitational pull you can just program a tendency for objects to fall toward the presumed largest object in the video game world: the world itself. The larger the object, the more gravitational influence it exerts. Gravity in the real world is the tendency for objects with mass to be drawn toward one another. If you can't think of any reasons, don't worry-it'll become apparent as you work through the sample code. Think about why gravity might involve collisions. You implemented some collision detection when you added an enemy to your game, but this article adds more because gravity requires collision detection.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |