I have a draw area that I want to draw to and borders that I would not like to draw over. Currently, if the mouse positions (m_x & m_y) are within the circles radius of the border I have the program draw the circle and then redraw rectangles which cut off the part of the circle which crossed. There must be a smarter and more efficient way to draw only the parts of the circle which are within the border.
if event.type == pygame.MOUSEBUTTONDOWN or pygame.MOUSEMOTION and mouse_pressed[0] == 1:
if m_x < draw_areax-brush_size and m_y < draw_areay-brush_size:
circle = pygame.draw.circle(screen,brush_colour,(m_x,m_y),brush_size)
else:
circle = pygame.draw.circle(screen,brush_colour,(m_x,m_y),brush_size)
reloadareas()
The documentation for
pygame.draw
says:So if you want to draw only the parts of the circle that are within some rectangular region, then set up a clip area by calling
pygame.Surface.set_clip
, draw the circle, and then remove the clip area. Assuming that you do not normally have a clip area in effect on the screen, then you would program it like this:Here's an example:
If you do a lot of drawing with a clip rectangle, then you might want to encapsulate the setting and unsetting of the clip rectangle in a context manager, perhaps like this, using
contextlib.contextmanager
:and then my example could be written like this:
You have a bit of repetitive code in here. I would move the
circle =
statement out of theif
/else
block, reverse theif
s condition, and putreloadareas()
in it: