【问题标题】:Python: How to Switch Case with Function Call for Pygame DisplayPython:如何使用 Pygame 显示的函数调用切换大小写
【发布时间】:2021-08-15 08:17:52
【问题描述】:

我正在尝试做一个简单的应用程序,用不同的参数调用相同的函数。

这是一个sn-p的代码:

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text, size, x, y): 
    #print(pygame.font.get_fonts())
    font = pygame.font.Font(None, size) 
    text_surface, text_rectangle = text_objects(text, font) 
    text_rectangle.center = (x, y) 
    gameDisplay.blit(text_surface, text_rectangle) 

def countdown(count_case):
    
    print("Checking Case")
    print(count_case)

    switcher={
            1: message_display("starting in 5", 40, display_width / 2, (display_height + 400) / 2),
            2: message_display("starting in 4", 40, display_width / 2, (display_height + 400) / 2),
            3: message_display("starting in 3", 40, display_width / 2, (display_height + 400) / 2),
            4: message_display("starting in 2", 40, display_width / 2, (display_height + 400) / 2),
            5: message_display("starting in 1", 40, display_width / 2, (display_height + 400) / 2)
            }
    func = switcher.get(count_case,"Invalid Countdown")
    return func()

我能够初始化 Pygame 屏幕并通过检查 print()count_case 正确传递给 countdown() 函数。

但是,我主要不知道根据count_case 的值正确调用和执行message_display 函数的语法; 1 到 5。提前致谢。

【问题讨论】:

    标签: python function pygame switch-statement


    【解决方案1】:

    表达式

    message_display("starting in 5", 40, display_width / 2, (display_height + 400) / 2)
    

    是一个函数调用。因此,您实际上将 message_display 的返回值存储在字典中。只需存储一个带有参数的元组:

    switcher = {
        1: ("starting in 5", 40, display_width / 2, (display_height + 400) / 2),
        2: ("starting in 4", 40, display_width / 2, (display_height + 400) / 2),
        3: ("starting in 3", 40, display_width / 2, (display_height + 400) / 2),
        4: ("starting in 2", 40, display_width / 2, (display_height + 400) / 2),
        5: ("starting in 1", 40, display_width / 2, (display_height + 400) / 2)
    }
    

    使用星号 (*) 运算符 ("unzip") 调用函数:

    message_display(*switcher[count_case])
    

    countdown函数:

    def countdown(count_case):
        
        print("Checking Case")
        print(count_case)
    
        switcher={
            1: ("starting in 5", 40, display_width // 2, (display_height + 400) // 2),
            2: ("starting in 4", 40, display_width // 2, (display_height + 400) // 2),
            3: ("starting in 3", 40, display_width // 2, (display_height + 400) // 2),
            4: ("starting in 2", 40, display_width // 2, (display_height + 400) // 2),
            5: ("starting in 1", 40, display_width // 2, (display_height + 400) // 2)
        }
        return message_display(*switcher[count_case])
    

    【讨论】:

      【解决方案2】:

      您根本不需要 dict/switch。您可以改为根据输入格式化显示的字符串

      def countdown(count_case):
          if count_case < 1 or count_case > 5:
              raise ValueError("count_case must be between 1 and 5")
          return message_display(f"starting in {6 - count_case}", 40, display_width / 2, (display_height + 400) / 2)
      

      【讨论】:

      • 嗨@Rabbid76 和lain Shelvington,非常感谢您在教我有关逻辑错误时的及时回复,非常感谢。但是随着 Rabbid76 对修复开关盒的回答更多,我将把他作为这个开关盒线程的官方答案,尽管如此,非常感谢 lain Shelvington 提供了一个不错的选择,应该想到那个 tooz ??
      猜你喜欢
      • 2020-09-28
      • 1970-01-01
      • 2013-09-26
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 2021-10-04
      • 2012-12-05
      相关资源
      最近更新 更多