python turtle库的使用

本文分享了使用Python的Turtle模块绘制彩虹糖图案和同心圆的方法,通过随机颜色填充,创造出绚丽多彩的视觉效果。文章提供了完整的代码示例,适合初学者实践。

最近给女儿看了一个用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()

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

 

python 笔记之“海龟”画图 演示画小猪佩奇,机器猫:https://blog.youkuaiyun.com/qq_42179526/article/details/83031653

 

转载于:https://www.cnblogs.com/doudouhaha521/p/11510072.html

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值