# How to detect collisions between two rectangular objects or images in pygame

Use `pygame.Rect` objects and `colliderect()` to detect the collision between the bounding rectangles of 2 objects or 2 images:

``````rect1 = pygame.Rect(x1, y1, w1, h1)
rect2 = pygame.Rect(x2, y2, w2, h2)
if rect1.colliderect(rect2):
# [...]
``````

If you have to images (`pygame.Surface` objects), the bounding rectangle of can be get by `get_rect()`, where the location of the Surface has to be set by an keyword argument, since the returned rectangle always starts at (0, 0):
(see Why is my collision test not working and why is the position of the rectangle of the image always wrong (0, 0)?)

``````def game_loop():
# [...]

while running:
# [...]

player_rect = player_img.get_rect(topleft = (x, y))
for i in range(len(things_cor)):

if player_rect.colliderect(thing_rect):
print("hit")

player(x, y)
x += x_change

for i in range(len(things_cor)):
thing_x, thing_y = things_cor[i]
``````

Use `pygame.time.get_ticks()` to delay the start of the game for a certain time. `pygame.time.get_ticks()` return the number of milliseconds since `pygame.init()` was called. For instance:

``````def game_loop():
# [...]

while running:
passed_time = pygame.time.get_ticks() # passed time in milliseconds
start_time = 100 * 1000 # start time in milliseconds (100 seconds)

# [...]

# move player
if passed_time >= start_time:
x += x_change
if x < 0:
x = 0
elif x > display_width - player_width:
x = display_width - player_width

# move things
if passed_time >= start_time:
for i in range(len(things_cor)):
things_cor[i] += y_change
if things_cor[i] > display_height:
things_cor[i] = random.randint(-2000, -1000)
things_cor[i] = random.randint(0, display_width)