I can't figure out to display text in pygame.
I know I can't use print like in regular Python IDLE but I don't know how.
import pygame, sys from pygame.locals import * BLACK = ( 0, 0, 0) WHITE = (255, 255, 255) GREEN = (0, 255, 0) RED = ( 255, 0, 0) pygame.init() size = (700, 500) screen = pygame.display.set_mode(size) DISPLAYSURF = pygame.display.set_mode((400, 300)) pygame.display.set_caption('P.Earth') while 1: # main game loop for event in pygame.event.get(): if event.type == QUIT: pygame.display.update() import time direction = '' print('Welcome to Earth') pygame.draw.rect(screen, RED, [55,500,10,5], 0) time.sleep(1)
This is only the beginning part of the whole program.
If there is a format that will allow me to show the text I type in the pygame window that'd be great. So instead of using print I would use something else. But I don't know what that something else is. When I run my program in pygame it doesn't show anything.
I want the program to run in the pygame window instead of it just running in idle.
In python, the print statement is used to display text. In python with the print statement, you can use Single Quotes(') or Double Quotes(").
To flip the image we need to use pygame. transform. flip(Surface, xbool, ybool) method which is called to flip the image in vertical direction or horizontal direction according to our needs.
There is no easy way to render text on multiple lines in pygame, but this helper function could provide some use to you. Just pass in your text (with newlines), x, y, and font size. The font size is not the same as the character height though.
You can create a surface with text on it. For this take a look at this short example:
pygame.font.init() # you have to call this at the start, # if you want to use this module. myfont = pygame.font.SysFont('Comic Sans MS', 30)
This creates a new object on which you can call the render
method.
textsurface = myfont.render('Some Text', False, (0, 0, 0))
This creates a new surface with text already drawn onto it. At the end you can just blit the text surface onto your main screen.
screen.blit(textsurface,(0,0))
Bear in mind, that everytime the text changes, you have to recreate the surface again, to see the new text.
There's also the pygame.freetype
module which is more modern, works with more fonts and offers additional functionality.
Create a font object with pygame.freetype.SysFont()
or pygame.freetype.Font
if the font is inside of your game directory.
You can render the text either with the render
method similarly to the old pygame.font.Font.render
or directly onto the target surface with render_to
.
import pygame import pygame.freetype # Import the freetype module. pygame.init() screen = pygame.display.set_mode((800, 600)) GAME_FONT = pygame.freetype.Font("your_font.ttf", 24) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((255,255,255)) # You can use `render` and then blit the text surface ... text_surface, rect = GAME_FONT.render("Hello World!", (0, 0, 0)) screen.blit(text_surface, (40, 250)) # or just `render_to` the target surface. GAME_FONT.render_to(screen, (40, 350), "Hello World!", (0, 0, 0)) pygame.display.flip() pygame.quit()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With