线程安全的同时移动海龟

"""
   线程安全的同时移动海龟
   本程序演示36只海龟同时移动效果.
   这是用screen的timer定时器无法做到的.
   
"""
from queue import Queue
from turtle import Screen, Turtle
from threading import Thread, active_count

QUEUE_SIZE = 1
amounts = 36
colors = ['red','orange','yellow','green','cyan','blue','purple']

def process_queue():
    while not actions.empty():
        # 获取动作及其参数,在这里就是forward和1
        action, *arguments = actions.get()
        action(*arguments)

    if active_count() > 1:
        screen.ontimer(process_queue, 100)
         
screen = Screen()
screen.delay(0)
screen.setup(800,640)
screen.title("线程安全的同时移动海龟")

ts = []         # 海龟列表
one = Turtle('turtle', visible=False)
one.color('red')
ts.append(one)
for h in range(1,amounts):   # 克隆其它海龟
    t = one.clone()
    t.color(colors[h%len(colors)])
    t.setheading(h * 360/amounts)
    ts.append(t)
  

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