Pygame (Python Game Development) Tutorial – 14 – Adding Text to the Screen

[ad_1]
Facebook –
GitHub –
Google+ –
LinkedIn –
reddit –
Support –
thenewboston –
Twitter –


Posted

in

by

Tags:

Comments

37 responses to “Pygame (Python Game Development) Tutorial – 14 – Adding Text to the Screen”

  1. Heyits raksa Avatar

    Can’t you just use
    pygame.time.wait(amt_seconds*1000)

  2. Robin IS Live Avatar

    This is my source code and it worked fine 🙂

    import pygame
    import time

    pygame.init()

    white=(255,255,255)
    black=(0,0,0)
    red=(255,0,0)

    display_width=800
    display_height=600

    gameDisplay = pygame.display.set_mode((display_width,display_height))
    pygame.display.set_caption('Slither')

    pygame.display.update()

    gameExit = False

    lead_x=display_width/2
    lead_y=display_height/2

    lead_x_change = 0
    lead_y_change = 0

    clock=pygame.time.Clock()

    block_size=10
    FPS=30

    font=pygame.font.SysFont(None,25)

    def message_to_screen(msg,color):
    screen_text=font.render(msg,True,color)
    gameDisplay.blit(screen_text,[display_width/2,display_height/2])

    while not gameExit:
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    gameExit = True
    if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_LEFT:
    lead_x_change = -block_size
    elif event.key == pygame.K_RIGHT:
    lead_x_change = block_size

    elif event.key == pygame.K_UP:
    lead_y_change = -block_size
    elif event.key == pygame.K_DOWN:
    lead_y_change = block_size

    if lead_x> display_width or lead_x < 0 or lead_y > display_height or lead_y < 0:
    gameExit=True

    lead_x += lead_x_change
    lead_y += lead_y_change
    gameDisplay.fill(white)
    pygame.draw.rect(gameDisplay,black,[lead_x,lead_y,block_size,block_size])
    pygame.display.update()

    clock.tick(FPS)

    message_to_screen("You Lose",red)
    pygame.display.update()
    time.sleep(2)

    pygame.quit()
    quit()

  3. YUHAO HE Avatar

    I should go outside gegeXD

  4. MojoStudios Avatar

    having a really good time….. wut just happened XD lmao

  5. Hussein Hourani Avatar

    my program quits regardless if I put pygame.quit() at the end. Also, my message doesn't show, it just loads for 3 seconds and quits by itself

  6. Tanuj Namdeo Avatar

    You loose you suck. haha

  7. Jihad Ouard Avatar

    you're fabulous; i really enjoy your tutoriels!!

  8. Petr Gardas Avatar

    What the Freak this is sentdex video and you are not sentdex REPORT all!

  9. דיויד דינסטון Avatar

    isnt it will be much more simple to update the game in the function text we build, then update the game each time we put some text?

  10. Ticcy Robby Avatar

    OSError: unable to read font file
    Im gettin this eror, any advice ? ı looked everywhere and there is no solution
    Oh ı fixed after 4-5 hours just changed
    font = pygame.font.SysFont(None, 25) to
    font = pygame.font.Font('freesansbold.ttf', 25)

  11. myk b Avatar

    "error: font not initialized"
    how do i fix this?

  12. MrPyfisher199 Avatar

    The text does not get displayed! Even when I tried to use the source code the text did not appear 🙁 I tried practically all the comments below yet nothing worked D:

  13. Ernesto Ibarrola Avatar

    NameError: name 'dispaly_width' is not defined

  14. Mathemagician 4 Avatar

    My code on Python 3.6.1 says 'name error, 'color' is not defined'. What should I do?

  15. Blessed Lucifer Avatar

    My code is so perfect that it doesn't show the error nor the text that should appear after losing xD i can't understand what i am doing right now

  16. kyle yoon Avatar

    how can it tell when you exit

  17. mohammed riaz Avatar

    @thenewboston

    basically i wanna make a caption for a box that says send and i also need the another box that allows for input of text if you could get back to me that would be GREAT

  18. Eric Barone Avatar

    Why do I have to sit through 1.5 and 2 minute long ads for EVERY one of your videos?

  19. Nir Karl Avatar

    how can i get the string to display from the user?

  20. jholotan best Avatar

    i am so bad 🙁 (i had 3 typos in this few lines of text)

  21. Lower Ur Grades Avatar

    With gameExit instead of making it leave the game you could have it do:
    "
    x_lead = 400
    y_lead = 300
    "
    Which would basically reset the game because the square would be thrown back to it's original position.

  22. Vladimir Avatar

    5:10 my self-esteem will definitely decrease if i would saw such message

  23. Sabarigirish M Avatar

    It is opening with a pure black screen. Please help me.

  24. fun Tertain Avatar

    If you want to run Pygame in pycharm just go to your settings -> project interperter change your interperter to Python.exe which should automatically have searched for Pygame on your computer(if not put it in the same folder as where you downloaded Python)make sure the version of Python you are using matches up with Pygame.

  25. Adam Black Avatar

    Hi Sendex;
    Some help here please; In Pygame Tutorial – 15, I cannot input the 'C' to continue or 'Q' to quit to either continue or quit the program. The Canvas just hang there. I using Python 3.1
    Thanks

  26. Reppercent Avatar

    im having a problem, which keeps causing my pygame to not respond, any ideas?
    line 67, in <module>
    time.sleep(2)
    NameError: name 'time' is not defined

  27. MaxPicAxe Avatar

    Why is this uploaded to the new boston and not sentdex

  28. Ethan Donovan Avatar

    For anyone new to watching this series like I am, and are having trouble displaying the text on screen… Type your code with an empty line in between the pygame.display.update and the time.sleep(2) like so.

    message_to_screen("You Lose", red)
    pygame.display.update()

    time.sleep(2)
    pygame.quit()
    quit()

    I don't know why this works but it did for me and I hope it does for everyone else.

  29. bunc11 Avatar

    How can that function see font object and display object if u dont pass that stuff into function?

  30. Ye Olde Coot Avatar

    I'm getting a syntax error for while please help, this is my code

    import pygame
    import random
    import time
    pygame.init()

    white = (255,255,255)
    black = (0,0,0)
    red = (255,0,0)
    green = (0,255,0)
    blue = (0,0,255)
    display_width = 800
    display_height = 600

    gamedisplay = pygame.display.set_mode((display_width,display_height))
    pygame.display.set_caption('slither')
    gameExit = True

    lead_x = display_width/2
    lead_y = display_height/2
    lead_x_change = 0
    lead_y_change = 0

    clock = pygame.time.Clock()
    FPS = 15
    block_size = 20

    font = pygame.font.SysFont(None, 25)

    def message_to_screen(msg,color):
        screen_text = dont.render(msg, True, color)
        gamedisplay.blit(screen_text, [display_width/2, display_height/2]
            

    while gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    lead_x_change = -block_size
                    lead_y_change = 0
                if event.key == pygame.K_RIGHT:
                    lead_x_change = block_size
                    lead_y_change = 0
                if event.key == pygame.K_UP:
                    lead_y_change = -block_size
                    lead_x_change = 0
                if event.key == pygame.K_DOWN:
                    lead_y_change = block_size
                    lead_x_change = 0
        if lead_x >= display_width or lead_x <= 0 or lead_y >= display_height or lead_y <= 0:
            gameExit = False

        
        lead_x += lead_x_change
        lead_y += lead_y_change
        
        gamedisplay.fill(white)
        pygame.draw.rect(gamedisplay, blue, [lead_x,lead_y,block_size,10])
        pygame.display.update()
        clock.tick(FPS)

                         
    message_to_screen("you is dead", red)
    pygame.display.update()
    time.sleep(2)
    pygame.quit()
    quit()

  31. TheRedstone Lemon Avatar

    I think this is sentdex.

  32. Yue Zhang Avatar

    For people who's using older python and having "font errors", try to change 
    font = pygame.font.SysFont(None, 25)
    to 
    font = pygame.font.Font(None, 25)

  33. Bruce Wayne Avatar

    The font = pygame.font.SysFont(None, 25)
    is giving me this error. How do I fix this/

    Warning (from warnings module):
      File "/Users/Sean/Desktop/Programming Projects/Python/Snake/snake.py", line 31
        font = pygame.font.SysFont(None, 25)
    RuntimeWarning: use font: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/font.so, 2): Symbol not found: _TTF_CloseFont
      Referenced from: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/font.so
      Expected in: flat namespace
     in /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/font.so
    (ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/font.so, 2): Symbol not found: _TTF_CloseFont
      Referenced from: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/font.so
      Expected in: flat namespace
     in /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/font.so)
    Traceback (most recent call last):
      File "/Users/Sean/Desktop/Programming Projects/Python/Snake/snake.py", line 31, in <module>
        font = pygame.font.SysFont(None, 25)
      File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/__init__.py", line 102, in _getattr_
        raise NotImplementedError(MissingPygameModule)
    NotImplementedError: font module not available
    (ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/font.so, 2): Symbol not found: _TTF_CloseFont
      Referenced from: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/font.so
      Expected in: flat namespace
     in /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/pygame/font.so)

  34. Hogan Dromgool Avatar

    For people having issues with Text not displaying!:

    Whenever you wish to update graphics OR text on the screen you must have this function:
    pygame.display.update()

    place it directly under the object you are trying to update. I'm not sure why his works but most of our's doesn't (I'm using the same version of Pygame & Python but this did not work for me without updating the graphics.)

  35. Ethan Watson Avatar

    I keep getting errors and not sure how to fix it.. i even tried your source code. I am using mac, python 2.7, and pygame 1,9

    Traceback (most recent call last):
      File "/Users/ethanwatson/Documents/testpygame.py", line 29, in <module>
        font = pygame.font.SysFont(None, 25)
      File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/__init__.py", line 70, in _getattr_
        raise NotImplementedError(MissingPygameModule)
    NotImplementedError: font module not available
    (ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/font.so, 2): Library not loaded: /usr/X11/lib/libfreetype.6.dylib
      Referenced from: /Library/Frameworks/SDL_ttf.framework/Versions/A/SDL_ttf
      Reason: image not found)

  36. mshin54 Avatar

    hey sentdex I have been following up with ur tuts and I have one question.
    I actually tried to put
            message_to_screen("You Lose", red)
            time.sleep(2)
    inside
    if lead_x > display_width – block_size or lead_x < 0 or lead_y > display_height – block_size or lead_y < 0:
    and when I run the file and I go out the frame the game would quit 2 sec later but the text wouldn't show. Any ideas as to why that's happening?

Leave a Reply

Your email address will not be published. Required fields are marked *