How do I find collisions between characters and images within PyGame? I have drawn a player from an image, and have drawn the walls from tiles, so how would I detect these collisions?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Lets say you have a player class that looks something like this, I like using pygame.sprite.collide_rect() in this case we'll also have a wall class.
Here is where we cheek for Collisions with anything in walls group, which we add when ever we make a new wall.
If you use the pygame Rect class to represent the boundaries of your object, you can detect whether two are colliding by using the Rect.colliderect function. For example:
a is colliding with b, and b is colliding with c, but a is not colliding with c. Note that rects that share a boundary are not colliding.
Pygame also supports letting you use a Rect as the position for an image when you want to 'blit' it onto the screen.
There are many ways you can do collision detection in Pygame. The Rect(@Darthfett) and spritecollide methods are the more popular ones, but there are 2 others that you can use:
Method 1:
This method is really easy to program, but does increase the sloppiness of coding, has many restrictions, and usually is not the best thing to use.
What you do is choose a certain color, check the pixel colors around the object, and make it collide if you detect a certain color. This allows simple games, like "Don't Let Your Mouse Touch The Color Black", and other simple programs.
Method 2:
This method is by far my favorite, and I use this in 90% of all my programs. You detect the collision by calculation. This is where you take the coordinates of objects 1 and 2, see how close they are to each other, and do collisions based off of that. That method allows lots of flexibility with collisions, isn't too sloppy if done right, and is quite simple. It may take a bit of tweaking, and you will need to test the program many times, but the results are well worth it. This method also allows detection between invisible objects, and it allows the easy adjustment of collision boxes, e.g. if there is a wizard, or just anything, and you die when you come a certain distance from it. This method really is not as popular as it should be, but I assure you it works. The 2 easiest forms of collision with this method are with circular and with rectangular collision.
Create a function that checks for the x, y, w, h perimeters of both objects and create an if statement as such that will check if the two objects are colliding:
Now you can just call the function with the objects perimeters as the arguments.
then make collision groups and use
pygame.sprite.spritecollide()
Yes, but make sure to use sprite classes for the rectangles, sprite info available here. So, for each sprite make a rect:
I know it looks like a lot, and thats what I thought when I started with sprites, but trust me, it is totally worth it.