欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python之画图 turtle 练习题

程序员文章站 2022-07-01 19:09:32
...

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

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()

效果图如下:Python之画图 turtle 练习题

题二:同心圆

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()

效果图如下:Python之画图 turtle 练习题