Egoiistic Prince
Egoiistic Prince

Reputation: 17

snake game button functionality

i am making a snake game which i have completed but i m trying to make some levels in it like easy medium and hard for that purpose i have created my buttons like this

def button(msg, xposition, yposition, width, height, color, action = None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    if click[0] == 1 and action!= None:
        if action == "play":
            another_screen()
        elif action == "easy":
            gameLoop_easy()
        elif action == "medium":
            gameLoop_medium()
        elif action == "hard":
            gameLoop_hard()
        elif action == "quit":
            pygame.quit()
            quit()
    pygame.draw.rect(screen, color, (xposition, yposition, width, height))
    message(msg, black, xposition + 10, yposition +10, 50)

i have created a gameloop easy and gameloop medium and hard functions by copy pasting it and edited the lines used to increase snake speed. but it when i play the game it seems like action == "easy" is excutes only when i click on medium or hard it plays gameloop easy. Here is more code

def another_screen():
    another_screen = True
    while another_screen:
        for event in pygame.event.get():
            screen.fill(white)
            border_design()
            message('Select Level', red, 20, 100, 100)
            button("Easy", 330, 290, 200, 65, green, "easy")
            button("Medium", 330, 390, 200, 65, pink, "medium")
            button("hard", 330, 490, 200, 65, blue, "hard")
            clock.tick(15)
            pygame.display.update()

i m unable to resolve this issue. Why button is not working why respected funtions are not being executed

Upvotes: 0

Views: 331

Answers (1)

furas
furas

Reputation: 142641

Your big mistake is that you don't check if it clicks in area "xposition, yposition, width, height". You can click in any place on screen and it clicks button.

You can use pygame.Rect() to keep button's position and size and then you can use

rect.collidepoint(mouse)

to check collision. You can use rect also to draw rectangle.

I would also assign function to action= instead of text and then you can use action() instead of those if/elif

BTW: You also don't have to run fill(), buttons and other functions inside for event. You can use them after for event. Because you don't use event so you don't even need for event. But you have to use pygame.event.pump() (instead of pygame.event.get()) because without this get_pos()/get_pressed() may not work.

def button(msg, x, y, width, height, color, action=None):
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()

    rect = pygame.Rect(x, y, width, height)

    if click[0] and action and rect.collidepoint(mouse):
        action()

    pygame.draw.rect(screen, color, rect)
    message(msg, black, x+10, y+10, 50)

def another_screen():
    another_screen = True
    while another_screen:
        pygame.event.pump()            

        screen.fill(white)
        border_design()
        message('Select Level', red, 20, 100, 100)

        button("Easy", 330, 290, 200, 65, green, gameLoop_easy)
        button("Medium", 330, 390, 200, 65, pink, gameLoop_medium)
        button("hard", 330, 490, 200, 65, blue, gameLoop_hard)

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

BTW: you can use rect.collidepoint(mouse) to change button color when mouse is over button ("hover"/"unhover").

BTW: using get_pressed() you will have problem when on new screen you will have other button in the same place. It will changes screen and check button on new screen before you release mouse button - so it will click automatically in new button.

Upvotes: 1

Related Questions