线程安全的同时移动海龟

"""
   线程安全的同时移动海龟
   本程序演示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)
  

成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在python, turtle分类目录。将固定链接加入收藏夹。