简体   繁体   中英

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. 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 = screen_text('Play',35,400,300)

Later you can use the play variable. eg:

screen.blit(play, (w1+10, h1+10))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM