Jim Larson
Jim Larson

Reputation: 55

Pygame circle collision?

I am using pygame to make a simple game. I am having issues with circle collisions. I am getting the following error:

"AttributeError: 'pygame.Rect' object has no attribute 'rect'"

Here is the particular code I am having issues with below:

if pygame.sprite.collide_circle(hero_circle, enemy_circle):
    gameover()

Upvotes: 0

Views: 23464

Answers (3)

DGenther
DGenther

Reputation: 11

The best way I've found to check circle collision detection is to calculate the distance between the center points of two circles. If the distance is less than the sum of the two circle's radii, then you've collided.

Upvotes: 1

Billylegota
Billylegota

Reputation: 316

Use pygame.mask to create a collision mesh for your objects and use the mesh to do collision detections.

In more detail:

  1. Create an image file for both of your circles and set the bg color to something you will not use anywhere else.
  2. Set that color to "transparent" in your image editor.
  3. Import the images.
  4. Create a mesh for them with pygame.mask and set it to make transparent pixels non-collidable.
  5. Use the generated mask as your collision detection mesh.
  6. PROFIT

(Technically this is just doing collision detection of a circle shaped area on a rectangle, but who cares!)

Upvotes: 4

magic-sudo
magic-sudo

Reputation: 1236

pygame.draw.rect()
draw a rectangle shape
rect(Surface, color, Rect, width=0) -> Rect

Draws a rectangular shape on the Surface. The given Rect is the area of the rectangle. The width argument is the thickness to draw the outer edge. If width is zero then the rectangle will be filled.

Keep in mind the Surface.fill() method works just as well for drawing filled rectangles. In fact the Surface.fill() can be hardware accelerated on some platforms with both software and hardware display modes.

Upvotes: 1

Related Questions