render box

This commit is contained in:
Frank DeMarco 2015-07-21 17:43:42 -04:00
parent 396d689932
commit a0aba9a0bb
1 changed files with 29 additions and 0 deletions

View File

@ -1,6 +1,9 @@
from random import randint
from math import sin, cos, atan2, radians, sqrt
from pygame import Surface
from pygame.locals import *
def get_step(start, end, speed):
x0, y0 = start
x1, y1 = end
@ -99,3 +102,29 @@ def collide_line_with_rect(rect, p0, p1):
(rect.bottomleft, rect.topleft)):
if get_intersection(p0, p1, *line):
return True
def render_box(font, text, antialias, color, background=None, border=None,
border_width=1, padding=0):
surface = font.render(text, antialias, color, background)
if padding:
if isinstance(padding, int):
padding = [padding] * 2
padding = [x * 2 for x in padding]
rect = surface.get_rect()
padded_surface = Surface(rect.inflate(padding).size, SRCALPHA)
if background is not None:
padded_surface.fill(background)
rect.center = padded_surface.get_rect().center
padded_surface.blit(surface, rect)
surface = padded_surface
if border is not None:
if isinstance(border_width, int):
border_width = [border_width] * 2
border_width = [x * 2 for x in border_width]
rect = surface.get_rect()
bordered_surface = Surface(rect.inflate(border_width).size)
bordered_surface.fill(border)
rect.center = bordered_surface.get_rect().center
bordered_surface.blit(surface, rect)
surface = bordered_surface
return surface