pygame is
Simple DirectMedia Layer is
Site Swing


This script contains two functions that allow you to "subtract" the area of one or more rects from the area of another.
The script returns several new, smaller rects, that cover the entire area not removed by the other rect(s).

import pygame
from pygame.locals import *
def sub_rect(r1, r2):
    """remove the area of r2 from r1"""
    if not r1.colliderect(r2):
        return [r1]
    clip = r2.clip(r1)
    #first we have to see if there is a complete rect to each side of the removing rect
    need_com_left = False
    need_com_right = False
    need_com_top = False
    need_com_bottom = False
    if clip.left > r1.left:
        need_com_left = True
    if clip.right < r1.right:
        need_com_right = True
    if >
        need_com_top = True
    if clip.bottom < r1.bottom:
        need_com_bottom = True
    left = None
    right = None
    top = None
    bottom = None
    if need_com_left: #ok, we also need to check top and bottom here...
        t =
        b = r1.bottom -
        l = r1.left
        r = clip.left - r1.left
        if need_com_top: #we need to cut a bit off the top of this
            t =
        if need_com_bottom:
            b = clip.bottom - t
        left = pygame.Rect(l, t, r, b)
    if need_com_right:
        t =
        b = r1.bottom -
        l = clip.right
        r = r1.right - clip.right
        if need_com_top:
            t =
        if need_com_bottom:
            b = clip.bottom - t
        right = pygame.Rect(l, t, r, b)
    if need_com_top:
        top = pygame.Rect(r1.left,, r1.width,
    if need_com_bottom:
        bottom = pygame.Rect(r1.left, clip.bottom, r1.width, r1.bottom-clip.bottom)
    ret = []
    for i in [left, right, top, bottom]:
        if i: ret.append(i)
    return ret
def sub_rect_list(r1, rl):
    dirty = [r1]
    for i in rl:
        new = []
        for x in dirty:
            new.extend(sub_rect(x, i))
        dirty = new
    return dirty
#test this!
a = pygame.Rect(0,0,100,100)
b = pygame.Rect(10,10,80,80)
c = pygame.Rect(80,80,20,20)
dirty = sub_rect_list(a, [b, c])
screen = pygame.display.set_mode((640, 480))
back = pygame.Surface(a.size)
screen.blit(back, (0,0))
new = pygame.Surface(a.size)
for i in dirty:
    if i:
        screen.blit(new, i.topleft, i)

our projects welcomes all python game, art, music, sound, video and multimedia projects. If they use pygame or not.
recent releases
Feb 21, 2017

Jan 31, 2017

Jan 24, 2017

Jan 18, 2017

Jan 7, 2017

Dec 30, 2016

Dec 8, 2016

Nov 28, 2016

Nov 27, 2016

... more!
for pygame related questions, comments, and suggestions, please see help (lists, irc)