Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import math
- # example of bit mask collision
- # fb.com/groups/pygame
- pygame.init()
- surface = pygame.display.set_mode((640, 480))
- black = [0, 0, 0]
- red = [255, 0, 0]
- blue = [0, 0, 255]
- # create two sprites
- surf1 = pygame.Surface((60, 60))
- pygame.draw.circle(surf1, red, (15, 15), 15, 0)
- pygame.draw.rect(surf1, red, (30, 30, 30, 30))
- surf2 = pygame.Surface((60, 60))
- pygame.draw.circle(surf2, blue, (45, 45), 15, 0)
- pygame.draw.rect(surf2, blue, (0, 0, 30, 30))
- # make the black background of the sprites transparent
- surf1.set_colorkey(black)
- surf2.set_colorkey(black)
- # get the sprites bit masks
- surf1_bitmask = pygame.mask.from_surface(surf1)
- surf2_bitmask = pygame.mask.from_surface(surf2)
- x = 290
- y = 180
- dead = False
- # press ESCAPE to exit
- while True:
- e = pygame.event.get()
- if pygame.key.get_pressed()[pygame.K_ESCAPE]: break
- mx, my = pygame.mouse.get_pos()
- # work out if the sprites bitmasks overlap
- if surf1_bitmask.overlap(surf2_bitmask, (mx - x, my - y)):
- dead = True
- surface.fill(black)
- if not dead: surface.blit(surf1, (x, y))
- surface.blit(surf2, (mx, my))
- pygame.display.update()
- pygame.time.Clock().tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement