pygame 大小不同的精灵碰撞

Collision of sprites that vary in size in pygame

我正在尝试编写类似于 flappy bird 游戏的代码。在我的程序中,我希望能够检查小鸟是否与管道接触,如果是,游戏应该结束。我有一个管道 class 和一只鸟 class,我有两个管道实例 class:

bird = bird(125,185)
pipe1 = pipe(300, 255)
pipe2 = pipe(520, 255)

在我的管道中class我有这两种与管道高度和位置相关的方法:

def __init__(self, x, y):
    self.bottom = pygame.transform.scale(pygame.image.load("pipe.png"), (52,145))
    self.x = x
    self.y = y
    self.width = 52
    self.height = 145

def resize(self): # The height of the pipe will vary randomly
    self.height = random.randint(100,150)
    self.bottom = pygame.transform.scale(pygame.image.load("pipe.png"), (52,self.height))
    self.y = 400 - self.height

def draw(self, win):
    if self.x > -52:
        self.x -= 5 # Pipe will move to the left of the screen
    else:
        self.x = 400
        self.resize()
    win.blit(self.bottom, (self.x,self.y))
    self.top = pygame.transform.rotate(self.bottom, 180) # There is a bottom pipe and a top pipe
    win.blit(self.top, (self.x,0)) 

如您所见,对于管道的每个实例 class,屏幕上都会绘制两条管道。一个在屏幕的顶部,一个在屏幕的底部。然后两个管道都在屏幕上移动。

这是小鸟的部分代码class:

def __init__(self, x, y):
    self.bird1 = pygame.transform.scale(pygame.image.load("bird1.png"), (34,24))
    self.bird2 = pygame.transform.scale(pygame.image.load("bird2.png"), (34,24))
    self.bird3 = pygame.transform.scale(pygame.image.load("bird3.png"), (34,24))
    self.birdpics = [self.bird1, self.bird2, self.bird3]
    self.x = x
    self.y = y
    self.width = 34
    self.height = 24
    self.moveCount = 0
    self.jumpCount = 7
    self.isJump = False
    self.image = self.birdpics[self.moveCount]

def draw(self, win):
    self.moveCount += 1
    if self.moveCount >= len(self.birdpics):
        self.moveCount = 0
    self.image = self.birdpics[self.moveCount]
    win.blit(self.image, (self.x,self.y)) 
    # The image of the bird on the screen switches between 3 images. The height and width of the bird stay constant.

当用户单击鼠标左键时,小鸟会根据用户的鼠标点击在管道之间跳跃 'fly'(每次单击鼠标时小鸟的 y 坐标都会增加,而 y-当用户什么都不做时,鸟的坐标稳定下降)。我现在如何找到一种方法来检测小鸟是否真的碰到了其中一根管道,在这种情况下游戏将结束?

要检查 pipe\bird 碰撞,在管道 class 中为 upper\lower 管道创建一个矩形,然后创建一个方法来测试碰撞:

def __init__(self, x, y):
    self.bottom = pygame.transform.scale(pygame.image.load("pipe.png"), (52,145))
    self.x = x
    self.y = y
    self.width = 52
    self.height = 145
    self.rects = [] # top\bottom pipe

def resize(self): # The height of the pipe will vary randomly
    self.height = random.randint(100,150)
    self.bottom = pygame.transform.scale(pygame.image.load("pipe.png"), (52,self.height))
    self.y = 400 - self.height

def draw(self, win):
    if self.x > -52:
        self.x -= 5 # Pipe will move to the left of the screen
    else:
        self.x = 400
        self.resize()
    win.blit(self.bottom, (self.x,self.y))
    self.top = pygame.transform.rotate(self.bottom, 180) # There is a bottom pipe and a top pipe
    win.blit(self.top, (self.x,0)) 
    # save rect list for top\bottom pipe
    self.rects=[
         pygame.rect((self.x,self.y),(self.width,self.height)), # bottom
         pygame.rect((self.x,0),(self.width,self.height))  # top
         ]

def CheckCollide(bird):  # did bird collide with pipe
    birdrect = pygamerect(bird.x. bird.y, bird.width, bird,height)
    for r in self.rects:
       if birdrect.colliderect(r):
           return True  # bird hit pipe
    return False # no collision