新手练习,具体总代码:
#总代码
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)移动到x,y所确定的那个电上
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()
实现效果图
具体请看
用python画随机颜色随机大小的正方形
如果这篇文章对你有帮助的话,请你点个赞吧,你的点赞是我前进的动力🤞❤