It is a three-step
process.
Step 1: Define a font object, ‘pygame.font.SysFont()’
method is used to get the font object.
Syntax
SysFont(name,
size, bold=False, italic=False)
Example
font =
pygame.font.SysFont('Calibri', 25, True, True)
Step 2: Render the text using render method.
Syntax
render(text,
antialias, color, background=None)
Example
text =
font.render("My text",True,BLACK)
Step 3: Use blit method to draw the text on surface.
Syntax
blit(source,
dest, area=None, special_flags = 0)
Example
screen.blit(text, [250, 250]) #import pygame import pygame import math #initialize game engine pygame.init() #Open a window size = (500, 500) screen = pygame.display.set_mode(size) #Set title to the window pygame.display.set_caption("Hello World") dead=False #Initialize values for color (RGB format) WHITE=(255,255,255) RED=(255,0,0) GREEN=(0, 255, 0) BLUE=(0,0,255) BLACK=(0,0,0) clock = pygame.time.Clock() PI=math.pi while(dead==False): for event in pygame.event.get(): if event.type == pygame.QUIT: dead = True screen.fill(WHITE) pygame.draw.polygon(screen, RED, [[10,10], [100, 10], [55, 80]], 0) pygame.draw.polygon(screen, RED, [[110,10], [200, 10], [155, 80]], 0) pygame.draw.polygon(screen, RED, [[210,10], [300, 10], [255, 80]], 0) pygame.draw.polygon(screen, RED, [[310,10], [400, 10], [355, 80]], 0) pygame.draw.polygon(screen, RED, [[410,10], [500, 10], [455, 80]], 0) pygame.draw.polygon(screen, GREEN, [[10, 500], [100, 500], [55, 420]], 0) pygame.draw.polygon(screen, GREEN, [[110, 500], [200, 500], [155, 420]], 0) pygame.draw.polygon(screen, GREEN, [[210, 500], [300, 500], [255, 420]], 0) pygame.draw.polygon(screen, GREEN, [[310, 500], [400, 500], [355, 420]], 0) pygame.draw.polygon(screen, GREEN, [[410, 500], [500, 500], [455, 420]], 0) font = pygame.font.SysFont('Calibri', 35, True, True) text = font.render("Welcome To Pygame",True,BLACK) screen.blit(text, [100, 240]) pygame.display.flip() clock.tick(60) #Shutdown display module pygame.display.quit()
No comments:
Post a Comment