简体   繁体   English

如何从一个函数中获取一个值并在另一个函数中使用它?

[英]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.

相关问题 python - 如何在不使用返回的情况下从一个函数中获取信息以在python中的另一个函数中使用? - How do I take information from one function to use in another in python without using return? 我如何从一个 function 中取出字典并在另一个 python 中使用它? - How do I take a dictionary from one function and use it in another in python? 如何获取一个程序的输出并将其用作另一个程序的输入? - How do I take the output of one program and use it as the input of another? 如何在另一个 function 的输入中使用? - How do I use in input from one function in another? 如何在同一类的另一个函数中使用一个函数中的一个对象 - How do I use one object from one function in another function in the same class 如何在另一个函数中使用一个函数收集的数据 - How do I use the data collected of one function in another function 如何在一个 function 中将变量的值从一个 IF 传递到另一个 IF? - How do I pass the value of a variable from one IF to another, inside one function? 如何将一个 function 的 output 用于另一个 function? - How do I use the output of one function for another function? 你如何使用一个function的output作为另一个输入值? - How do you use the output of one function, as an inputted value in another? 如何在python中的另一个函数中使用一个函数中的数据? - How do I use data from one function in another function in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM