如何创建跳过功能pygame?

How to create skip functionality pygame?

我正在尝试在 pygame 中创建一种方法,让玩家只需按 "escape" 即可跳过教程阶段。但是,我似乎无法在使用以下内容时让它工作:

                for event in pygame.event.get():
                    if event.type == pygame.K_ESCAPE:
                        brek2 = True
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        run = False
                        run2 = False
                        brek = True
                if brek or brek2:
                    break
                pygame.time.delay(1000)
            if brek:
                break

如果您需要,这里是教程阶段的完整循环:

        while run2:
                # intro sequence
                pygame.event.pump()
                fade(screen_width, screen_height, BLACK)
                fade(screen_width, screen_height, WHITE)
                techi = pygame.transform.scale(pygame.image.load("Techi-Joe.gif"), (75, 75))
                bg = pygame.Surface((screen_width, screen_height))
                bg.fill(WHITE)
                win.blit(pygame.transform.scale(bg, (screen_width, screen_height)), (0, 0))
                win.blit(techi, (100, 600))
                pygame.display.update()
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        run = False
                        run2 = False
                pygame.time.delay(1000)
                brek2 = False
                presss = 1
                brek = False
                techi_bubble = pygame.transform.scale(pygame.image.load("techi_speech_bubble.png"), (700, 350))
                techi_font = pygame.font.Font('Consolas.ttf', 25)
                for a in range(len(techi_says)):
                    win.blit(pygame.transform.scale(bg, (screen_width, screen_height)), (0, 0))
                    win.blit(techi_bubble, (175, 300))
                    win.blit(techi, (100, 600))
                    writeLikeCode(techi_says[a], 235, 350, techi_font, 20)
                    for event in pygame.event.get():
                        if event.type == pygame.K_ESCAPE:
                            brek2 = True
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            run = False
                            run2 = False
                            brek = True
                    if brek or brek2:
                        break
                    pygame.time.delay(1000)
                if brek:
                    break

这里有一些附加代码被引用为函数 writeLikeCode:

techi_says = ["Hello, User!", "My name is Techi-Joe.", "I'm a programmer,", "and this is my first game:",
          "Dunk the Chief!", "in Dunk the Chief,", "your main objective", "is to dunk any US president", "you choose",
          "in a cold tub of water!", "yay!"]


# writeLikeCode function
def writeLikeCode(string, xpos, ypos, font, fontsize):
    for x in range(len(string)):
        lstring = split(string)
        text = font.render(lstring[x], 1, GREEN)
        win.blit(text, (xpos + (x * fontsize), ypos))
        pygame.time.delay(50)
        pygame.display.update()

这是拆分函数:

def split(string):
    return [char for char in string]

pygame.event.get() 不仅获取事件,还从事件队列中移除事件。如果您连续调用 pygame.event.get() 两次,那么第一次调用将检索事件,但第二次调用 returns 一个空列表。

for event in pygame.event.get(): # returns the list of events
   # [...]
for event in pygame.event.get(): # returns empty list
   # [...]

如果事件发生在 pygame.event.get() 的两次调用之间,则事件将在第二次调用时接收。但那将是一种罕见的情况。

pygame.K_ESCAPE 不是事件类型(参见 pygame.event), it is a key see (pygame.key)。
如果要检测 ESC 是否被按下,则必须检测 KEYDOWN 事件并评估 .key 属性是否为 K_ESCAPE .例如:

for event in pygame.event.get():

    if event.type == pygame.QUIT:
        run = False
        run2 = False
        brek = True

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_ESCAPE:
            brek2 = True