import pygame, sys pygame.init() def bouncing_rect(): global x_speed, y_speed, other_speed moving_rect.x += x_speed moving_rect.y += y_speed # collision with screen borders if moving_rect.right >+ screen_width or moving_rect.left <= 0: x_speed *= -1 if moving_rect.bottom >= screen_height or moving_rect.top <= 0: # moving the other rectangle\ other_rect.y += other_speed if other_rect.top <= 0 other_rect.bottom >= screen_height:\] other_speed *= -1 # collision with rect collision_tolerence = 10 if moving_rect.colliderect(other_rect): if abs(other_rect.top - moving_rect.bottom) < collision_tolerence and y_speed > 0 y_speed *_ -1 if abs (other_rect.bottom - moving_rect.top) < collision_tolerence and y_speed > 0 y_speed *_ -1 if abs() # collision with screen borders if moving_rect.right >= screen_width or moving_rect.left <= 0: x_speed *= -1 if moving_rect.bottom >= screen_height or moving_rect.top <= 0: clock = pygame.time.clock() screen_width,screen_height = 800,800 screen = pygame.display.sec_mode((screen_width, screen_height)) moving_rect = pygame.rect(350,350,100,100) x_speed, y_speed = 5,4 other_rect = pygame