本程序需要python精灵模块支持,需要安装请在运行对话框输入cmd,然后输入以下命令:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple sprites –upgrade
以下是完整源代码:
"""
三bug多线程示例程序.py
本程序创建了三个线程,每个线程都会创建一个bug。
bug定位后会不断地旋转。
"""
from threading import *
from sprites import Sprite,Screen
def myt1():
bug = Sprite()
while True:
bug.fd(1)
bug.rt(1)
def myt2():
bug = Sprite()
bug.goto(100,100)
while True:
bug.fd(1)
bug.lt(1)
def myt3():
bug = Sprite()
bug.goto(-100,100)
while True:
bug.fd(1)
bug.lt(1)
screen = Screen()
thread1 = Thread(target=myt1) # 创建一个线程
thread2 = Thread(target=myt2) # 创建一个线程
thread3 = Thread(target=myt3) # 创建一个线程
thread1.start() # 启动线程1
thread2.start() # 启动线程2
thread3.start() # 启动线程3
screen.mainloop()

