0

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()
3
  • I also don't know "classes" but I don't think I need it Commented Dec 24, 2014 at 21:23
  • well you are working with an object. therefore you should be familiar with classes. anyway could you show us the code where you get the error with pygame.Rect? Commented Dec 24, 2014 at 21:34
  • Classes are crucial in collsion detection Commented Dec 30, 2014 at 22:48

3 Answers 3

4

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!)

Sign up to request clarification or add additional context in comments.

Comments

1
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.

Comments

1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.