Python:如何使用 Pygame 显示的函数调用切换大小写
Python: How to Switch Case with Function Call for Pygame Display
我正在尝试做一个简单的应用程序,用不同的参数调用相同的函数。
这是代码片段:
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。提前致谢。
表达式
message_display("starting in 5", 40, display_width / 2, (display_height + 400) / 2)
是函数调用。因此,您实际上将 message_display
的 return 值存储在字典中。只需存储一个带有参数的元组:
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])
您根本不需要 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)
我正在尝试做一个简单的应用程序,用不同的参数调用相同的函数。
这是代码片段:
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。提前致谢。
表达式
message_display("starting in 5", 40, display_width / 2, (display_height + 400) / 2)
是函数调用。因此,您实际上将 message_display
的 return 值存储在字典中。只需存储一个带有参数的元组:
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])
您根本不需要 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)