我正在用Zelle graphics.py制作一个圣诞灯笼,我想让中间的圆圈闪烁随机的彩色灯光

2024-06-28 11:22:54 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码。我想制作一个带灯的圣诞灯笼。灯光是我评论的中间圆圈。我似乎不知道如何创建闪烁的随机彩色灯,以看起来像圣诞灯

代码:

from graphics import *
import random, time

def lantern():
    win = GraphWin('Lantern', 800, 800)

    #3 Large Circles
    c = [[400,300], [400,300], [400,300]]

    largeCircle = [250, 180, 100]

    for i in range (len(largeCircle)):
        c[i] = Circle(Point(c[i][0] ,c[i][1]), largeCircle[i])

    #10 Medium Circles
    mc = [[465,95], [560,155], [610,250], [610,360], [550,455], [245,447], [193,350], [193,250], [245,150], [350,92]]


    for i in range (len(mc)):
        mc[i] = Circle(Point(mc[i][0], mc[i][1]), 25)

    #11 Small Circles
    sc = [[450,160], [520,200], [550,265], [545,344], [508,410], [400,450], [290,405], [250,339], [250,265], [285,195], [355,155]]

    for i in range (len(sc)):
        sc[i] = Circle(Point(sc[i][0], sc[i][1]), 15)

    #5 XtraSmall Circles
    xsc = [[453,237], [474,325], [400,365], [325,325], [350,237]]

    for i in range (len(xsc)):
        xsc[i] = Circle(Point(xsc[i][0], xsc[i][1]), 8)

    #16 Lines
    l1 = [[532,90], [540,93], [620,180], [622,185], [613,430], [609,435], [520,520], [515,520], [310,457], [305,454], [240,380], [235,378], [178,187], [180,182], [265,90], [270,88]]
    l2 = [[500,150], [505,156], [555,210], [558,215], [555,390], [550,396], [490,454], [485,456], [275,519], [270,516], [180,420], [178,415], [242,216], [244,212], [300,150], [304,150]]
    n = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    for i in range (len(n)):
        n[i] = Line(Point(l1[i][0], l1[i][1]), Point(l2[i][0],l2[i][1]))


    #Star
    p1 = Polygon(Point(400,200), Point(440,260), Point(498,280), Point(450,320), Point(470,370), Point(400,345), Point(327,369), Point(345,320), Point(303,280), Point(360,260))

    #Red Box
    p2 = Polygon(Point(400,480), Point(480,560), Point(400,650), Point(325,560))

    #Triangle
    p3 = Polygon(Point(400,120), Point(437,207), Point(503,153), Point(478,238), Point(556,213), Point(500,280), Point(580,305), Point(495,330), Point(555,395), Point(470,373), Point(488,455), Point(400,400), Point(307,454), Point(328,370), Point(235,378), Point(305,330), Point(220,303), Point(303,280), Point(242,215),Point(330,230), Point(300,150), Point(370,205)) 

    r1 = Rectangle(Point(650,300),Point(580,305))
    r2 = Rectangle(Point(397,50),Point(404,120))
    r3 = Rectangle(Point(220,300),Point(150,305))


    for i in range(3):
        c[i].draw(win)
        c[i].setWidth(3)

    c[0].setFill('lavender')
    c[1].setFill('orange')
    c[2].setFill('red')

    for i in range(10):
        r = random.randrange(256)
        b = random.randrange(256)
        g = random.randrange(256)
        colors = color_rgb(r, g, b)
        mc[i].draw(win)
        mc[i].setWidth(3)
        mc[i].setFill(colors)

    for i in range(11):
        sc[i].draw(win)
        sc[i].setWidth(3)

    for i in range(5):
        xsc[i].draw(win)
        xsc[i].setWidth(3)

    for i in range(16):
        n[i].draw(win)
        n[i].setFill('brown')
        n[i].setWidth(3)


    p1.draw(win)
    p1.setWidth(3)
    p1.setFill('yellow')

    p2.draw(win)
    p2.setFill("red")
    p2.setWidth(3)

    p3.draw(win)
    p3.setWidth(3)
    p3.setOutline('purple')

    r1.draw(win)
    r1.setOutline('brown')
    r1.setWidth(3)

    r2.draw(win)
    r2.setOutline('brown')
    r2.setWidth(3)

    r3.draw(win)
    r3.setOutline('brown')
    r3.setWidth(3)

    message = Text(Point(400,560), '福')
    message.setSize(30)
    message.setFill('gold')
    message.draw(win)

    win.getMouse()
    win.close()

lantern()

Tags: inforlenrangerandommcwinpoint
1条回答
网友
1楼 · 发布于 2024-06-28 11:22:54

这里有一个方法。它部分基于Zelle graphics模块在线documentationControlling Display Updates (Advanced)部分中的信息

对代码所做的修改用# ALL CAPS注释表示。GraphWin的构造及其关闭已从lantern()函数中移出,并添加了一个循环以定期更新表示灯光的圆

由于将多次重画灯光,因此添加了一个名为draw_mcs()的函数,并在循环中重复调用该函数,以生成动画的连续“帧”,使灯光显示为闪烁,偶尔将其绘制为黑色。您可以通过在循环结束时传递给update()函数的整数参数来控制速度,该参数是每秒重绘它们的次数

为了在lantern()draw_mcs()函数之间共享数据,声明了两个列表global。其中一个是新的,并存储每个灯光的颜色,因此它将闪烁回原来的颜色

from graphics_5p0 import *
import random, time

BLACK = color_rgb(0,0,0)
RATE = 5


def lantern(win):
    global colors  # ADDED
    global mc      # ADDED

#    win = GraphWin('Lantern', 800, 800)  # MOVED.

    #3 Large Circles
    c = [[400,300], [400,300], [400,300]]

    largeCircle = [250, 180, 100]

    for i in range (len(largeCircle)):
        c[i] = Circle(Point(c[i][0] ,c[i][1]), largeCircle[i])

    #10 Medium Circles
    mc = [[465,95], [560,155], [610,250], [610,360], [550,455], [245,447], [193,350], [193,250], [245,150], [350,92]]


    for i in range (len(mc)):
        mc[i] = Circle(Point(mc[i][0], mc[i][1]), 25)

    #11 Small Circles
    sc = [[450,160], [520,200], [550,265], [545,344], [508,410], [400,450], [290,405], [250,339], [250,265], [285,195], [355,155]]

    for i in range (len(sc)):
        sc[i] = Circle(Point(sc[i][0], sc[i][1]), 15)

    #5 XtraSmall Circles
    xsc = [[453,237], [474,325], [400,365], [325,325], [350,237]]

    for i in range (len(xsc)):
        xsc[i] = Circle(Point(xsc[i][0], xsc[i][1]), 8)

    #16 Lines
    l1 = [[532,90], [540,93], [620,180], [622,185], [613,430], [609,435], [520,520], [515,520], [310,457], [305,454], [240,380], [235,378], [178,187], [180,182], [265,90], [270,88]]
    l2 = [[500,150], [505,156], [555,210], [558,215], [555,390], [550,396], [490,454], [485,456], [275,519], [270,516], [180,420], [178,415], [242,216], [244,212], [300,150], [304,150]]
    n = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

    for i in range (len(n)):
        n[i] = Line(Point(l1[i][0], l1[i][1]), Point(l2[i][0],l2[i][1]))

    #Star
    p1 = Polygon(Point(400,200), Point(440,260), Point(498,280), Point(450,320), Point(470,370), Point(400,345), Point(327,369), Point(345,320), Point(303,280), Point(360,260))

    #Red Box
    p2 = Polygon(Point(400,480), Point(480,560), Point(400,650), Point(325,560))

    #Triangle
    p3 = Polygon(Point(400,120), Point(437,207), Point(503,153), Point(478,238), Point(556,213), Point(500,280), Point(580,305), Point(495,330), Point(555,395), Point(470,373), Point(488,455), Point(400,400), Point(307,454), Point(328,370), Point(235,378), Point(305,330), Point(220,303), Point(303,280), Point(242,215),Point(330,230), Point(300,150), Point(370,205))

    r1 = Rectangle(Point(650,300),Point(580,305))
    r2 = Rectangle(Point(397,50),Point(404,120))
    r3 = Rectangle(Point(220,300),Point(150,305))

    for i in range(3):
        c[i].draw(win)
        c[i].setWidth(3)

    c[0].setFill('lavender')
    c[1].setFill('orange')
    c[2].setFill('red')

    colors = []  # ADDED
    for i in range(10):
        r = random.randrange(256)
        b = random.randrange(256)
        g = random.randrange(256)
        color = color_rgb(r, g, b)         # ADDED
        colors.append(color)               # ADDED
        mc[i].draw(win)
        mc[i].setWidth(3)
        mc[i].setFill(color)  # CHANGED


    for i in range(11):
        sc[i].draw(win)
        sc[i].setWidth(3)

    for i in range(5):
        xsc[i].draw(win)
        xsc[i].setWidth(3)

    for i in range(16):
        n[i].draw(win)
        n[i].setFill('brown')
        n[i].setWidth(3)


    p1.draw(win)
    p1.setWidth(3)
    p1.setFill('yellow')

    p2.draw(win)
    p2.setFill("red")
    p2.setWidth(3)

    p3.draw(win)
    p3.setWidth(3)
    p3.setOutline('purple')

    r1.draw(win)
    r1.setOutline('brown')
    r1.setWidth(3)

    r2.draw(win)
    r2.setOutline('brown')
    r2.setWidth(3)

    r3.draw(win)
    r3.setOutline('brown')
    r3.setWidth(3)

    message = Text(Point(400,560), '福')
    message.setSize(30)
    message.setFill('gold')
    message.draw(win)

#    win.getMouse()  # MOVED.
#    win.close()     # MOVED.

# ADDED.
def draw_mcs(win):
    """ Redraw the mc circles and change their color to black once in a while. """
    global colors  # ADDED
    global mc      # ADDED

    for i in range(10):
        mc[i].undraw()
        mc[i].draw(win)
        mc[i].setWidth(3)
        color = colors[i] if random.randint(0, RATE) else BLACK
        mc[i].setFill(color)  # CHANGED

# MAIN LOOP.
win = GraphWin('Lantern', 800, 800, autoflush=False)
lantern(win)
for _ in range(100):
    draw_mcs(win)
    update(10)

win.getMouse()
win.close()

相关问题 更多 >