#!/usr/bin/env python
# coding: utf
'''
Простейший пример печати текста для PyGame
'''

import pygame, os

def Label(scr, text, pos=(0,0), size=12, fg="White", bg=None, font="mono", centered=False):
    '''Рисует текст text на экране scr по координатам pos.
    Параметры fg и bg могут быть Color() или str().
    Параметр font может быть именем файла, типом шрифта или объектом Font()
    При установленном centered pos считается координатами центра'''
    if type(fg) is str: fg=pygame.Color(fg)
    if type(bg) is str: bg=pygame.Color(bg)
    if type(font) is str:
        if font[0] != os.path.sep:
            font=pygame.font.match_font(font)
        font=pygame.font.Font(font, size)
    rect=pygame.Rect(pos,font.size(text))
    if centered: rect.move_ip((-rect.width/2,-rect.height/2))
    if bg:
        scr.blit(font.render(text, True, fg, bg), rect)
    else:
        scr.blit(font.render(text, True, fg), rect)
    return rect

if __name__ == "__main__":
    import random
    pygame.init()
    Size=(800,400)
    Scr=pygame.display.set_mode(Size)
    Scr.fill(pygame.Color("Black"))
    r=20
    for i in xrange(100):
        pos=random.randint(r,Size[0]-r),random.randint(r,Size[1]-r)
        col=random.randint(10,255),random.randint(10,255),random.randint(10,255)
        pygame.draw.circle(Scr,col,pos,r)
    cont=True
    while cont:
        ev=pygame.event.wait()
        if ev.type is pygame.QUIT:
            cont=False
        sz=random.randrange(10,32)
        txt="Hello!"
        pos=random.randrange(0,Size[0]-3*sz),random.randrange(0,Size[1]-sz)
        rect=Label(Scr,txt,pos,sz)
        rect.inflate_ip((sz/2,sz))
        pygame.draw.ellipse(Scr,pygame.Color("Orange"),rect,1)
        pygame.display.flip()
