In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to use the wizard and collision detection in Pygame". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Before we begin to learn the relevant knowledge points, it is necessary for us to learn the meaning of sprites and collision detection.
Genie (translated in English as Sprite), in fact, in a game program, elf essence refers to a small size of pictures, such as the game's various props, characters, scene decoration, etc., they can be seen into a small "elf" picture. In addition, the movement of a character can also be thought of as a sequence of elf images (a sequence of frames), as shown in the following figure:
Figure 1: action decomposition diagram frame by frame
If the action after being decomposed frame by frame is played at a certain frequency, then an animated spirit will be formed, and you will see eagles flying high, people galloping on horses, and athletes struggling for the long jump.
A feature of elves is that they allow elves to interact with each other, also known as collisions, while collision detection refers to the detection of whether a collision has occurred between two elves. For example, whether the head of the snake collided with the food in the gluttonous snake game, or whether the bullet hit the alien in the plane war game and so on. When a collision is detected, some events will be triggered, such as a bullet hitting an alien, the alien will disappear, the player's score will increase, and another alien will appear on the game screen.
Pygame specifically provides a module for dealing with sprites, that is, the sprite (pygame.sprite) module. Typically, we use the module's base class, Sprite, to create a subclass for dealing with sprites, which provides common properties and methods for manipulating sprites, as follows:
Property & method explains that self.image loads the sprite image to be displayed, controls the size and fill color of the self.rect sprite image, and where the Sprite.update () refreshes the sprite image. Make its corresponding effect effective Sprite.add () add elf map to the spirit group (groups) Sprite.remove () delete the selected elf image from the spirit group Sprite.kill () delete all the sprite Sprite.alive () in the spirit group to determine whether a spirit belongs to the spirit group
Note that when there are a large number of sprites in the game, manipulating them becomes complex, and these sprites are managed uniformly by building a sprite container (group class), that is, a sprite group. The construction method is as follows:
# create a sprite group group = pygame.sprite.Group () # add a sprite group.add (sprite_one) to the group
At the same time, the pygame.sprite module also provides a variety of ways to detect whether sprites collide, as follows:
The method describes the rectangle detection between the two sprites of pygame.sprite.collide_rect (), that is, whether the rectangular region intersects, and returns a Boolean value. The circle detection between the two sprites of pygame.sprite.collide_circle (), that is, whether the circular areas meet, returns a Boolean value. Pixel mask detection between pygame.sprite.collide_mask () two sprites is a more accurate detection method. The rectangular collision detection between the pygame.sprite.spritecollide () wizard and the spirit group, in which all the sprites in one group perform collision detection on another single elf one by one, and the return value is a list of all the sprites that collided. Pygame.sprite.spritecollideany () wizard and spirit group between the rectangular collision detection, a variant of the above function, when a collision occurs, returns a wizard in the group, no collision occurs, returns None. Pygame.sprite.groupcollide () detects all sprites that collide between two groups and returns a dictionary with the sprite that collides in the first group as the key and the sprite that collides in the second group as the value.
Let's take a look at a set of simple examples, with the code as follows:
Import pygameclass Snake (pygame.sprite.Sprite): # defines the constructor def _ _ init__ (self,filename Location): # call the parent class to initialize the subclass pygame.sprite.Sprite.__init__ (self) # load the picture self.image = pygame.image.load (filename) # get the picture rect area self.rect = self.image.get_rect () # set the location self.rect.topleft=location# initialize pygamepygame.init ( ) screen = pygame.display.set_mode ((500400)) pygame.display.set_caption ('C language Chinese net') # is filled with white screen screen.fill ((255255255)) filename = "C:/Users/Administrator/Desktop/snake.png" location = (100150) snake1 = Snake (filename) Location) # collision detection, there must be two sprites So create another wizard and use location to control the location of the second wizard location_2 = (100pjj80) snake2 = Snake ('C _ stop _ UsersActuator _ account _ Desktopip _ logo.png _ wizard _ locationkeeper 2) # call collide_rect () for rectangular region detection, return a Boolean value, collision return True, otherwise return Falsecrash_result = pygame.sprite.collide_rect (snake1,snake2) if crash_result: print ("Sprite collided!") Passelse: print ('genie didn't collide') while True: for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () exit () # draw wizard to the screen screen.blit (snake1.image,snake1.rect) screen.blit (snake2.image,snake2.rect) # refresh the display screen pygame.display.update ()
When the sprite does not collide, the result of the program is as follows:
Figure 2: run result
Next, we change the location_2 parameter of snake2 to (100140), and then run the program again, when the two sprites collide, and the result is as follows:
Figure 3: run result
That's all for "how Pygame uses Sprite and collision Detection". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.