多线程碰到边缘就反弹弹球例子_multithread bounce on edge turtle example

你好,请进入Q群:225792826,免费下载本站最新提供的视频教程原创源码资料,从2020年2月20号到3月底,成为本站会员只要原价的3折,即2980元*0.3 = 894元 ,向微信scratch8付款即可成为本站会员。即可拥有本站所有原创资源,并提供技术支持。如本消息修改,以最后时间的为准。

"""
   多线程碰到边缘就反弹弹球例子_multithread bounce on edge turtle example 
"""
from time import sleep
from queue import Queue
from random import randint
from turtle import Screen, Turtle
from threading import Thread, active_count

QUEUE_SIZE = 1 

def bounce_on_edge(turtle):
    """
       碰到边缘就反弹的一个线程
    """
    dx = randint(-5,5)
    dy = randint(-5,5)
    x, y = turtle.position()
    screen = turtle.getscreen()
    width = screen.window_width()
    height = screen.window_height()
    while True:        
        if abs(x) > width//2: dx = -dx
        if abs(y) > height//2 : dy = -dy
        x = x + dx
        y = y + dy
        actions.put((turtle.goto,  (x,y)))        

def process_queue():
    """处理队列,动作列表不空就执行"""
    while not actions.empty():
        action, argument = actions.get()
        action(argument)
        screen.update()

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

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

  

本站所有作品,教程等皆为原创,盗版必究。一旦付款,表示同意本站知识付费原则:数字商品,不支持退款。亦可直接向微信号scratch8付款购买。QQ群免费教学群:225792826 (提供真实姓名可入群)
李兴球的博客_Python创意编程技术前沿 » 多线程碰到边缘就反弹弹球例子_multithread bounce on edge turtle example

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

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