新手練習(xí)视卢,具體總代碼:
#總代碼
import turtle
import random
def random_color():
color_list=['0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F']
color = ''
for i in range(6):
color_number = color_list[random.randint(0,15)]
color += color_number
color = '#' +color
return color
def draw_square(size):
for i in range(4):
turtle.forward(size)
turtle.right(90)
def main():
for i in range(20):
x = random.randrange(-200,200)
y = random.randrange(-200,200)
turtle.penup()
# goto(x,y)移動(dòng)到x早像,y所確定的那個(gè)電上
turtle.goto(x,y)
turtle.pendown()
# turtle.pencolor(random_color())
turtle.color(random_color())
turtle.begin_fill()
size = random.randrange(-200,200)
draw_square(size)
turtle.end_fill()
turtle.exitonclick()
if __name__ =='__main__':
main()
實(shí)現(xiàn)效果圖
1.JPG