Why does background revert back to normal?

Viewed 27

I am writing a code for a video game where a character has to walk up to a door and click on it for the background to change. The default value is where the background starts as 'bg_1' or bedroom, and then the next one being 'bg_2' or the kitchen. the code actually works to the point that when I walk up to the door and click on it, the background changes, but when I release the mouse, the second background stays on the screen, and the first original background is blitted on top of it. here's my code:

import pygame
pygame.init()
#door_open = pygame.mixer.Sound('put name here.ogg')
#music = pygame.mixer.music.load('put name here.oog')
win = pygame.display.set_mode((600, 600))
work = pygame.image.load('bernie front.jpg')
work = pygame.transform.scale(work, (100, 160))
work_2 = pygame.image.load('block.png')
pygame.display.set_caption("Altoria")
mc_front = pygame.image.load('bernie front.jpg')
mc_front = pygame.transform.scale(mc_front, (100, 160))
block = pygame.image.load('block.png')
block = pygame.transform.scale(block,(100,60))
bg = pygame.image.load('bg.png')
bg = pygame.transform.scale(bg,(600,600))
bg_2 = pygame.image.load('kitchen.png')
bg_2 = pygame.transform.scale(bg_2, (600,600))
block_x = 178
block_y = 490
x = 50
y = 300
width = 40
height = 60
vel = 20
black = (255, 255, 255, 255)
white = (0, 0, 0, 0)

run = True
while run:
    win.blit(block, [block_x, block_y])
    win.blit(mc_front, (x, y))
    pygame.display.update()
    pygame.time.delay(100)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    keys = pygame.key.get_pressed()
    mouse = pygame.mouse.get_pos()

    if keys[pygame.K_a] and x > vel:
        x -= vel
    if keys[pygame.K_d] and x < 500 - vel - width:
        x += vel
    if keys[pygame.K_w] and y > vel:
        y -= vel
    if keys[pygame.K_s] and y < 500 - height - vel:
        y += vel

    room_1 = True
    room_2 = False

    if room_1:
        win.blit(bg, [0,0])

    if room_2:
        win.blit(bg_2, [0,0])

    if keys[pygame.K_1]:
        room_2 = False
        room_1 = True
        win.fill(pygame.Color('black'))
        win.blit(bg, [0,0])
        pygame.display.update()

    if keys[pygame.K_2]:
        room_2 = True
        room_1 = False
        win.fill(pygame.Color('white'))
        win.blit(bg_2, [0,0])
        pygame.display.update()

    if 150+100 > mouse[0] > 150 and 450 + 50 > mouse[1] > 450 and x == 110 and y == 60 and event.type == pygame.MOUSEBUTTONDOWN:
        win.fill(pygame.Color('white'))
        win.blit(bg_2, [0,0])
        pygame.display.update()
        #pygame.mixer.Sound.play(crash_sound)

    if 150+100 > mouse[0] > 150 and 450 + 50 > mouse[1] > 450 and x == 110 and y == 60:
        print('action in line')

    win.blit(mc_front, (x, y))
    pygame.display.update() 
pygame.quit()

any help would be greatly appreciated, thanks! :)

1 Answers

The values of the variables room_1 and room_2 are continuously set in the application loop. You have to initialize the variables, before the loop and to change them in the loop:

# initialize room_1 and room_2 
room_1 = True
room_2 = False

run = True
while run:  

    # [...]

    # room_1 = True  <--- DELETE
    # room_2 = False <--- DELETE

    # [...]

    if keys[pygame.K_1]:
        # change room_1 and room_2
        room_2 = False  
        room_1 = True

        # [...]        

    if keys[pygame.K_2]:
        # change room_1 and room_2
        room_2 = True
        room_1 = False

        # [...]
Related