【问题标题】:helper function or encapsulation works better?辅助功能或封装效果更好?
【发布时间】:2018-09-30 07:59:18
【问题描述】:

这段代码是重复的,我应该使用辅助函数来调用这个函数还是封装它?

if exitcode==0:
   pygame.font.init()
   font = pygame.font.Font(None, 24)
   text = font.render("Accuracy: "+str(accuracy)+"%", True, (255,0,0))
   textRect = text.get_rect()
   textRect.centerx = screen.get_rect().centerx
   textRect.centery = screen.get_rect().centery+24
   screen.blit(gameover, (0,0))
   screen.blit(text, textRect)
else:
   pygame.font.init()
   font = pygame.font.Font(None, 24)
   text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0))
   textRect = text.get_rect()
   textRect.centerx = screen.get_rect().centerx
   textRect.centery = screen.get_rect().centery+24
   screen.blit(youwin, (0,0))
   screen.blit(text, textRect)

唯一的区别是 if-else 语句的倒数第二行 screen.blit(youwin / youlose, (0,0))text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0) / (255,0,0))

这是我使用辅助函数所做的,但它不会运行:

if exitcode==0:
   initialize_game()
   text = font.render("Accuracy: "+str(accuracy)+"%", True, (255,0,0))
   produce_text_on_screen()
else:
   initialize_game()
   text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0))
   produce_text_on_screen()

def initialize_game():
    pygame.font.init()
    font = pygame.font.Font(None, 24)

def produce_text_on_screen():
   textRect = text.get_rect()
   textRect.centerx = screen.get_rect().centerx
   textRect.centery = screen.get_rect().centery+24
   screen.blit(gameover, (0,0))
   screen.blit(text, textRect)

NameError:名称“initialize_game”未定义。 一些帮助会很棒,谢谢!

【问题讨论】:

    标签: python pygame encapsulation helper


    【解决方案1】:

    在 Python 中,函数定义的顺序很重要。在底层,def 只是一个赋值,它在执行时赋值给函数名。这意味着函数定义必须在使用之前出现:

    def initialize_game():
       pygame.font.init()
       font = pygame.font.Font(None, 24)
    
    def produce_text_on_screen():
       textRect = text.get_rect()
       textRect.centerx = screen.get_rect().centerx
       textRect.centery = screen.get_rect().centery+24
       screen.blit(gameover, (0,0))
       screen.blit(text, textRect)
    
    if exitcode==0:
       initialize_game()
       text = font.render("Accuracy: "+str(accuracy)+"%", True, (255,0,0))
       produce_text_on_screen()
    else:
       initialize_game()
       text = font.render("Accuracy: "+str(accuracy)+"%", True, (0,255,0))
       produce_text_on_screen()
    

    或者更准确地说,定义并不总是必须在源代码中排在第一位,它们只需要先执行,以便在使用时定义名称。

    【讨论】:

    • 嗯,好的,谢谢!自从我用 Python 编码以来已经有一段时间了。所以,我有点想知道如何做到这一点!
    【解决方案2】:

    您可以使用三元运算符 (value1 if condition else value2):

    result = gameover if exitcode == 0 else youwin
    color = (255, 0, 0) if exitcode == 0 else (0, 255, 0)
    
    pygame.font.init()
    font = pygame.font.Font(None,  24)
    text = font.render("Accuracy: " + str(accuracy) + "%",  True,  color)
    textRect = text.get_rect()
    textRect.centerx = screen.get_rect().centerx
    textRect.centery = screen.get_rect().centery + 24
    screen.blit(result,  (0, 0))
    screen.blit(text,  textRect)
    

    当然,制作函数绝不是一个坏主意。

    def show_final_screen(result, color, accuracy):
        # ...
    
    if exitcode == 0:
        show_final_screen(gameover, (255, 0, 0), accuracy)
    else
        show_final_screen(youwin, (0, 255, 0), accuracy)
    

    【讨论】:

    • 这对我来说是新事物,所以在语法上它与: if exitcode == 0 : gameover else: youwin ?
    • 基本上,是的。比较:docs.python.org/3/reference/…
    猜你喜欢
    • 2023-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 2016-06-03
    • 1970-01-01
    相关资源
    最近更新 更多