Zouhair
Zouhair

Reputation: 43

Blitting an image in pygame immediately disappears after I release my mouse

def options():
options = True

while options:
    for event in pygame.event.get():
        win.fill(WHITE)
        win.blit(background, (0, 0))

        ... # Blitting text and switch buttons

        options_x, options_y = pygame.mouse.get_pos()

        if event.type == pygame.QUIT:
            # Exit button
            pygame.QUIT()
            quit()

        elif event.type == pygame.MOUSEBUTTONDOWN:
            # Sound effect tuning
            if 470 > options_x > 390 and 220 > options_y > 185: 
                # Checking if mouse click is on the ON SWITCH
                mouse_click.play()
                screen.blit(off_switch, (off_switch_x, off_switch_y))

                pygame.mixer.stop() 
                # But doesn't stop sound from playing when I quit options section

            # Music effect tuning
            elif 470 > options_x > 390 and 300 > options_y > 260:
                # Checking if mouse click is on the ON SWITCH
                mouse_click.play()
                screen.blit(off_switch, (off_switch_x, off_switch_y))
                pygame.mixer.music.stop()

            # Interactive BACK button
            elif 90 > options_x > 42 and 75 > options_y > 25:
                mouse_click.play()
                options = False

        pygame.display.update()

So this is a part of my HANGMAN game where I try to set up the OPTIONS section which allows you to configure the volume.

The problem is in the "Music" and "Sound" effect tuning.
Pressing on the ON SWITCH button for both "Music" and "Sound" will display the OFF SWITCH, but as soon as I release my mouse, they go back to their original state.
The music stops, but not the sound effects (mouse clicks, plop sounds, etc...).

I would like the image blitting to be saved, and the sounds effects to be stopped. How can I fix this?

Upvotes: 0

Views: 184

Answers (1)

Mike67
Mike67

Reputation: 11342

You undo the blit in every loop. At the top I see code which clears\resets everything.

win.fill(WHITE)
win.blit(background, (0, 0))
....

You blit the change inside the event handler:

screen.blit(off_switch, (off_switch_x, off_switch_y))

The event switch blit will be cleared at the next event loop (probably mouse move).

Think of the game as a series of states. The code at the top of the loop should draw the current state of the game.

for event in pygame.event.get():
    win.fill(WHITE)
    win.blit(background, (0, 0))
    if SwitchIsOn: 
        screen.blit(on_switch, (on_switch_x, on_switch_y))
    else:
        screen.blit(off_switch, (off_switch_x, off_switch_y)) 
    ....

The event handler should be used to change the game state.

if 470 > options_x > 390 and 220 > options_y > 185: 
    # Checking if mouse click is on the ON SWITCH
    mouse_click.play()
    SwitchIsOn = not SwitchIsOn # reverse switch position

This will prevent your event changes from being cleared.

Upvotes: 1

Related Questions