[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] hoe to create the RoundRect ?



Well, I fixed it up a little.  There are still a few problems in the rounded edges, but you get the idea.
import pygame
from pygame.locals import *
import sys, os
from math import *
if sys.platform == 'win32' or sys.platform == 'win64':
    os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()

Screen = (800,600)

icon = pygame.Surface((1,1)); icon.set_alpha(0); pygame.display.set_icon(icon)
pygame.display.set_caption("Rounded Rectangle Demo - Ian Mallett - 2008")
Surface = pygame.display.set_mode(Screen)

def RoundedRect(surface,BorderColor,FillColor,(posx,posy,dimensionx,dimensiony),width,roundedness):
    for x in xrange(roundedness,0-1,-1):
        y = sqrt((roundedness**2)-(x**2))
        rect = (posx+(roundedness-x),
                posy+(roundedness-y),
                dimensionx-(2*(roundedness-x)),
                dimensiony-(2*(roundedness-y)))
        pygame.draw.rect(surface,BorderColor,rect,0)
    for x in xrange(roundedness-width,0-1,-1):
        y = sqrt(((roundedness-width)**2)-(x**2))
        rect = (posx+(roundedness-x),
                posy+(roundedness-y),
                dimensionx-(2*(roundedness-x)),
                dimensiony-(2*(roundedness-y)))
        pygame.draw.rect(surface,FillColor,rect,0)
def GetInput():
    key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == QUIT or key[K_ESCAPE]:
            pygame.quit(); sys.exit()
def Draw():
    Surface.fill((0,0,0))
    RoundedRect(Surface,(0,255,0),(255,0,0),(100,100,200,200),1,32)
    RoundedRect(Surface,(0,0,255),(0,0,0),(450,100,250,300),2,32)
    RoundedRect(Surface,(0,255,0),(0,0,255),(150,350,200,200),16,32)
    RoundedRect(Surface,(0,255,0),(0,0,0),(400,450,350,64),3,32)
    pygame.display.flip()
def main():
    while True:
        GetInput()
        Draw()
if __name__ == '__main__': main()