In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge about "how to use Python to simulate gravity in the game". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Add a gravity function
Remember that your player already has an action-determining attribute. Use this attribute to pull the player sprite toward the bottom of the screen.
In Pygame, higher numbers are closer to the bottom edge of the screen.
In the real world, gravity affects everything. However, in platform games gravity is selective-if you add gravity to your entire game world, all your platforms will fall to the ground. Instead, you can add gravity only to your players and enemy sprites.
First, add a gravity function to your Player class:
def gravity(self): self.movey += 3.2 #how fast players drop
This is a simple function. First, you set your player to move vertically, regardless of whether your player wants to move or not. That is, you have programmed your players to always be on the decline. This is basically gravity.
For gravity to work, you must call it in your main loop. This way, Python applies drop motion to your players as each processing loop.
In this code, add the first line to your loop:
player.gravity() #Check gravity player.update()
Start your game and see what happens. Be careful, because it happens quickly: you are a player falling from the sky and immediately dropping off your game screen.
Your gravity simulation worked, but maybe too well.
As an experiment, try changing the speed at which your player falls.
Add a floor to gravity
Your game has no way of detecting when your character drops out of the world. In some games, if a player drops out of the world, the sprite is deleted and reborn in a new location. In other games, players lose points or a life. When a player drops out of the world, no matter what you want to happen, you must be able to detect when the player disappears off screen.
In Python, to check a condition, you can use an if statement.
You have to check if your players are dropping and to what extent your players are dropping. If your player falls to the bottom of the screen, then you can do something about it. To simplify, set the position of the player sprite to 20 pixels above the bottom edge.
Make your gravity function look like this:
def gravity(self): self.movey += 3.2 #how fast players drop if self.rect.y > worldy and self.movey >= 0: self.movey = 0 self.rect.y = worldy-ty
Then start your game. Your sprite is still falling, but it stops at the bottom of the screen. However, you may not be able to see your sprites above the ground level. A simple solution is to bounce your sprite higher after it collides with the bottom of the game world by adding another-ty to its new Y position:
def gravity(self): self.movey += 3.2 #how fast players drop if self.rect.y > worldy and self.movey >= 0: self.movey = 0 self.rect.y = worldy-ty-ty
Now your player bounces at the bottom of the screen, right above your ground sprite.
What your players really need is a way to fight gravity. The problem with gravity is that you can't fight it unless you have something to push gravity away. So, in the next article, you'll add ground and platform collisions and jumping abilities. In the meantime, try applying gravity to enemy sprites.
So far, here's the full code:
#!/ usr/bin/env python3# draw a world# add a player and player control# add player movement# add enemy and basic collision# add platform# add gravity # GNU All-Permissive License# Copying and distribution of this file, with or without modification,# are permitted in any medium without royalty provided the copyright# notice and this notice are preserved. This file is offered as-is,# without any warranty. import pygameimport sysimport os '''Objects''' class Platform(pygame.sprite.Sprite): # x location, y location, img width, img height, img file def __init__(self,xloc,yloc,imgw,imgh,img): pygame.sprite.Sprite.__ init__(self) self.image = pygame.image.load(os.path.join('images',img)).convert() self.image.convert_alpha() self.rect = self.image.get_rect() self.rect.y = yloc self.rect.x = xloc class Player(pygame.sprite.Sprite): ''' Spawn a player ''' def __init__(self): pygame.sprite.Sprite.__ init__(self) self.movex = 0 self.movey = 0 self.frame = 0 self.health = 10 self.score = 1 self.images = [] for i in range(1,9): img = pygame.image.load(os.path.join('images','hero' + str(i) + '.png')).convert() img.convert_alpha() img.set_colorkey(ALPHA) self.images.append(img) self.image = self.images[0] self.rect = self.image.get_rect() def gravity(self): self.movey += 3.2 # how fast player falls if self.rect.y > worldy and self.movey >= 0: self.movey = 0 self.rect.y = worldy-ty-ty def control(self,x,y): ''' control player movement ''' self.movex += x self.movey += y def update(self): ''' Update sprite position ''' self.rect.x = self.rect.x + self.movex self.rect.y = self.rect.y + self.movey # moving left if self.movex
< 0: self.frame += 1 if self.frame >ani*3: self.frame = 0 self.image = self.images[self.frame//ani] # moving right if self.movex > 0: self.frame += 1 if self.frame > ani*3: self.frame = 0 self.image = self.images[(self.frame//ani)+4] # collisions enemy_hit_list = pygame.sprite.spritecollide(self, enemy_list, False) for enemy in enemy_hit_list: self.health -= 1 print(self.health) ground_hit_list = pygame.sprite.spritecollide(self, ground_list, False) for g in ground_hit_list: self.health -= 1 print(self.health) class Enemy(pygame.sprite.Sprite): ''' Spawn an enemy ''' def __init__(self,x,y,img): pygame.sprite.Sprite.__ init__(self) self.image = pygame.image.load(os.path.join('images',img)) #self.image.convert_alpha() #self.image.set_colorkey(ALPHA) self.rect = self.image.get_rect() self.rect.x = x self.rect.y = y self.counter = 0 def move(self): ''' enemy movement ''' distance = 80 speed = 8 if self.counter >= 0 and self.counter = distance and self.counter
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 216
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.