How To Draw Text In Pygame
Displaying text to PyGame screen
Now that nosotros've got movement, and boundaries, nosotros need some way of informing our gamer that they 'dun goofed. Just only exiting the game is rather harsh on our player. Instead, what nosotros want to exercise 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 fourth dimension 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.brandish.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.heart = ((display_width/2),(display_height/2)) gameDisplay.blit(TextSurf, TextRect) pygame.display.update() time.slumber(ii) 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 = Imitation while not gameExit: for event in pygame.event.become(): if event.type == pygame.QUIT: pygame.quit() quit() if result.type == pygame.KEYDOWN: if effect.primal == pygame.K_LEFT: x_change = -5 if outcome.key == pygame.K_RIGHT: x_change = five if consequence.type == pygame.KEYUP: if event.fundamental == pygame.K_LEFT or event.key == pygame.K_RIGHT: x_change = 0 x += x_change gameDisplay.make full(white) car(x,y) if x > display_width - car_width or 10 < 0: crash() pygame.display.update() clock.tick(threescore) game_loop() pygame.quit() quit() So the major changes are some new functions, merely they all eddy downward to this line from our principal loop:
if ten > display_width - car_width or 10 < 0: crash()
We want to be able to have some sort of handling specific to our users crashing rather than just lumping information technology in with an go out. Then, we know we want to be able to practice a crash, so nosotros just demand to brand a crash office.
Then, and so nosotros consider what would be the most ideal state of affairs for u.s.a. to create a crash:
def crash(): message_display('Y'all Crashed') Since we might want to brand a crash more elaborate in the time to come, we brand a specific role, but, for now, we just desire to utilize a message_display office. Besides bad it doesn't exist! We tin see how information technology might be useful though, and then we're going to make 1.
def message_display(text): largeText = pygame.font.Font('freesansbold.ttf',115) TextSurf, TextRect = text_objects(text, largeText) TextRect.center = ((display_width/2),(display_height/two)) gameDisplay.blit(TextSurf, TextRect) pygame.display.update() time.sleep(2) game_loop() Then there'due south our message displaying function, nosotros define the big text, then we ascertain the text and the rectangle that would encompass it. We then middle the text, using our width and peak variables (the souvenir that keeps on giving...just imagine deciding you lot wanted to alter dimensions now?). Then we blit this stuff to the surface, remembering this simply draws information technology in the background, and that we need to call "pygame.display.update()."
Piece of cake enough, afterwards the message display, we run dorsum to the game loop, any that happens to be. In hindsight, I think the superior choice for the game_loop() function phone call would actually be in our crash office, after calling message_display. It makes way more sense, since all messages you lot might desire 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 simply just now noticing this every bit I write this a while subsequently writing and filming.
Finally, we realize that, gosh darnit, text_objects doesn't exist notwithstanding! Permit's finally make that ane!
def text_objects(text, font): textSurface = font.render(text, True, black) return textSurface, textSurface.get_rect()
The only fancy thing we're doing hither is using .get_rect() to get the rectangle that is somewhat invisible, and so nosotros can reference it and center the text.
In that location exists 1 quiz/question(south) for this tutorial. for access to these, video downloads, and no ads.
The next tutorial:
Source: https://pythonprogramming.net/displaying-text-pygame-screen/
Posted by: tatesincom.blogspot.com

0 Response to "How To Draw Text In Pygame"
Post a Comment