![](/img/trans.png)
[英]How do I take information from one function to use in another in python without using return?
[英]How do I take a value from one function and use it in another?
如何獲取輸入的文本
screen_text('Zombie Game',75,400,100)
play_but=screen_but(365,440,280,320)
play=screen_text('Play',35,400,300)
稍后使用這些函數使用
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)
並使用輸入的文本,所以我可以將它疊加到按鈕上
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()
如果沒有 screen.blit,我可以將鼠標懸停在 rect 所在的位置並且按鈕的突出顯示有效,但是我似乎無法找到一種方法來允許將文本疊加到它上面而不將其從函數中移除。 但我希望能夠在我的程序中多次使用它。
從函數返回精靈:
def screen_text(text,size,posx,posy):
# [...]
return text_surf
調用該函數並將返回值存儲在變量 ( play
) 中:
play = screen_text('Play',35,400,300)
稍后您可以使用play
變量。 例如:
screen.blit(play, (w1+10, h1+10))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.