This is a tutorial on the Collision detection between sprite(s) in Pygame.
One of the many advanced topics in game development in Collision detection. This refers to the collision between two Sprites (objects) in the game environment. In this pygame tutorial we’ll explain how to detect and deal with collisions.
In order to be able to implement collision detection, you first need to know how pygame detects collisions between sprites.
Every Sprite in pygame has (should) have a rect or “rectangle” object assigned to it. This rectangle object has the same width and height as the Sprite itself and represents it’s boundaries.
This “rectangle” around the player is obviously a hidden (not visible) one and used for scenarios like collision detection. Pygame comes with several functions that can detect if two or more rect objects are intersecting with each other, otherwise known as a “collision”.
Using the below method on a rect object, you can determine whether it is in contact with the rect object that has been passed into it’s parameters. In simpler words, you can determine whether Sprite A is in contact with Sprite B.
It returns True if the rectangles of the two objects overlap in anyway way, otherwise returns False.
Sprite Collision Functions
colliderect() is effective, but there are better collision detection functions in Pygame. The most commonly used function is
spritecollideany() which takes two parameters, a sprite and a group.
The benefit of this function is that no matter how large the group, it will detect whether or whether not the sprite is in collision with any of the sprites within the group. It will return True if a collision was detected, otherwise False. If no collision occurred, the value
None will be returned.
Remember that as we explained earlier, these collisions are judged based of the intersection of the rect objects of the sprites.
Another effective function is
spritecollideany() it occurs between two groups, which it takes as the first and second parameter.
The return value from this function is in the form of a dictionary where the colliding sprites of group1 are they keys and the value is the list of sprites in group2 that they intersect.
This function can also take third and fourth (optional) parameters called
dokill2, which take True or False values. If
dokill1 is True, the colliding sprites from group1 will be removed using the kill method. And if
dokill2 is True, sprites from group2 will be removed.
Collision Detection Example
Finally a real life example to complete the topic of Pygame Sprite collision detection. We’re taking this example from our pygame tutorial where we made a 2D car game where the goal was to dodge the incoming cars.
In such a game, we had to implement a system where we were continuously checking for the collision between the Player and the incoming cars. The below code was extracted from this car game. For the full code, refer to the tutorial.
if pygame.sprite.spritecollideany(P1, enemies): DISPLAYSURF.fill(RED) pygame.display.update() for entity in all_sprites: entity.kill() time.sleep(2) pygame.quit() sys.exit()
P1 is the player sprite object.
enemies is the sprite group in which the sprites of the incoming cars are stored.
DISPLAYSURF is the name of the pygame display (background/screen) and
all_sprites is a sprite group which contains all the sprites in the game.
The concept is simple. If the
spritecollideany()returns True, the if statement will execute. Since the objective is to make a game over screen, the screen will turn red and the display will update to reflect this change. Next all the sprites will be killed using the kill method and after two seconds the game and program will be closed respectively.
You can find another example of collision detection in our Pygame platformer series. The collision detection in this series is much more in depth so it will help you build your concept.
You can create your own system too of course. If you have a concept of multiple lives, a collision could simply remove one life instead of closing the entire game.
This marks the end of the Pygame Sprite Collision Detection Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.