不确定为什么如果语句不起作用.(pygame)



好吧,现在当我按" start.png"的e按E时,它会跳到" cele.png"时,当我希望它转到" new.png"时。我希望它从" start.png"开始,然后当您按E时,它应该更改为" new.png",如果您再次按E,而背景为" new.png",则应更改为" cele.png",谢谢!

#import library
import pygame
from pygame.locals import *
 
#initialize the game
pygame.init()
width, height = 1000, 800
screen=pygame.display.set_mode((width, height))
pygame.display.set_caption("Pygame Window")
 
#load images
bg_filename = "start.png"
background = pygame.image.load(bg_filename)
#keep looping through
while 1:
    #clear the screen before drawing it again
    screen.fill(0)
    #draw the screen elements
    screen.blit(background, (0,0))
    #update the screen
    pygame.display.flip()
    #loop through the events
    for event in pygame.event.get():
        #start to new
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_e:
                if bg_filename == "start.png":
                    bg_filename = "new.png"
                    background = pygame.image.load(bg_filename)
        #new to cele 
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_e:
                if bg_filename == "new.png":
                    bg_filename = "cele.png"
                    background = pygame.image.load(bg_filename)
        #check if the event is the X button 
        if event.type==pygame.QUIT:
            #if it is quit the game
            pygame.quit() 
            exit(0)

表面没有检索文件名的对象,但是也许您可以使用另一个变量保存文件名?

#load images
bg_filename = "start.png"
background = pygame.image.load(bg_filename)
#keep looping through
while 1:
    #clear the screen before drawing it again
    screen.fill(0)
    #draw the screen elements
    screen.blit(background, (0,0))
    #update the screen
    pygame.display.flip()
    #loop through the events
    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_e:
                if bg_filename == "start.png":
                    bg_filename = "new.png"
                    background = pygame.image.load(bg_filename)

最新更新