0

I am coding a game in which the player enters its name, and I'd like to blit it on the screen.

Heres how I take the name (name is a string that is put in global in the following function):

while player_name_screen:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    print(name)
                    menu_screen = False
                    player_name_screen = False
                    game_animation = True
                    enemy_animation = True
                    standing = True
                else:
                    name += event.unicode
                    name_text = game_font.render(name, True, (125, 156, 255))
        win.blit(name_text, (500, 500))            
        pygame.display.update()
        clock.tick(fps)

And here's how I wanna blit player's name (following code is in a fonction, that is called in my main loop):

if game_animation:
        win.blit(name_text, (100, 300))

But nothing appears on the screen, and when I print this in the shell, It appears like this : <Surface(63x50x32 SW)> How can I make it blit correctly on the screen ? I looked already for similar questions but couldn't find one that answers my problem. Thanks for your help

Edit : Heres the whole thing that is needed for this to work:

name = ""
name_text = game_font.render('', True, (255, 255, 255))

def input_player_name():
    global menu_screen
    global game_animation
    global enemy_animation
    global standing
    global deluxe_or_not
    global name
    global name_text
    #print("Enter player's name")
    player_name_screen = True
    win.blit(playerNameImg, (0, 0))
    while player_name_screen:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_RETURN:
                    print(name)
                    menu_screen = False
                    player_name_screen = False
                    game_animation = True
                    enemy_animation = True
                    standing = True
                    imperial_march.play()
                    if "deluxe.ver" in name:
                        print("deluxe.ver.exe")
                        deluxe_or_not = 1
                else:
                    name += event.unicode
                    #print(name)
                    name_text = game_font.render(name, True, (125, 156, 255))
        win.blit(name_text, (500, 500))            
        pygame.display.update()
        clock.tick(fps)

def rwg():
    if game_animation:
        win.blit(name_text, (100, 300))
        print(name_text)

run = True
while run:
     for event in pygame.event.get():
          if event.type == pygame.KEYDOWN:
               if event.key == pygame.K_SPACE and menu_screen == True:
                    input_player_name()

     rwg()
bappi
  • 137
  • 9
  • `name_text` local variable or a variable in global namespace? *"following code is in a fonction"* - Where it the function? Where and how do you call the function? What are the a arguments of the function? Please read [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Rabbid76 Apr 15 '20 at 20:12

1 Answers1

0

You dont update the screen. You only update the screen in the input_player_name function, but when you get the name and is stops getting called, there is not update to update the screen.

So add pygame.display.update() at the end of the loop

run = True
while run:
     for event in pygame.event.get():
          if event.type == pygame.KEYDOWN:
               if event.key == pygame.K_SPACE and menu_screen == True:
                    input_player_name()

     rwg()
     pygame.display.update()

then you can can get rid of the one in input_player_name as the one in the loop happens very frame and you dont want to update the screen twice in a frame

The Big Kahuna
  • 2,097
  • 1
  • 6
  • 19
  • If I remove the display update in ```input_player_name```and add it after rwg(), it freezes the screen when I call the fonction for player name Edit : I forgot to say that of course display updates in the rwg function, the rest of my program (around 1k lines) is working great – bappi Apr 16 '20 at 08:56
  • @bappi My bad, didn't realise you had another loop in `input_player_name()`. Which is something i wouldn't recomend. But it works so keep the `pygame.display.update()` in the function and everything will work fine – The Big Kahuna Apr 16 '20 at 09:02
  • Dont worry, and it still doesn't work, Im just trying to blit the name and it still doesn't appear on the screen. – bappi Apr 16 '20 at 09:25
  • @bappi So, i ran your code, and it works for me, i did get rid of `and menu_screen == True:` and moved the text from `(500,500)` to `(200,200)` and it worked, i guess just make sure your screen is big enough, the background colour is not the same and `menu_screen = True`. If it still doesn't work, post all of your code. – The Big Kahuna Apr 16 '20 at 11:27
  • I see, I wasn't clear. I'm trying to blit the name once I exit the ``input_player_name()```, which is ``ìf game_animation : win.blit(name, (100, 300)) ```. I don't know if there's a way to show a video if what I'm trying to do is hard to understand. – bappi Apr 16 '20 at 11:32
  • Yeah, once i press enter after inputting name, it displays name in that area and prints the surface, it works for me, Could you post all of your code – The Big Kahuna Apr 17 '20 at 00:01
  • Ok Im just so dumb, I wrote the line for blitting the name before the line for blitting the background. I just thought of changing this now, and of course it wasn't gonna work. Thanks for trying to helping me tho ! – bappi Apr 17 '20 at 12:05
  • It happens to the best of us. Glad you fixed it – The Big Kahuna Apr 17 '20 at 12:25
  • is there a way to close the question ? Now that I got the answer – bappi Apr 17 '20 at 12:38