Pthyon
Pthyon

Reputation: 172

Don't know where to start with integrating buttons

I'm having issues integrating working buttons into this code. The buttons have been defined and show within pygame but I don't know where to go next. I've attempted to add mouse and click functions but I really don't know where they would be best located or how to use their functions

import pygame

pygame.init()

screen = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()

BLACK = (0, 0, 0)
BACKGROUND = (200, 230, 234)
WHITE = (255, 255, 255)
HOVER_COLOUR = (50, 70, 90)
# Text Variables 
FONT = pygame.font.SysFont ("Times New Norman", 60)
TEXT = FONT.render ("", True, WHITE)
background_images = pygame.image.load("background.jpg").convert()
screen.blit(background_images, [0,0])
screen.blit(TEXT, (150, 50))
# Text & Rectangles construction
text1 = FONT.render("PlAY", True, WHITE)
text2 = FONT.render("CONTROLS", True, WHITE)
text3 = FONT.render("DIFFICULTY", True, WHITE)
text4 = FONT.render("SCOREBOARD", True, WHITE)

rect1 = pygame.Rect(250,200,300,80)
rect2 = pygame.Rect(250,300,300,80)
rect3 = pygame.Rect(250,400,300,80)
rect4 = pygame.Rect(250,500,300,80)
# The button construction arry. Text and Rectangle 
buttons = [
    [text1, rect1, BACKGROUND],
    [text2, rect2, BACKGROUND],
    [text3, rect3, BACKGROUND],
        [text4, rect4, BACKGROUND],
    ]

def game_intro():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEMOTION:
                for button in buttons:
                    # Uses collisionpoint to detect mouse position collisions
                    if button[1].collidepoint(event.pos):
                        # Set the button's colour to the hover colour.
                        button[2] = HOVER_COLOUR
                    else:
                        # resets the colour to normal.
                        button[2] = BACKGROUND

        # Draws the buttons with their current colours (normal & collisions)
        for text, rect, colour in buttons:
            pygame.draw.rect(screen, colour, rect)
            screen.blit(text, rect)

        pygame.display.flip()
        clock.tick(15)

#Run Game
game_intro()
scene_change()
pygame.quit()

When integrating this:

mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

The program kicks out an error about the mouse position function

Upvotes: 1

Views: 96

Answers (1)

Rabbid76
Rabbid76

Reputation: 210948

Add ids to your buttons:

buttons = [
    [text1, rect1, BACKGROUND, 1],
    [text2, rect2, BACKGROUND, 2],
    [text3, rect3, BACKGROUND, 3], 
    [text4, rect4, BACKGROUND, 4]
]

Add a function which handles a button event:

def on_button(button):
    print(button[3])

Call the function when the button is pressed:

def game_intro():
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return
            elif event.type == pygame.MOUSEMOTION:
                for button in buttons:
                    # Uses collisionpoint to detect mouse position collisions
                    if button[1].collidepoint(event.pos):
                        # Set the button's colour to the hover colour.
                        button[2] = HOVER_COLOUR
                    else:
                        # resets the colour to normal.
                        button[2] = BACKGROUND
            elif event.type == pygame.MOUSEBUTTONDOWN:
                for button in buttons:
                    # Uses collisionpoint to detect mouse position collisions
                    if button[1].collidepoint(event.pos):
                        on_button(button)

        # Draws the buttons with their current colours (normal & collisions)
        for text, rect, colour, button_id in buttons:
            pygame.draw.rect(screen, colour, rect)
            screen.blit(text, rect)

        pygame.display.flip()
        clock.tick(15)

Upvotes: 1

Related Questions