最简基于mask的overlap命令检测图形冲突

"""
   创建圆环和实心圆,把它们渲染到screen上的同一个坐标。
   这样,小圆是在大圆里面,它们是没有碰撞在一起的。
   本例是用基于mask的overlape命令,从而检测出它们是没有重叠的。
"""
import pygame
from pygame.locals import *

red = (255,0,0)
blue = (0,0,255)
w,h, = size = 480,360
pygame.init()
screen = pygame.display.set_mode(size)

# 创建大的圆环
radius1 = 50
width1,height1 = 2*radius1,2*radius1
hollow_circle = pygame.Surface((width1,height1),SRCALPHA)
pos = width1//2,height1//2                # 画圆中心坐标
rect1 = hollow_circle.get_rect(center=(w//2,h//2))
pygame.draw.circle(hollow_circle,red,pos,radius1,10)
mask1 = pygame.mask.from_surface(hollow_circle)
screen.blit(hollow_circle,rect1)

You must log in to read the rest of this article. Please log in or register as a user.