How do I make a pygame.draw.rect detect collision with a pygame.surface?
Question:
The title basically explain it all. Here’s the code for the pygame.draw.rect:
stand = pygame.draw.rect(screen, green, (spike_x + 900, 400 - player_y + 476, 500, 500), border_radius=15)
I want the stand
to be able to collide with the player. The player is using a rect collision box:
player_collision = player.get_rect(topleft=(player_x, player_y))
I’ve tried to use if player_collsion.collidepoint(stand):
, but it doesn’t work. When I try that, it says TypeError: argument must contain two numbers
Answers:
You have to use colliderect()
rather than collidepoint()
. collidepoint
is used to compare a rectangle and a point, colliderect
is used to compare 2 rectangles:
if player_collsion.collidepoint(stand):
if player_collsion.colliderect(stand):
The title basically explain it all. Here’s the code for the pygame.draw.rect:
stand = pygame.draw.rect(screen, green, (spike_x + 900, 400 - player_y + 476, 500, 500), border_radius=15)
I want the stand
to be able to collide with the player. The player is using a rect collision box:
player_collision = player.get_rect(topleft=(player_x, player_y))
I’ve tried to use if player_collsion.collidepoint(stand):
, but it doesn’t work. When I try that, it says TypeError: argument must contain two numbers
You have to use colliderect()
rather than collidepoint()
. collidepoint
is used to compare a rectangle and a point, colliderect
is used to compare 2 rectangles:
if player_collsion.collidepoint(stand):
if player_collsion.colliderect(stand):