text to screen

Displaying text to PyGame screen



Now that we've got movement, and boundaries, we need some way of informing our gamer that they 'dun goofed. Just simply exiting the game is rather harsh on our player. Instead, what we want to do is have some sort of "game over" screen. To do this, we need to show some text to the screen.

Our new code is as follows:

import pygame
import time

pygame.init()

display_width = 800
display_height = 600

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

car_width = 73

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()

carImg = pygame.image.load('racecar.png')

def car(x,y):
    gameDisplay.blit(carImg,(x,y))

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()
    
    

def crash():
    message_display('You Crashed')
    
def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0

    gameExit = False

    while not gameExit:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                if event.key == pygame.K_RIGHT:
                    x_change = 5

            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        x += x_change

        gameDisplay.fill(white)
        car(x,y)

        if x > display_width - car_width or x < 0:
            crash()
            
        
        pygame.display.update()
        clock.tick(60)

game_loop()
pygame.quit()
quit()

So the major changes are some new functions, but they all boil down to this line from our main loop:

         if x > display_width - car_width or x < 0:
            crash()

We want to be able to have some sort of handling specific to our users crashing rather than just lumping it in with an exit. So, we know we want to be able to do a crash, so we just need to make a crash function.

So, then we consider what would be the most ideal situation for us to create a crash:

def crash():
    message_display('You Crashed')

Since we might want to make a crash more elaborate in the future, we make a specific function, but, for now, we just want to utilize a message_display function. Too bad it doesn't exist! We can see how it might be useful though, so we're going to make one.

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)
    pygame.display.update()
    time.sleep(2)
    game_loop()

So there's our message displaying function, we define the large text, then we define the text and the rectangle that would encompass it. We then center the text, using our width and height variables (the gift that keeps on giving...just imagine deciding you wanted to change dimensions now?). Then we blit this stuff to the surface, remembering this only draws it in the background, and that we need to call "pygame.display.update()."

Easy enough, after the message display, we run back to the game loop, whatever that happens to be. In hindsight, I think the superior choice for the game_loop() function call would actually be in our crash function, after calling message_display. It makes way more sense, since all messages you might want to display shouldn't lead to restarting the game. I advise doing that instead of the way shown in the code here and the video, I am only just now noticing this as I write this a while after writing and filming.

Finally, we realize that, gosh darnit, text_objects doesn't exist yet! Let's finally make that one!

def text_objects(text, font):
    textSurface = font.render(text, True, black)
    return textSurface, textSurface.get_rect()

The only fancy thing we're doing here is using .get_rect() to get the rectangle that is somewhat invisible, so we can reference it and center the text.


There exists 1 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:





  • Introduction to PyGame
  • Displaying images with PyGame
  • Moving an image around in PyGame
  • Adding boundaries
  • Displaying text to PyGame screen
  • Drawing objects with PyGame
  • Crashing
  • PyGame Score
  • Drawing Objects and Shapes in PyGame
  • Creating a start menu
  • PyGame Buttons, part 1, drawing the rectangle
  • PyGame Buttons, part 2, making the buttons interactive
  • PyGame Buttons, part 3, adding text to the button
  • PyGame Buttons, part 4, creating a general PyGame button function
  • PyGame Buttons, part 5, running functions on a button click
  • Converting PyGame to an executable
  • Adding a pause function to our game and Game Over
  • PyGame Icon
  • Sounds and Music with PyGame