0

Why the text message on screen is not updating and the print statement updates it? Here is the code:

font = pygame.font.SysFont('Comic Sans MS', 30)
textsurface = font.render(("You have lifes: " + str(lifes)), False, (0, 0, 0))

def check(lifes):
    if poop.x == bullet.x and bullet.y > poop.y and bullet.y < poop.y + pheight:
        lifes -= 1
        print(lifes)


def moves(move):
    if move[pygame.K_UP] and poop.y - vel > 0:
        poop.y -= vel
    if move[pygame.K_DOWN] and poop.y + vel + pheight < win_height:
        poop.y += vel
    bullet.x -= 5


def redraw_screen(white):
    WIN.fill(white)
    WIN.blit(poo, (poop.x, poop.y))
    WIN.blit(bull, (bullet.x, bullet.y))
    WIN.blit(textsurface, (20, 20))
    pygame.display.update()


def main(): 
     redraw_screen(blue)
     move = pygame.key.get_pressed()
     textsurface = font.render(str(lifes), False, (0, 0, 0))
     moves(move)
     check(lifes)
Ted Klein Bergman
  • 9,146
  • 4
  • 29
  • 50
Los Drakos
  • 13
  • 2
  • This code never calls `main`, but I think the issue is that main creates a new local variable 'textsurface' and never returns it or passes it to 'redraw_screen' – JeffUK Jan 16 '22 at 12:39
  • It does in my version, I just shorten it for here. And do you know what can I do about it for this not to happen? – Los Drakos Jan 16 '22 at 12:45
  • Main should pass 'textsurface' to 'redraw_screen' ... or redraw_screen should create it's own 'text_surface'.. the problem is you're not passing variables between any of your functions as far as we can see. e.g. `check()` returns nothing, just prints to the screen. – JeffUK Jan 16 '22 at 12:49
  • @LosDrakos Always create a [mcve]. It helps people to fully understand your problem and to not answer or ask questions about your question that should be clear from your example. It also helps future readers to easier see if it's the same problem they have and thus can get help from the answer. – Ted Klein Bergman Jan 16 '22 at 12:57
  • Also, be consistent with your indentation, as it's crucial in Python. I fixed it now but make sure it's correct when you post it in the future. – Ted Klein Bergman Jan 16 '22 at 12:59
  • And just so you know `textsurface = font.render(str(lifes), False, (0, 0, 0))` in `main` creates a new **local** variable. This only exists in `main` and cannot be used anywhere else. However, you **also** have a **global** variable with the same name that you use in other functions. These are not the same. Lookup how to define and use global variables in python, and about variable scopes. – Ted Klein Bergman Jan 16 '22 at 13:06
  • Ok I think I got it now. Thanks for help. – Los Drakos Jan 16 '22 at 13:09

1 Answers1

0

order of execution in python from top to bottom refer - Order of execution and style of coding in Python

In you code lifes variable is not updated, when code is executed from top to down, but lifes is empty, but after your main() execution lifes is printed properly because check(lifes) is called explicitly in main()

You may want to put below code after your main() is called like

if __name__ == '__main__':.
    main()
    font = pygame.font.SysFont('Comic Sans MS', 30)
    textsurface = font.render(("You have lifes: " + str(lifes)), False, (0, 0, 0))
Nalq
  • 16
  • 5
  • 1
    This isn't really correct as I assume they want it to update continuously throughout the game, and your solution wouldn't render anything to the screen as the program exits before the new text surface is rendered. – Ted Klein Bergman Jan 16 '22 at 13:02
  • Question is specific to why text message on screen is not updating and the print statement updates it, It is hard to assume how the complete program is designed, but to address the query, I am sure my answer is doing that job. – Nalq Jan 16 '22 at 13:13
  • Don't you still need `pygame.display.update()` to update the displayed font? – OneCricketeer Jan 17 '22 at 16:38