如何在pygame中更改背景图像?

时间:2016-02-25 07:39:17

标签: python pygame

这是我的代码:

def bathRoom():
    global drag, a, b, c, d, e, f, g, h, i, j
    bg_bathroom = True
    bathroom = pygame.image.load("pou_bathroom.jpg")
    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    gameDisplay.blit(bathroom,[0,0])
    gameDisplay.blit(pivi,[200,80])
    gameDisplay.blit(soap,(a, b))
    gameDisplay.blit(shower,(c,d))
    gameDisplay.blit(shop,(e,f))


    if click[0] == 1 and a + soapWidth > mouse[0] > a and b + soapHeight > mouse[1] > b:
        drag == 1      
        a = mouse[0] - (soapWidth / 2)   
        b = mouse[1] - (soapHeight / 2)


    if click[0] == 1 and c + showerWidth > mouse[0] > c and d + showerHeight > mouse[1] > d: 
        drag == 1
        c = mouse[0] - (showerWidth / 2)
        d = mouse[1] - (showerHeight / 2)


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

        mainScreen()
        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        if click[0] == 1 and x + bathWidth > mouse[0] > x and y + bathHeight > mouse[1] > y: 
        bathRoom()
        pygame.display.update()

这是我尝试更改背景的部分。 单击图像后,我需要显示一个新的背景,但它只显示0.5秒。我需要它,然后我会再次改变它。

main_loop()

1 个答案:

答案 0 :(得分:0)

首先,这里的相关代码带有固定缩进
其次,我假设gameDisplay = pygame.display.set_mode((800, 600))

def bathRoom():
    bathroom = pygame.image.load("pou_bathroom.jpg")
    gameDisplay.blit(bathroom,[0,0])
    gameDisplay.blit(pivi,[200,80])
    gameDisplay.blit(soap,(a, b))
    gameDisplay.blit(shower,(c,d))
    gameDisplay.blit(shop,(e,f))

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

        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        if click[0] == 1 and x + bathWidth > mouse[0] > x and y + bathHeight > mouse[1] > y: 
            bathRoom()
            pygame.display.update()

由于其他对象可能不会与背景重叠,因此我也会将其删除,以使代码更易于阅读。

然后我要将pygame.display.update()切换为pygame.display.flip(),因为这实际上会重新呈现" blitted"图像完全。

我也将bathroom = ...移到了函数之外,因为当你真的不需要时,这会给HW带来压力。

单独的东西应该有效:

bathroom = pygame.image.load("pou_bathroom.jpg")

def bathRoom():
    global bathroom
    gameDisplay.blit(bathroom,[0,0])

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

        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        if click[0] == 1 and x + bathWidth > mouse[0] > x and y + bathHeight > mouse[1] > y: 
            bathRoom()
            pygame.display.flip()

另请考虑更改您正在工作的IDE 它会在行尾留下文物并破坏你的缩进。

以下是手册中的引文:

  

pygame.display.flip - 将完整的显示Surface更新到屏幕   pygame.display.update - 更新软件显示的部分屏幕

update 应该工作,但通常除非您想要更新小区域,否则请使用flip,因为它在某些时候更可靠,更真实地更快(甚至他们声称不应该这样做。