school-horror-game/test.py

30 lines
826 B
Python
Raw Normal View History

2026-08-06 09:38:54 +00:00
import pygame
def rect_circle_collision(rect, circle):
rect_x, rect_y, rect_w, rect_h = rect
circle_x, circle_y, circle_r = circle
# Calculate the closest point on the rectangle to the center of the circle
closest_x = max(rect_x, min(circle_x, rect_x + rect_w))
closest_y = max(rect_y, min(circle_y, rect_y + rect_h))
# Calculate the distance between the center of the circle and the closest point on the rectangle
dx = circle_x - closest_x
dy = circle_y - closest_y
distance = (dx ** 2 + dy ** 2) ** 0.5
return distance <= circle_r
# Example usage
pygame.init()
rect = (100, 100, 200, 200) # (x, y, width, height)
circle = (150, 150, 50) # (x, y, radius)
if rect_circle_collision(rect, circle):
print("Collision detected!")
else:
print("No collision.")
pygame.quit()