2014年12月4日 星期四

HW01_1_改變圖形及其個數與色彩大小等

from turtle import *

def stop_running():
    global running
 
    running= False

def create_compound_shape(basic_shape= 'square'):
    '''
    由基本形狀,製造出複合形狀。
    '''
    shape(basic_shape)  

    f=   .7 # fraction
    a=   10 # angle
    s=    6 # size
    c=    1 # color
     
    sh= Shape("compound")
 
    #
    # 用 8個 基本形狀 結合成 1個 複合形狀
    #
    for i in range(8):
     
        shapesize(s)
        tilt(a)
     
        p=  get_shapepoly()

        c1= (  c, 0.2, 1-c)
        c2= (1-c, 0.8,   c)
     
        sh.addcomponent(p, c1, c2)
     
        s *= f
        c *= f

    return sh

def create_dancers():

    global dancers

    clearscreen()
    bgcolor("blue") # 背景色。
    tracer(False)
 
    sh= create_compound_shape()
    register_shape('dancer', sh)
    shape('dancer')
 
    pu(); goto(0, -200)
 
    dancers= []
    for i in range(180):
        fd(8); lt(2); tilt(4)

        if i % 10 == 0:
            d=     clone()
            dancers += [d]

    home()

    update()


def dancers_running():
 
    global running
    global dancers
 
    running= True
 
    t= 0
    while running:

     
        for d in dancers:
            d.fd(8); d.lt(2); d.tilt(4)

   
        rt(4)    

   
        t %= 100
        s= t if t<50 else (100-t)
        if s==0: s += .1

        shapesize(s)

        t += .1

        update()
     
def write_mesage():
    '''
    按鍵說明。
    '''
    pencolor('white')
 
    goto(-300,300);
    write("Key-x:     start, 開始", font= 30)
 
    goto(-300,280);
    write("Key-x: stop, 停", font= 30)

    pencolor('black')

    goto(-70,0);
    write("轉吧轉吧八彩正方形", font= 30)
 
    home()

def main():
    global running, dancers

    create_dancers()

    write_mesage()
 
    onkey(dancers_running,'z')     # 用 'z' 鍵 來開始
    onkey(stop_running,   'xx') # 用 'x' 來停止

    listen()
 
    return "DONE!"


if __name__=='__main__':

    msg= main()
    print(msg)
    mainloop()

0 個意見:

張貼留言

訂閱 張貼留言 [Atom]

<< 首頁