2

Making a button for a menu screen. I had to adapt from an example online so I could use a custom image to work as a button. Rather than using,

play_button = pygame.Rect(50,100,200,50)

Heres what I have so far,

def main_menu():
    while True:
        #menu background
        menu_background = pygame.image.load('game graphics/menu.png')
        menu_background = pygame.transform.scale(menu_background, (screen_w,screen_h)).convert()
        screen.blit(menu_background,(0,0))
        #menu buttons
        mx, my = pygame.mouse.get_pos()
        
        play_button = pygame.image.load('game graphics/play_button.png').convert_alpha()
        play_button = pygame.transform.scale(play_button, (400,300))
        # play_button = pygame.Rect(50,100,200,50)
        screen.blit(play_button,(-50,200))
        
        
        if play_button.collidepoint((mx,my)):
            if click:
                game()

The error im getting is,

AttributeError: 'pygame.Surface' object has no attribute 'collidepoint'

All I want to do is have the image I have selected used as a button. What am I doing wrong?

wise1
  • 25
  • 2
  • As the error suggests, `pygame.Surface` has no function `collidepoint`, `pygame.Rect` does. Your button is only a texture and does not have collision. – B Remmelzwaal Jan 27 '23 at 16:38

1 Answers1

0

collidepoint does not exist in the pygame documentation for surface. This is because rect is basically the hitbox, so it would be the thing you need to collide with. here is the pygame documentationPygame documentation the documentation: https://www.pygame.org/docs/