冒泡排序彩柱图动态演示_python bubble sort dynamic show

"""
   冒泡排序彩柱图演示.py

"""

from turtle import *
from random import randint
from time import sleep

class Column(Turtle):
    def __init__(self,x):
        Turtle.__init__(self,shape='square')
        """形状square的初始大小为20x20,所以半高就是10"""        
        self.up()                  # 抬笔
        r = randint(0,255)
        g = randint(0,255)
        b = randint(0,255)        
        self.fillcolor(r,g,b)
        self.scale = randint(1,38) # 20 * self.scale 为柱子高度
        self.setx(x)
        # 屏幕的半高
        self.__half__ = self.screen.window_height()//2
        # 相对于自身坐标系前进的垂直方向放大
        self.shapesize(self.scale,1)                   
        """10*self.scale是柱子的半高,往下移屏幕半高,
          再抬高自身高一半加30,让所有柱子和屏幕底边对齐"""
        self.sety(0- self.__half__ + 10*self.scale  + 30)  
        

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