鼠标指针跟随海龟多线程版

0 Comments

以前编写的程序是海龟跟随鼠标指针,现在倒过来了,这是利用事件产生方法实现的。

import time
import turtle
from threading import Thread

def mouse_follow_turtle(test_turtle):
    root = test_turtle.getscreen()._root    
    while True:        
        left = test_turtle.xcor() + 400
        top = 300 - test_turtle.ycor()
        root.event_generate("",warp=True,x=left,y=top)
        time.sleep(0.1)
        
def turtle_move(test_turtle):
     while True:
        test_turtle.fd(1)
        test_turtle.left(1)
        
if __name__ == '__main__':
    
    turtle.setup(800,600)
    tom = turtle.Turtle('turtle')
    t1 = Thread(target=mouse_follow_turtle,args=(tom,))
    #t1.setDaemon(True)
    t1.start()
    t2 = Thread(target=turtle_move,args=(tom,))
    #t2.setDaemon(True)
    t2.start()   

    turtle.mainloop()
        


发表评论