【问题标题】:How to create skip functionality pygame? [duplicate]如何创建跳过功能pygame? [复制]
【发布时间】:2020-03-21 05:36:34
【问题描述】:

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

                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]

【问题讨论】:

    标签: python loops pygame skip


    【解决方案1】:

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

    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),它是一个键,参见(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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多