Python之画图 turtle 练习题

有关画图的文章视频有很多,大家都可以很容易找到,我也不想再重复了,不过这里有几道很不错的题目,分享一下。
题一:彩虹糖

from turtle import *
from random import random
import contextlib

def draw_circle(r):
    a, b, c = random(), random(), random()
    #pencolor(a, b, c)
    fillcolor(a, b, c)
    begin_fill()
    circle(r)
    end_fill()

def pen_skip(step):
    penup()
    forward(step)
    pendown()

speed(5)
setup(width=800,height=600)
screensize(600,400, "gray")
long = 600
high = 450
left(180)
pen_skip(250)
left(90)
pen_skip(200)
left(90)
high_start = 50
high_step = 50
long_start = 50
long_step = 50
for i in range(high_start,high,high_step):
    for j in range(long_start,long,long_step):
        if (i//high_step)%2 ==1:
            if j == long-long_step:
                draw_circle(long_step//2)
                continue
            draw_circle(long_step // 2)
            pen_skip(long_step)
        else:
            if j == long-long_step:
                draw_circle(-long_step // 2)
                continue
            draw_circle(-long_step // 2)
            pen_skip(long_step)
    if (i//50)%2 == 1:
        left(90)
        pen_skip(high_step)
        left(90)
    else:
        right(90)
        pen_skip(high_step)
        right(90)
exitonclick()

效果图如下:颜色是根据三原色自动生成的

题二:同心圆

from turtle import *
import random
def pen_skip(step):
    penup()
    forward(step)
    pendown()

color = ['blue','red','yellow','pink','black']
for i in range(100,10,-10):
    fillcolor(random.sample(color,1)[0])
    begin_fill()
    circle(i)
    end_fill()
    left(90)
    pen_skip(10)
    right(90)

exitonclick()

效果图如下:颜色搭配的不是很好

猜你喜欢

转载自blog.csdn.net/qq_42849332/article/details/81670008