使用PyGame检测两个Sprite是否重叠

时间:2019-02-03 18:34:06

标签: python pygame

我正在尝试制作自己的游戏,我需要知道两个精灵何时重叠,如果它们重叠,则游戏将使用win.blit加载到新图片中 我尝试查看其他人的帖子,但是他们根本没有帮助我。我是新手,请详细解释

编辑:请使其尽可能短

1 个答案:

答案 0 :(得分:0)

越短越好。

if ( sprite1.rect.colliderect( sprite2.rect ) ):
    # TODO handle collision

但有一个更有用的示例...

创建一个全局精灵组。 Sprite组允许代码一次对整个组进行一次简单的冲突检查。也许两组有用,例如:aliensbulletsbiscuitsdips

SPRITES = pygame.sprite.Group()

定义一个精灵。子画面的update()函数在每帧中被调用以执行子画面需要进行的所有 帧间操作。这可能是移动,更改位图(用于动画)或检查碰撞等。此精灵具有name,因此我们可以打印出与谁碰撞的人。

class RockSprite(pygame.sprite.Sprite):
    def __init__(self, name, image, position):
        pygame.sprite.Sprite.__init__(self)
        self.name         = name
        self.image        = image
        self.rect         = self.image.get_rect()
        self.rect.center  = position

    def update(self):
        # Move the sprite
        # ... TODO
        # Have we collided with any sprite?
        hit_by = pygame.sprite.spritecollide( self, SPRITES, False )
        hit_by.remove( self ) # other than ourselves
        for other_sprite in hit_by:
            print( "Sprite [%s] collided with [%s]" % ( self.name, other_sprite.name ) )

创建一堆精灵。这说明了如何创建RockSprite的实例并将其添加到SPRITE组中。

# Make some sprites, including two that overlap (but it depends on the .png size)
sprite_image = pygame.image.load("rock.png").convert_alpha()
rock_01 = RockSprite( "rock01", sprite_image, (10, 10) )
rock_02 = RockSprite( "rock02", sprite_image, (15, 15) )
rock_03 = RockSprite( "rock03", sprite_image, (50, 50) )
# Add them to the global SPRITE group
SPRITES.add(rock_01)        
SPRITES.add(rock_02)
SPRITES.add(rock_03)

请确保在主循环中调用子画面组update()

while not done:

    # Move the sprites (and checks for collisions) - calls update() on each member
    SPRITES.update()

    # paint he screen
    # handle user input  
    ...

其中提供了完整的演示代码:

import pygame
WINDOW_WIDTH=400
WINDOW_HEIGHT=400

pygame.init()
WINDOW  = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
SPRITES = pygame.sprite.Group()

class RockSprite(pygame.sprite.Sprite):
    def __init__(self, name, image, position):
        pygame.sprite.Sprite.__init__(self)
        self.name         = name
        self.image        = image
        self.rect         = self.image.get_rect()
        self.rect.center  = position

    def update(self):
        # Move the sprite
        # ... TODO
        # Have we collided with any sprite?
        hit_by = pygame.sprite.spritecollide( self, SPRITES, False )
        hit_by.remove( self ) # other than ourselves
        for other_sprite in hit_by:
            print( "Sprite [%s] collided with [%s]" % ( self.name, other_sprite.name ) )


# Make some sprites, including two that overlap
sprite_image = pygame.image.load("rock.png").convert_alpha()
rock_01 = RockSprite( "rock01", sprite_image, (10, 10) )
rock_02 = RockSprite( "rock02", sprite_image, (15, 15) )
rock_03 = RockSprite( "rock03", sprite_image, (90, 90) )
# Add them to the global SPRITE group
SPRITES.add(rock_01)
SPRITES.add(rock_02)
SPRITES.add(rock_03)

clock = pygame.time.Clock()
done = False
while not done:

    # Move the sprites (and checks for collisions)
    SPRITES.update()

    # Paint the screen
    WINDOW.fill( ( 0,0,0 ) )
    SPRITES.draw( WINDOW )
    pygame.display.update()
    pygame.display.flip()

    # Check for user-events
    for event in pygame.event.get():
        if (event.type == pygame.QUIT):
            done = True

    clock.tick_busy_loop(60)