Python 使用PyGame检测两个精灵是否重叠

Python 使用PyGame检测两个精灵是否重叠,python,pygame,Python,Pygame,我正在尝试制作自己的游戏,我需要知道两个精灵何时重叠,如果重叠,游戏将使用win.blit加载到新图片中 我试过看其他人的帖子,但他们根本帮不了我。我是新来的,请给我一个深入的解释 编辑:请尽量简短 编辑2(大约2年后):很抱歉把这样一个狗屎问题尽量简短 if ( sprite1.rect.colliderect( sprite2.rect ) ): # TODO handle collision 但有一个更有用的例子 创建一个全局精灵组。sprite组允许代码一次对整个组执行简单的冲

我正在尝试制作自己的游戏,我需要知道两个精灵何时重叠,如果重叠,游戏将使用win.blit加载到新图片中 我试过看其他人的帖子,但他们根本帮不了我。我是新来的,请给我一个深入的解释

编辑:请尽量简短

编辑2(大约2年后):很抱歉把这样一个狗屎问题尽量简短

if ( sprite1.rect.colliderect( sprite2.rect ) ):
    # TODO handle collision
但有一个更有用的例子

创建一个全局精灵组。sprite组允许代码一次对整个组执行简单的冲突检查。可能有两组有用,例如:
外星人
子弹
饼干
蘸酱

SPRITES = pygame.sprite.Group()
定义一个精灵。精灵的
update()
函数会在每一帧中调用,以完成精灵在帧间需要完成的所有操作。这可能是移动、更改位图(用于动画)或检查碰撞等。此精灵具有
名称
,因此我们可以打印出谁与谁发生碰撞

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 (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)
确保在主循环中调用sprite组
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)

用于获取图像的大小(
曲面
),并用于检查两个矩形是否重叠。到目前为止,您尝试了什么?StackOverflow不是免费的代码编写服务。希望你表现出一些努力。