Python Pygame查询-精灵碰撞不工作?

Python Pygame查询-精灵碰撞不工作?,python,pygame,Python,Pygame,因此,在我的pygame游戏中,我最终设法消除了错误,但碰撞仍然不起作用。我想让玩家和方块碰撞。这是精灵的代码 class Player(pygame.sprite.Sprite): def __init__(self, x, y, image): pygame.sprite.Sprite.__init__(self) self.image = pygame.image.load('Tri.png') self.image = pygam

因此,在我的pygame游戏中,我最终设法消除了错误,但碰撞仍然不起作用。我想让玩家和方块碰撞。这是精灵的代码

class Player(pygame.sprite.Sprite):

    def __init__(self, x, y, image):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load('Tri.png')
        self.image = pygame.transform.scale (self.image, (int(width/16), int(width/15)))
        self.rect = self.image.get_rect()
        self.width, self.height = self.image.get_size()
        self.rect.x = x
        self.rect.y = y


    def update(self):
        mx, my = pygame.mouse.get_pos()
        self.rect.x = mx - self.width/2
        self.rect.y = (height * 0.8)

        if self.rect.x <= 0 - self.width/2 + 10:    
            self.rect.x += 10
        if self.rect.x + self.width >= width:
            self.rect.x = width - self.width

        self.rect.topleft = self.rect.x, self.rect.y


    def draw(self, screen):

        if bgColour == black:
            self.image = pygame.image.load('Tri2.png')
            self.image = pygame.transform.scale (self.image, (int(width/16), int(width/15)))
        else:
            self.image = pygame.image.load('Tri.png')
            self.image = pygame.transform.scale (self.image, (int(width/16), int(width/15)))
        self.width, self.height = self.image.get_size()

        gameDisplay.blit(self.image, self.rect)
        self.rect.topleft = self.rect.x, self.rect.y




#Class for the squares to dodge
class Square(pygame.sprite.Sprite):
    def __init__(self, box_x, box_y, box_width, box_height,colour, box_speed, box_border, BC):
        pygame.sprite.Sprite.__init__(self)
        self.box_x = box_x
        self.box_y = box_y
        self.box_width = box_width
        self.box_height = box_width
        self.colour = colour
        self.box_speed = box_speed
        self.box_border = box_border
        self.BC = BC

        self.image = pygame.Surface((self.box_width, self.box_width))
        self.image.fill(self.BC)
        draw = pygame.draw.rect(self.image, self.BC, [self.box_x - self.box_border/2, self.box_y - self.box_border/2, self.box_width + self.box_border, self.box_height + self.box_border])
        box = pygame.draw.rect(gameDisplay, self.colour, [self.box_x, self.box_y, self.box_width, self.box_height])
        self.rect = self.image.get_rect()


    def Fall(self):

        if self.box_y < height:
            self.box_y += box_speed
        elif self.box_y > height + 100:
            del square[0]

        border = pygame.draw.rect(gameDisplay, self.BC, [self.box_x - self.box_border/2, self.box_y - self.box_border/2, self.box_width + self.box_border, self.box_height + self.box_border])
        box = pygame.draw.rect(gameDisplay, self.colour, [self.box_x, self.box_y, self.box_width, self.box_height])
        self.rect.topleft = self.box_x, self.box_y

问题是每次生成新的Square时,都会创建两个不同的
Square
实例。将第一个添加到
方形
列表中,并将第二个添加到
块组
精灵组中。然后,仅更新
方形
列表中的精灵,而不更新
块组
中的精灵,但这些精灵用于碰撞检测

要修复此错误,请删除
square
列表,只使用
blocksGroup


您还可以为
Square
类提供一个
update
方法,该方法允许您通过调用
blocksGroup.update()
来更新所有包含的精灵。要绘制精灵,只需调用blocksGroup.draw(gameDisplay)

即可。问题是,每次生成一个新的方块时,都会创建两个不同的
Square
实例。将第一个添加到
方形
列表中,并将第二个添加到
块组
精灵组中。然后,仅更新
方形
列表中的精灵,而不更新
块组
中的精灵,但这些精灵用于碰撞检测

要修复此错误,请删除
square
列表,只使用
blocksGroup


您还可以为
Square
类提供一个
update
方法,该方法允许您通过调用
blocksGroup.update()
来更新所有包含的精灵。要绘制精灵,只需调用blocksGroup.draw(gameDisplay)

欢迎使用堆栈溢出。请尽量将代码减少到绝对值,删除与问题无关的所有内容,但确保代码是可执行的,并且错误仍然可以重现。并详细解释所需的行为和错误。欢迎使用堆栈溢出。请尽量将代码减少到绝对值,删除与问题无关的所有内容,但确保代码是可执行的,并且错误仍然可以重现。并详细解释期望的行为和错误。
def game_loop():

    mx, my = pygame.mouse.get_pos()
    x = mx
    y = (height * 0.8)
    player = Player(x, y, 'Tri.png')

    box_width = int(width/15)
    blocksGroup = pygame.sprite.Group()

    if round(box_width/5) % 10 == 0:
        box_border = round(box_width/5)
    else:
        box_border = round(box_width/5 + 1)

    box_x = random.randrange(0, width)
    box_y =  0 - box_width

    min_gap = box_width/4

    global box_speed

    box_col = False
    box_start = random.randrange(0, width)
    delay = 0

    global square
    square = []



    move_speed = 10


    #level variables
    box_speed = 6
    max_gap = box_width/2
    score = 0
    bgColourList = [white, black, white, white]
    global bgColour
    bgColour = bgColourList[0]
    Blist = [red, green, black, pink, white]
    BC = Blist[0]
    Clist = [red, black, black, pink, white]
    box_colour = red
    text_colour = black
    z = 60
    level = 0
    delayBG = 0
    levelChange = 400
    gameExit = False

    #while still inside of game

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    gameExit = True

        gameDisplay.fill(bgColour)
        player.update()
        player.draw(gameDisplay)
        #dodgers.checkCollision()




        if score % levelChange == 0:
            delayBG = 120
            z = 120

        if delayBG == 0:
            bgColour = bgColourList[level]
            BC = Blist[level]
            box_colour = Clist[level]


        if delay == 0:
            score += 1
            delay += 3
            if delayBG == 0:
                level += 1
                box_speed += 1
                max_gap -= 1



        #print(m_x)
        if z == 0:            
            new = random.randint(0, width)
            square.append(Square(new, box_y, box_width, box_width , box_colour, box_speed, box_border, BC))
            steven = Square(new, box_y, box_width, box_width , box_colour, box_speed, box_border, BC)
            blocksGroup.add(steven)
            print(player, blocksGroup)
            z = random.randint(int(min_gap), int(max_gap))
            last = new
            lasty = box_y

        for i in square: 
            i.Fall()
            """tris.remove(i)
            i.checkCollision(tris)
            tris.add(i)"""






        pygame.draw.rect(gameDisplay, bgColour, [0,0, width, int(height/23)])
        message_to_screen(str(score), text_colour, -height/2 + 15, 0)
        collide = pygame.sprite.spritecollide(player, blocksGroup, False)
        if collide:
            gameExit = True
            print("Dead")



        delayBG -= 1
        z -= 1

        delay -= 1
        pygame.display.update()
        clock.tick(FPS)
game_loop()
pygame.quit()
quit()