微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Pygame 中运行游戏时更改背景?

如何解决如何在 Pygame 中运行游戏时更改背景?

我希望每 15 秒左右让我的背景在白天和黑夜之间交替(精灵),但我希望这仅在游戏运行时发生(真),我已经将它放置在我的每个地方循环,似乎无法让它继续。我们正在查看的代码如下:

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

以下是它如何适合我的游戏:

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,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,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))
    
    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))
        """

    else:
        screen.blit(game_over_screen,0))

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

解决方法

添加一个变量来指示要绘制的背景:

bg_images = [bg_image,bg_image2]
bg_index = 0

在发生 BACKCHANGE 事件时更改背景索引:

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

blit 应用程序循环中的当前背景:

while True:
    # [...]

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

    # [...]

应用循环:

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))

        # [...]

    else:
        screen.blit(game_over_screen,0))

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。