0

I have looked for a solution to my problem but the only one I could find was outdated. I know how to put text on the window but it doesn't work for variables. It would be greatly appreciated if someone could provide an example code.

edit: this is what i tried using a variable with

myfont = pygame.font.SysFont('Arial', 30)
textsurface = myfont.render(score, True, (0, 0, 0))
windowSurface.blit(textsurface,(250, 250))

then python me to use a unicode or byte answer

Chinx
  • 1
  • 1
  • 2
    How about you start by showing what you've tried? – Pavel Dec 10 '17 at 23:33
  • probably you have to put it inside `while` loop (mainloop) so it will redraw it in every loop. – furas Dec 10 '17 at 23:34
  • Maybe find some tutorial. ie. [Program Arcade Games With Python And Pygame](http://programarcadegames.com/) – furas Dec 10 '17 at 23:36
  • There seems to be a lot of uncertainty in your question. Can oyu look at the answers and comments, and please clarify what exactly it is that you're trying to do? – roelofs Dec 10 '17 at 23:54
  • You just have to convert your variable to a string and pass it to `Font.render` every frame. Take a look at [this answer](https://stackoverflow.com/a/20842987/6220679) if you don't know how to render a text surface. – skrx Dec 10 '17 at 23:59

1 Answers1

2

It would be easier to help if we had more details.

However, if what you want to show is the change in a variable throughout the program, there's nothing wrong with just printing them as you go along. You can always take them out later if it all works and you don't want to see them.

for i in range(0, 10):

    print("i is", i) #prints out what i is each time through the loop.

If what you want to do is print out the name instead of the value, then you should reconsider - it's possible but awkward, and probably not necessary.

EDIT:

If you mean that you want to display a variable's value to the player, and have that update, then this might be helpful: How to display text in pygame?

What you'll want to do is update the surface every time the value changes, or every time you update the rest of the display. Don't edit it, just redraw it with different text.

Peritract
  • 761
  • 5
  • 13
  • 1
    `print` in question with tag `PyGame` ??? – furas Dec 10 '17 at 23:44
  • 2
    If they just want to know what a variable is, then they can print it to the shell. If they want a variable to update constantly on the screen, then that is different. – Peritract Dec 10 '17 at 23:48