Rachel Ambrose
Rachel Ambrose

Reputation: 15

Python Pygame collision rect not detected between two sprites

I'm making a game and I'm currently stuck trying to make the player to "collide" with a "coin". I've changed all my code to make them sprites with classes, but it says one of the classes has "no attribute rect" here's the player code and collide code (sorry about the formatting, been at it all day):

class Player(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface ((40, 40))
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        self.rect.center = (425), (400)
        self.movex = 0  # move along X
        self.movey = 0  # move along Y
        self.frame = 0  # count frames

    def control(self, x, y): # player movement
        self.movex += x
        self.movey += y

    def update(self):
        self.rect.x = self.rect.x + self.movex # updating the position
        self.rect.y = self.rect.y + self.movey


class Coin(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.Surface ((40, 40))
        self.image.fill(YELLOW)
        self.rect = self.image.get_rect()
        self.rect.center = (random.randint(20, 780)), (random.randint(20, 230))

    def check_collision(self, Coin, Player):
        if pygame.sprite.collide_rect(Coin, Player):
            print("collide")

And this is the main function loop:

def main():
    score_check = 0
    clock = pygame.time.Clock()
    run = True

    coin = Coin()
    PLAYER = Player()

    all_sprites = pygame.sprite.Group()
    all_sprites.add(coin, PLAYER)

    # drawing the layers of sprites
    def draw_window():
        WIN.fill(BLACK)  # background colour
        # score drawn
        global score_check
        score_print = SCORE_FONT.render("Score: " + str(score_check), 1, WHITE)
        WIN.blit(score_print, (30, 30))

        # displaying the images
        all_sprites.update()
        all_sprites.draw(WIN)
        pygame.display.update()

    while run:
        # game speed
        clock.tick(FPS)
        # draw window
        draw_window()

        for event in pygame.event.get():
            # checking quit function is pressed
            if event.type == pygame.QUIT:
                run = False
                pygame.quit()
                exit()

            coin.check_collision(Coin, Player)

Upvotes: 1

Views: 72

Answers (1)

Rabbid76
Rabbid76

Reputation: 210978

You must pass the player object to the check_collision:

Remove the Coin argument:

class Coin(pygame.sprite.Sprite):
    # [...]

    def check_collision(self, player):
        if pygame.sprite.collide_rect(self, player):
            print("collide")

Invoke check_collision in the application loop instead of the event loop:

while run:
    # game speed
    clock.tick(FPS)
    # draw window
    draw_window()

    for event in pygame.event.get():
        # checking quit function is pressed
        if event.type == pygame.QUIT:
            run = False
            pygame.quit()
            exit()

    # INDENTATION
    #<--|
    coin.check_collision(PLAYER)

Upvotes: 1

Related Questions