color_collide_color命令测试程序

color_collide_color命令测试程序

李兴球python颜色碰到颜色color_collide_color

"""
   color_collide_color命令测试程序
   本程序用一个十字架彩色图形去和彩色小方块进行重叠,
   会显示出重叠区域的颜色。本程序需要gameturtle0.2版支持。
"""
from gameturtle import *
from random import randint,choice
from PIL import ImageColor

w,h = 480,360                    # 定义画布宽高

cs = ['red','orange','yellow','green','cyan','lime',
      'blue','purple','pink','magenta','gray','gold']

cs = [ImageColor.getcolor(c,'RGB') for c in cs]

root = Tk()
root.title('color_collide_color颜色碰撞检测命令')

cv = Canvas(width=w,height=h,bg='black')
cv.pack()

# 生成12个彩色小方块
for c in cs:
    x = randint(0,w)
    y = randint(0,h)
    pic = Image.new("RGBA",(20,20),color=c)
    Sprite(cv,pic,pos=(x,y))

cross_cs = [(255,0,51),(0,255,0),(0,0,153),(255,204,51)]
cross_pic = Image.open('彩色十字架.png')
cross = GameTurtle(cv,cross_pic)

while True:
    mx,my = cv.mouse_pos()
    cross.goto(mx,my)

    for c1 in cross_cs:
        for c2 in cs:
            if cross.color_collide_color(c1,c2):
                root.title(str(c1) + "碰到了" + str(c2))
    
    cv.update()
    time.sleep(0.01)
    

李兴球

李兴球的博客是Python创意编程原创博客