本人以前做了一个版本,这个是最新版本,基本不会更新了,付费后提供3个版本下载,你可以学到很多很多。
"""
冒泡排序彩柱图演示2021版2.py
"""
import turtle
from time import sleep
from random import randint
def randcolor():
"""产生随机RGB255三元组表示颜色"""
r = randint(0,255)
g = randint(0,255)
b = randint(0,255)
return r,g,b
def draw_rect(rect):
"""rect:矩形对象,
本函数使用turtle画一个矩形
"""
turtle.color(rect.color)
turtle.goto(rect.pos)
turtle.begin_fill()
for _ in range(2):
turtle.fd(rect.width)
turtle.left(90)
turtle.fd(rect.height)
turtle.left(90)
turtle.end_fill()
class Rect:
def __init__(self,x,y,w,h,c):
"""x,y:左下角坐标
w,h:宽高
c:颜色三元组
"""
self.pos = x,y
self.width = w
self.height = h
self.color = c
def draw_all_rects(rects):
# 下面是清除,然后重画所有矩形
turtle.clear() # 擦除所有
[draw_rect(r) for r in rects] # 重新画所有矩形
turtle.update() # 刷新屏幕显示
sleep(0.4) # 等待0.4秒
def main():
"""主要执行函数"""
width,height=800,800
caption = '冒泡排序动态演示2021版,作者:李兴球 2021/1/4'
turtle.penup() # 抬笔
turtle.speed(0) # 海龟移动速度为最快
turtle.hideturtle() # 隐藏海龟对象
turtle.tracer(0,0)
turtle.colormode(255) # 设定颜色模式
turtle.setup(width,height) # 设定窗口宽高
turtle.title(caption)
tom = turtle.Turtle(visible=False) # 显示标题的
tom.up() # tom抬笔
ft = ('黑体',22,'normal') # 定义字体风格
tom.color('gray') # 灰色的
tom.goto(0,280) # 坐标定位
tom.write(caption,align='center',font=ft)# 写上汉字
pass # 从这里开始省略一些代码
if __name__=="__main__":
main()
需要所有源代码,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)

