#-*- coding: utf-8 -*- #CircularSquare #Clase en Python que dibuja un Rectángulo con bordes redondos #2008 por BrunoProg64 #Este código se libera bajo la Licencia MIT (http://www.opensource.org/licenses/mit-license.php) import pygame, math, sys class CircularSquare: def __init__(self, color): #el color debe ser un string reconocible, de no serlo se usa 'red' (rojo) #es decir RGB(255,0,0) try: self.color = pygame.color.Color(color) except ValueError: self.color = pygame.color.Color('red') def square(self, Surface, x1, x2, y1, y2, radius): tmp = x2 - x1 #largo del rectángulo cpx = tmp - radius tmp = y2 - y1 cpy = tmp - radius p1 = (x1 + cpx, y1) p2 = (x2 - cpx, y1) p3 = (p1[0], y2) p4 = (p2[0], y2) #lineas horizontales pygame.draw.line(Surface, self.color, p1, p2) pygame.draw.line(Surface, self.color, p3, p4) v1 = (x1, y1 + cpy) v2 = (x1, y2 - cpy) v3 = (x2, v1[1]) v4 = (x2, v2[1]) #lineas verticales pygame.draw.line(Surface, self.color, v1, v2 ) pygame.draw.line(Surface, self.color, v3, v4 ) self.__arc__(Surface,x1+radius,y1+radius,90.0,180.0,radius) self.__arc__(Surface,x1+radius,y2-radius,180.0,270.0,radius) self.__arc__(Surface,x2-radius,y2-radius,270.0,360.0,radius+1) self.__arc__(Surface,x2-radius,y1+radius,0.0,90.0,radius+0) def __arc__(self, Surface, pos1, pos2, anin, anfin, radius): x = 0 y = 0 res = (2 * 3.1416) / 360.0 tmp = anin while tmp < anfin: x = radius * math.cos(res*tmp) y = radius * math.sin(res*tmp) Surface.set_at((pos1+x,pos2-y),self.color) tmp = tmp + 0.10 def main(): pygame.init() sq = CircularSquare('green') screen = pygame.display.set_mode((320,240)) while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() sq.square(screen,20,250,10,210,40) pygame.display.flip() if __name__ == "__main__": main()