HomePython Page 3 - PyGame for Game Development: Sprite Groups and Collision Detection
Collision - Python
In this second part of a tutorial covering PyGame, you will learn how to manage entire groups of sprites. You'll also learn how to test for collisions within your applications.
Sprite groups also provide a simple way to test for collision within your applications. PyGame contains three methods that allow us to test for collision, each behaving a bit differently than the rest. They are pygame.sprite.spritecollide, pygame.sprite.groupcollide and pygame.sprite.collideany. Let's create an example application featuring three non-playable StickMan sprites and one character StickMan sprite. A collision between the player and a non-playable sprite will create different results for each sprite:
# Add them each to a group group1 = pygame.sprite.Group() group2 = pygame.sprite.Group() group3 = pygame.sprite.Group() group1.add(stick1) group2.add(stick2) group3.add(stick3)
# Create a playable stickman and add it to a group player = StickMan((200,10)) playerGroup = pygame.sprite.RenderUpdates() playerGroup.add(player)
# Draw each sprite group1.draw(screen) group2.draw(screen) group3.draw(screen) playerGroup.draw(screen)
pygame.display.update()
while True:
for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit()
# Move the player if a key is pressed elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: player.update([-10, 0]) elif event.key == pygame.K_UP: player.update([0, -10]) elif event.key == pygame.K_RIGHT: player.update([10, 0]) elif event.key == pygame.K_DOWN: player.update([0, 10])
# Check to see if the player has collided with stick1 in group1 # If a collision is detected, kill stick1 (True) collide1 = pygame.sprite.spritecollide(player, group1, True)
# Check to see if the playerGroup has collided with group2 # Kill all involved sprites if there is a collision collide2 = pygame.sprite.groupcollide(playerGroup, group2, True, True)
# Check to see if the player has collided with anything in group3 collide3 = pygame.sprite.spritecollideany(player, group3)
# Print the test results print 'Test 1:', collide1 print 'Test 2:', collide2 print 'Test 3:', collide3
# If the player has died, print a game over message, wait and then quit if not player.alive(): print 'Game Over' pygame.time.wait(3000) sys.exit()
# Clear the player's old spot playerGroup.clear(screen, eraseSprite)
# Draw the player updateRects = playerGroup.draw(screen)
# If we need to redraw other things, then do so if collide1: group1.clear(screen, eraseSprite) group1.draw(screen) updateRects.append(stick1.rect) elif collide2: group2.draw(screen) updateRects.append(stick2.rect) elif collide3: group3.draw(screen) updateRects.append(stick3.rect)