Hussein
Hussein

Reputation: 97

How do I make a changing background while the game is running in Pygame?

I want to have my background alternate between night and day (sprites) every 15 seconds or so, but I want this to happen only while the game is running (True), I've placed it in every place in my while loop, can't seem to get it going. The code we're looking at is the following:

if event.type == BACKCHANGE:
            screen.blit(back_change,(0,0))
        else:
            screen.blit(bg_image,(0,0))

Here's how it fits into my game:

import pygame, sys, time, random

pygame.init()
clock = pygame.time.Clock()
screen = pygame.display.set_mode((500, 800))
pygame.display.set_caption('FALLING MAN')

#Game elements
gravity = 0.15
mov_of_man_x = 0
mov_of_man_y = 0
right_mov = 50
left_mov = 50
game_active = True

#day background
bg_image = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/bckwall.jpg").convert()
bg_image = pygame.transform.scale(bg_image,(500, 900))

#night background
bg_image2 = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/bckwall2.jpg").convert()
bg_image2 = pygame.transform.scale(bg_image2,(500, 900))

#background swap
back_changer = [bg_image, bg_image2]
back_change = random.choice(back_changer)

#the player
man = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/man.png").convert_alpha()
man = pygame.transform.scale(man, (51, 70))
man_rec = man.get_rect(center = (250, -500))

#the platforms player moves on
platform = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/platform.png").convert_alpha()
platform = pygame.transform.scale(platform,(300,60))
platform_rec = platform.get_rect(center = (250,730))

#game over screen
game_over_screen = pygame.image.load("/Users/apple/Downloads/Python Projects/Climbing_Game/End_screen.png").convert_alpha()
game_over_screen = pygame.transform.scale(game_over_screen,(500,800))

#moving platforms
def create_plat():
    random_plat_pos = random.choice(plat_pos)
    new_plat = platform.get_rect(center = (random_plat_pos, 800))
    return new_plat

def mov_plat(plats):
    for plat in plats:
        plat.centery -= 4
    return plats

def draw_plat(plats):
    for plat in plats:
        screen.blit(platform, plat)

#collison detection
def detect_plat(plats):
    for plat in plats:
        if man_rec.colliderect(plat):
            global mov_of_man_y
            mov_of_man_y = -4
            return

def detect_man(mans):
    for man in mans:
        if man_rec.top <= -700 or man_rec.bottom >= 900:
            return False
        if man_rec.left <= -100 or man_rec.right >= 500:
            return False
        else:
            return True

#score
def display_score():
    pass

#moving platforms
plat_list = []
PLATMOV = pygame.USEREVENT
pygame.time.set_timer(PLATMOV, 1200)
plat_pos = [100,200,300,400]


#back change
BACKCHANGE = pygame.USEREVENT
pygame.time.set_timer(BACKCHANGE, 1200)


while True:
    for event in pygame.event.get():
        
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

        if event.type == pygame.KEYDOWN:

            if event.key == pygame.K_RIGHT:
                mov_of_man_x += right_mov
                man_rec.centerx += mov_of_man_x
                mov_of_man_x -= 50

            if event.key == pygame.K_LEFT:
                mov_of_man_x += left_mov
                man_rec.centerx -= mov_of_man_x
                mov_of_man_x -= 50
        
        if event.type == PLATMOV:
            plat_list.append(create_plat())

    #surfaces
    screen.blit(bg_image,(0,0))
    
    if game_active == True:

        #gravity
        mov_of_man_y += gravity
        man_rec.centery += mov_of_man_y

        #plats
        plat_list = mov_plat(plat_list)
        draw_plat(plat_list)
        detect_plat(plat_list)
        game_active = detect_man(man_rec)
        
        #character
        screen.blit(man, man_rec)

        """
        if event.type == BACKCHANGE:
            screen.blit(back_change,(0,0))
        else:
            screen.blit(bg_image,(0,0))
        """

    else:
        screen.blit(game_over_screen, (0,0))

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

Upvotes: 1

Views: 524

Answers (1)

Rabbid76
Rabbid76

Reputation: 210948

Add a variable that indicates which background to draw:

bg_images = [bg_image, bg_image2]
bg_index = 0

Change the background index when the BACKCHANGE event occurs:

for event in pygame.event.get():
    if event.type == BACKCHANGE:
        bg_index += 1
        if bg_index >= len(bg_images):
            bg_index = 0

blit the current background in the application loop:

while True:
    # [...]

    screen.blit(bg_images[bg_index], (0,0))

    # [...]

Application loop:

bg_images = [bg_image, bg_image2]
bg_index = 0

# [...]

while True:
    for event in pygame.event.get():   
        if event.type == pygame.QUIT:
            # [...]

        if event.type == pygame.KEYDOWN:
            # [...]
        
        if event.type == PLATMOV:
             # [...]

        if event.type == BACKCHANGE:
            bg_index += 1
            if bg_index >= len(bg_images):
                bg_index = 0

    if game_active == True:
        screen.blit(bg_images[bg_index], (0,0))

        # [...]

    else:
        screen.blit(game_over_screen, (0,0))

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

Upvotes: 1

Related Questions