多线程海龟移动动画例子_multithread turtle example

"""
   多线程海龟移动动画例子_multithread turtle example 
"""
from queue import Queue
from random import randint
from turtle import Screen, Turtle
from threading import Thread, active_count

QUEUE_SIZE = 1
turtle_SPEED = 3

def move_turtle(turtle, direction):
    """
       移动海龟线程,超过x坐标288就往下移50
    """
    x, y = turtle.position()

    while True:
        while direction == "right":

            if x > 288:
                y -= 50
                actions.put((turtle.sety, y))
                direction = "left"
            else:
                x += turtle_SPEED
                actions.put((turtle.setx, x))

        while direction == "left":
            if x < -288:
                y -= 50
                actions.put((turtle.sety, y))
                direction = "right"
            else:
                x -= turtle_SPEED
                actions.put((turtle.setx, x))
[rml_read_more]
def process_queue():
    """处理队列,动作列表不空就执行"""
    while not actions.empty():
        action, argument = actions.get()
        action(argument)

    if active_count() > 1:
        screen.ontimer(process_queue, 100)

actions = Queue(QUEUE_SIZE)

x, y = randint(-200, 200), randint(100, 200)

direction = "right"

for dy in range(2):
    for dx in range(2):
        turtle = Turtle("turtle", visible=False)
        turtle.speed('fastest')
        turtle.setheading(270)
        turtle.penup()
        turtle.setposition(x + dx * 60, y + dy * 100)
        turtle.showturtle()

        Thread(target=move_turtle, args=(turtle, direction), daemon=True).start()

    direction = ["left", "right"][direction == "left"]

screen = Screen()
 
process_queue()

screen.mainloop()

 

本站所有作品,教程等皆为原创,版权所有。只供个人及单位内部研究使用,对外展示或传播必需经本站同意,且注明来自本站。培训机构等用本站资源培训学生,需经本站授权。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。入住QQ群:225792826 和爱好者共同交流,并且能下载免费提供的Python资源(需提供真实姓名才可入群)
李兴球的博客_Python创意编程技术前沿_pygame » 多线程海龟移动动画例子_multithread turtle example
scratch

学本领,探索更大的世界!

李兴球博客 风火轮编程主页