3. Pygame Collision Detection¶


def doRectsOverlap(rect1,rect2):
for a,b in [(rect1,rect2),(rect2,rect1)]:
# Check if a's corners are inside b
if ((isPointInsideRect(a.left,a.top,b)) or (isPointInsideRect(a.left,a.bottom,b)) or (isPointInsideRect(a.right,a.top,b)) or (isPointInsideRect(a.right,a.bottom,b))):
return True
return False
def isPointInsideRect(x,y,rect):
if (x > rect.left) and (x < rect.right) and (y > rect.top) and (y < rect.bottom):
return True
else:
return False
xxxxxxxxxx
if ((isPointInsideRect(a.left,a.top,b)) or (isPointInsideRect(a.left,a.bottom,b)) or (isPointInsideRect(a.right,a.top,b)) or (isPointInsideRect(a.right,a.bottom,b))):
import pygame, sys , time
from pygame.locals import *
from pygame.color import *
import random
#set up the colors
BLACK = Color(0,0,0)
WHITE = Color(255,255,255)
RED = Color(255,0,0)
GREEN = Color(0,255,0)
BLUE = Color(0,0,255)
LIGHT_BLUE= Color(153,255,255)
# set up pygame
pygame.init()
#set up fonts
rectFont = pygame.font.SysFont('timesnewroman',10)
pointFont = pygame.font.SysFont('timesnewroman',10)
# set up the window
WINDOWWIDTH = 560
WINDOWHEIGHT = 350
windowSurface = pygame.display.set_mode((WINDOWWIDTH,WINDOWHEIGHT),0,32)
Activity: 3.1 ActiveCode (ac_l60_3_en)
You have attempted 1 of 2 activities on this page