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

Python 在Pygame中运行游戏时,如何更改背景?,python,python-3.x,pygame,Python,Python 3.x,Pygame,我想让我的背景每隔15秒左右在白天和黑夜之间交替(精灵),但我希望这只发生在游戏运行时(真的),我把它放在我的while循环中的每个地方,似乎无法让它运行。我们正在查看的代码如下所示: if event.type == BACKCHANGE: screen.blit(back_change,(0,0)) else: screen.blit(bg_image,(0,0)) 以下是它如何融入我的游戏: import pygame, s

我想让我的背景每隔15秒左右在白天和黑夜之间交替(精灵),但我希望这只发生在游戏运行时(真的),我把它放在我的while循环中的每个地方,似乎无法让它运行。我们正在查看的代码如下所示:

if event.type == BACKCHANGE:
            screen.blit(back_change,(0,0))
        else:
            screen.blit(bg_image,(0,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,(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)
导入pygame、sys、time、random
pygame.init()
clock=pygame.time.clock()
screen=pygame.display.set_模式((500800))
pygame.display.set_标题(‘坠落的人’)
#游戏元素
重力=0.15
mov_of_man_x=0
男人的动作y=0
右_mov=50
左_mov=50
game_active=True
#日背景
bg_image=pygame.image.load(“/Users/apple/Downloads/Python Projects/granning_Game/bckwall.jpg”).convert()
bg_image=pygame.transform.scale(bg_image,(500900))
#夜背景
bg_image2=pygame.image.load(“/Users/apple/Downloads/Python Projects/granning_Game/bckwall2.jpg”).convert()
bg_image2=pygame.transform.scale(bg_image2,(500900))
#后台交换
背面换片机=[bg_图像,bg_图像2]
后退更改=随机选择(后退更改)
#球员
man=pygame.image.load(“/Users/apple/Downloads/Python Projects/granning\u Game/man.png”).convert\u alpha()
man=pygame.transform.scale(man,(51,70))
man_rec=man.get_rect(中心=(250,-500))
#玩家继续前进
platform=pygame.image.load(“/Users/apple/Downloads/Python Projects/granning\u Game/platform.png”).convert\u alpha()
平台=pygame.transform.scale(平台,(300,60))
platform_rec=platform.get_rect(中心=(250730))
#屏幕上的游戏
game\u over\u screen=pygame.image.load(“/Users/apple/Downloads/Python Projects/granning\u game/End\u screen.png”).convert\u alpha()
game\u over\u screen=pygame.transform.scale(game\u over\u screen,(500800))
#移动平台
def create_plat():
随机平台位置=随机选择(平台位置)
新建平台=平台。获取(中心=(随机平台位置,800))
返回新平台
def移动平台(平台):
对于平台中的平台:
平台中心-=4
返回平台
def绘图平台(平台):
对于平台中的平台:
屏幕。blit(平台、平台)
#碰撞检测
def检测平台(平台):
对于平台中的平台:
如果人工记录碰撞(平面):
全球男子运动
男人的动作=-4
返回
def检测人员(mans):
对于男人中的男人:
如果man_rec.top=900:
返回错误
如果man_rec.left=500:
返回错误
其他:
返回真值
#得分
def显示_分数():
通过
#移动平台
平台列表=[]
PLATMOV=pygame.USEREVENT
pygame.time.set_定时器(普拉特莫夫,1200)
平台位置=[100200300400]
#换钱
BACKCHANGE=pygame.USEREVENT
pygame.time.set_定时器(回退,1200)
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
sys.exit()
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_RIGHT:
mov_of_man_x+=右_mov
man_rec.centerx+=移动man_x
人的移动量x-=50
如果event.key==pygame.K_左:
mov_of_man_x+=左移动
man_rec.centerx-=移动man_x
人的移动量x-=50
如果event.type==PLATMOV:
平台列表。追加(创建平台())
#表面
屏幕。blit(背景图像,(0,0))
如果游戏_活动==真:
#重力
人的移动=重力
男主角+=男主角的动作
#平板
平台列表=移动平台(平台列表)
绘图平台(平台列表)
检测平台(平台列表)
游戏激活=探测人(人记录)
#性格
屏幕。blit(人,人)
"""
如果event.type==反向更改:
屏幕。blit(背面变化,(0,0))
其他:
屏幕。blit(背景图像,(0,0))
"""
其他:
blit(游戏在屏幕上(0,0))
pygame.display.update()
时钟滴答(120)

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

bg_图像=[bg_图像,bg_图像2]
bg_指数=0
发生
反向更改
事件时更改背景索引:

pygame.event.get()中事件的
:
如果event.type==反向更改:
bg_指数+=1
如果bg\U索引>=len(bg\U图像):
bg_指数=0
blit
应用程序循环中的当前背景:

为True时:
# [...]
blit(bg_图像[bg_索引],(0,0))
# [...]

应用程序循环:

bg_图像=[bg_图像,bg_图像2]
bg_指数=0
# [...]
尽管如此:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
# [...]
如果event.type==pygame.KEYDOWN:
# [...]
如果event.type==PLATMOV:
# [...]
如果event.type==反向更改:
bg_指数+=1
如果bg\U索引>=len(bg\U图像):
bg_指数=0
如果游戏_活动==真:
blit(bg_图像[bg_索引],(0,0))
# [...]
其他:
blit(游戏在屏幕上(0,0))
pygame.display.update()
时钟滴答(120)

它给了我一个不可编写脚本的错误文件“/Users/apple/Downloads/Python Projects/granning_Game/granning_Game.py”,第122行,在screen.blit(bg_image[bg_index],(0,0))类型错误:“pygame.Surface”对象不是subscriptable@Hussein
bg\u图像[bg\u索引]
->
bg\u图像[bg\u索引]
代码已修复且运行良好,除了一件小事。当我将事件时间设置为1200时,它可以无缝工作,但当我将其设置为更长的时间时,它会延迟平台的生成。我假设这是因为循环正在等待事件发生,然后继续执行其他操作