Saturday, February 4, 2017

Ex07. Text

To create text output, we need a font, and then we may create a text image by using its render method


We use the get_rect method to find the region on main display to blit it to. It is also possible to add more elements to the image prior to sending it to main display, as we will do in Button example later.



# Ex07.py 
# Text

import pygame as pg

colors = ['red','green','blue','cyan',
    'purple','brown','magenta','orange']
cmap = list(map(pg.Color,colors))

width, height = 640, 480

pg.init()

screen = pg.display.set_mode((width, height))
pg.display.set_caption('Ex07. Text')

screen.fill(pg.Color("white"))

# font
font = pg.font.SysFont("arial",56)

# create Text Surface, and get bounding rect
text = font.render("Hello pygame!", True, cmap[0])
rect = text.get_rect()

# center on screen
rect.center = (width/2,height/2)

screen.blit(text,rect)

pg.display.update()

done = False
while not done:
    for event in pg.event.get():
        if event.type == pg.QUIT:
         done = True
            
pg.quit()

This is the output:


No comments:

Post a Comment