pythonpygameattributesrect

Python 3.9.5 Pygame "AttributeError: 'pygame.Rect' object has no attribute 'pygame'"


i am doing a python game and need to make a game menu.

when i run my code i get this error:
AttributeError: 'pygame.Rect' object has no attribute 'pygame'

def main():
    screen.fill(fill_color)
    flag = True
    clock = pygame.time.Clock()
    menu = Menu(20, 70, 30, 'hello!', (255, 0, 0))
    while flag:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
        menu.drow()
        if event == pygame.MOUSEBUTTONDOWN:
            mouse_pos = event.pos
        if menu.pos.pygame.Rect.collidepoint(mouse_pos):
           

here is menu.draw():

        font = pygame.font.SysFont('fonts/crackman.ttf', self.fs)
        text = font.render(self.text, False, self.color)
        screen.blit(text, (round(self.pos_x), round(self.pos_y)))

Solution

  • collidepoint is a method of a pygame.Rect object. You have to crate a pygame.Rect object from the text:

    self.rect = text.get_rect(topleft = (round(self.pos_x), round(self.pos_y)))
    

    Use the rectangle for the collision detection:

    if menu.rect.pygame.Rect.collidepoint(mouse_pos):#

    if menu.rect.collidepoint(mouse_pos):