-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquare.py
More file actions
32 lines (25 loc) · 1009 Bytes
/
Copy pathsquare.py
File metadata and controls
32 lines (25 loc) · 1009 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import pygame
import math
class Square:
width, height = (60,20)
visible = True
x = 5
y = 5
def __init__(self,width,height,x,y):
self.x = x
self.y = y
self.width = width
self.height = height
def draw(self,screen):
pygame.draw.rect(screen,pygame.Color(0,0,0), (self.x,self.y,60,20),0,3)
def check_collision(self, ball):
# Calculate the nearest point on the rectangle to the center of the ball
nearest_x = max(self.x, min(ball.x, self.x + self.width))
nearest_y = max(self.y, min(ball.y, self.y + self.height))
# Calculate the distance between the nearest point and the center of the ball
distance_x = ball.x - nearest_x
distance_y = ball.y - nearest_y
# Calculate the distance
distance = math.sqrt(distance_x ** 2 + distance_y ** 2)
# Check if the distance is less than or equal to the ball's radius
return distance <= ball.radius