[英]How do I take a value from one function and use it in another?
How do I take the inputted text如何获取输入的文本
screen_text('Zombie Game',75,400,100)
play_but=screen_but(365,440,280,320)
play=screen_text('Play',35,400,300)
used later using these functions稍后使用这些函数使用
def text_objects(text,font):
text_surface = font.render(text,True,RED)
return text_surface,text_surface.get_rect()
def screen_text(text,size,posx,posy):
_text = pygame.font.Font('freesansbold.ttf',size)
text_surf,text_rect = text_objects(text,_text)
text_rect.center = ((posx),(posy))
screen.blit(text_surf,text_rect)
and use that inputted text, so I can superimpose it to the button并使用输入的文本,所以我可以将它叠加到按钮上
def screen_but(w1,w2,h1,h2):
while True:
for ev in pygame.event.get():
if ev.type == pygame.MOUSEBUTTONUP:
if w1 <= mouse[0] <= w2 and h1 <= mouse[1] <= h2:
return
mouse = pygame.mouse.get_pos()
if w1 <= mouse[0] <= w2 and h1 <= mouse[1] <= h2:
pygame.draw.rect(screen,GREY,[w1,h1,(w2-w1),(h2-h1)])
else:
pygame.draw.rect(screen,BLACK,[w1,h1,(w2-w1),(h2-h1)])
screen.blit(,(w1+10,h1+10))
pygame.display.update()
Without the screen.blit I can hover over where the rect is and the highlighting of the button works, however I just can't seem to find a way to allow the text to be superimposed onto it without taking it out of the functions.如果没有 screen.blit,我可以将鼠标悬停在 rect 所在的位置并且按钮的突出显示有效,但是我似乎无法找到一种方法来允许将文本叠加到它上面而不将其从函数中移除。 But I want to be able to use it more than just the once in my program.
但我希望能够在我的程序中多次使用它。
Return the Sprite from the function:从函数返回精灵:
def screen_text(text,size,posx,posy):
# [...]
return text_surf
Call the function and store the return value in a variable ( play
):调用该函数并将返回值存储在变量 (
play
) 中:
play = screen_text('Play',35,400,300)
Later you can use the play
variable.稍后您可以使用
play
变量。 eg:例如:
screen.blit(play, (w1+10, h1+10))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.