我为飞扬的龙创建了碰撞箱,但碰撞箱没有碰撞或没有产生输出。我该如何解决?

I have created hitboxes for my flappy dragon but the hitboxes are either not colliding or doesn't produce an output. How do i fix that?

我正在尝试为我的 Python 编程制作一个 flappybird 游戏 class 我使用了像 drag.hit() 这样的提示来使它们打印("hit" ) 每次 hitboxes 应该碰撞。但是,dragon 和 stumpy hitbox 的碰撞不起作用,我似乎无法让它起作用。

这是我的代码:

    pygame.init()

class player(object):   **class for dragon**
    def __init__(self, x, y, width, height):
        self.hitbox = (self.x + 5, self.y +3, 67, 65)

 def draw(self,win):     **drawing of hitbox for dragon**
        self.hitbox = (self.x + 5, self.y +3, 67, 65)     
        pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)

    def hit(self):        **During collision of hitbox, print hit**
        print("hit")


class stumpy(object):    **class for obstacle 1**
    def __init__(self, x, y, width, height):
        self.hitbox = (self.x + 8, self.y+10, self.width - 18, self.height-15)


    def draw(self, win):
        win.blit(pygame.transform.scale(self.walkLeft[self.count//3], (75, 200)), (self.x, self.y))      #fx to scale up or down object. transform, dimension, placement
        pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)  #arguments - window, colour, dimension, thickness
        self.hitbox = (self.x + 8, self.y+10, self.width - 18, self.height-15) #x-start, y-start, length, breath

class stumper(object):      **class for obstacle 2**
    def __init__(self, x, y, width, height):

        self.hitbox = (self.x + 8, self.y+10, self.width - 18, self.height-20)     


    def draw(self,win):
        self.hitbox = (self.x + 8, self.y+10, self.width - 18, self.height-20)
        pygame.draw.rect(win, (255,0,0), self.hitbox, 2)
        win.blit(pygame.transform.scale(self.walkLeft[self.count//1], (75,200)), (self.x,self.y))


def redrawGameWindow():
    global walkCount
    win.blit(bg, (bgX,0))  # This will draw our background image at (0,0)
    win.blit(bg, (bgX2,0))  # This will draw our background image at (1024,0)
    drag.draw(win)

    for obstacle in obstacles:
        obstacle.draw(win)

    pygame.display.update()

speed = 30             **assign variable for fps**
drag = player(95, 396//2, 75, 73)       #class for dragon, unnecessary
stump = stumpy(810, 310, 75, 200)     #class for stump, unnecessary
Istump = stumper(810, -20, 75, 200)


obstacles= []
#main loop
run = True
while run:
    redrawGameWindow()
    pygame.time.delay(25)

    for obstacle in obstacles:

        if drag.hitbox[1] < Istump.hitbox[1] + Istump.hitbox[3] and drag.hitbox[1] + drag.hitbox[3] > stump.hitbox[1]:      #must create player instance for this to work so using class is useless
            if drag.hitbox[0] + drag.hitbox[2] > Istump.hitbox[0] and drag.hitbox[0] < Istump.hitbox[0] + Istump.hitbox[2]:
                 if drag.hitbox[0] + drag.hitbox[2] > stump.hitbox[0] and drag.hitbox[0] < stump.hitbox[0] + stump.hitbox[2]:
                     drag.hit()
                     obstacles.append(stumpy(250, 250, 100, 100))



    redrawGameWindow()

pygame.quit()

在您的播放器 class 中,使用 pygame 矩形作为您的碰撞框:

self.hitbox = pygame.Rect(self.x + 5, self.y +3, 67, 65)

然后对你的物体做同样的事情,这样玩家和障碍物都是 pygame.Rect 物体。要检测玩家是否处于障碍物中,您只需使用:

if player.hitbox.colliderect(obstacle.hitbox):
    print('hit')