In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to understand the wizard and collision detection in Python Pygame". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand the wizard and collision detection in Python Pygame".
Pygame Sprite and collision Detection
Today let's take a look at pygame, python's most famous game library. Learn two nouns: genie and collision detection.
The Elvish letter is Sprite. Sprite is a two-dimensional graphics, in the game can be used as a variety of objects, such as characters, props, anyway, everything shown in the game image can use Sprite.
Collision detection is to detect whether there is a collision between two Sprite. For example, in the game to eat gold coins, eat beans, fight the enemy can use collision detection.
Class Sprite (pygame.sprite.Sprite): def _ init__ (self, pos): pygame.sprite.Sprite.__init__ (self) self.image = pygame.Surface ([20,20]) self.image.fill ((255,0,255)) self.rect = self.image.get_rect () self.rect.center = pos
The above is the definition of the Sprite class, if you need more content, you can add it yourself. Or just inherit it.
Pygame.init () clock = pygame.time.Clock () fps = 50 bg = [0,0,0] size = [300,300] screen = pygame.display.set_mode (size) player = Sprite ([40,50]) # Define keys for player movement player.move = [pygame.K_LEFT, pygame.K_RIGHT, pygame.K_UP, pygame.K_DOWN] player.vx = 5 player.vy = 5 wall = Sprite 60]) wall_group = pygame.sprite.Group () wall_group.add (wall) player_group = pygame.sprite.Group () player_group.add (player)
Initialize the game and divide the wall into wall_group and the player into player_group. This is to prepare for collision detection.
While True: for event in pygame.event.get (): if event.type = = pygame.QUIT: return False key = pygame.key.get_pressed () for i in range (2): if key [player. Move [I]: player.rect.x + = player.vx * [- 1 1] [I] for i in range (2): if key [player.move [2:4] [I]]: player.rect.y + = player.vy * [- 1,1] [I] screen.fill (bg) # first parameter takes a single sprite # second parameter takes sprite groups hit = pygame.sprite.spritecollide (player, wall_group True) if hit: # if collision is detected call a function to destroy # rect player.image.fill ((255,255,255))
The above code has key detection, for example, pressing the exit button will end the game. Pressing up and down will move the player. Finally, pygame.sprite.spritecollide is the collision detection function. When the player hits the wall, it turns red.
Attach more examples of pygame wizard collision detection
Rectangle detection between two sprites
Pygame.sprite.collide_rect (first, second) # returns a Boolean value
Rectangle detection between sprites and groups
# the first parameter is the sprite, the second parameter is the elf group, and the third parameter is True, then after collision detection, all colliding sprites in the group are deleted # return the collided sprite in the group collide_list = pygame.sprite.spritecollide (sprite,group,False)
Rectangle detection between two groups
# the first two parameters are both groups # the last two parameters represent whether to delete sprites in the event of a collision # this function returns a dictionary # each wizard in the first group will be added to the dictionary # the sprite that collides with it in the second group will be added to the corresponding entry in the dictionary hit_list = pygame.sprite.groupcollide (group1,group2,True,False) Thank you for reading The above is the content of "how to understand the wizard and collision detection in Python Pygame". After the study of this article, I believe you have a deeper understanding of how to understand the wizard and collision detection in Python Pygame. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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: 0
*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.