pygame中大小不一的精灵碰撞

问题描述

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

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

在我的管道班上,我有两种与管道的高度和位置有关的方法

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"),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,0)) 

如您所见,对于管道类的每个实例,在屏幕上绘制了两个管道。一个在屏幕顶部,另一个在屏幕对面。然后,两个管道都在屏幕上移动。

这是bird类的一些代码

def __init__(self,y):
    self.bird1 = pygame.transform.scale(pygame.image.load("bird1.png"),(34,24))
    self.bird2 = pygame.transform.scale(pygame.image.load("bird2.png"),24))
    self.bird3 = pygame.transform.scale(pygame.image.load("bird3.png"),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.y)) 
    # The image of the bird on the screen switches between 3 images. The height and width of the bird stay constant.

用户左键单击时,鸟会跳起并可以根据用户的鼠标单击在管道之间“飞行”(每次单击鼠标时,鸟的y坐标都会增加,鸟的y坐标会增加用户什么都不做时,它会逐渐减少)。我现在如何找到一种方法来检测鸟儿是否真的碰到了一根烟斗,在这种情况下游戏将结束?

Game preview

解决方法

要检查管道\鸟的碰撞,请在管道类中为上部\下部的管道创建一个矩形,然后创建一种测试碰撞的方法:

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"),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,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.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