当前位置: 首页 > news >正文

有关python作画

import turtle
import random

========== 初始化设置 ==========

screen = turtle.Screen()
screen.setup(900, 700)
screen.bgcolor("#87CEEB")
screen.title("Python 精美风景画")
screen.tracer(0)
screen.colormode(255)

pen = turtle.Turtle()
pen.speed(0)
pen.pensize(1)

========== 渐变天空 ==========

def draw_gradient_sky():
"""从深蓝到浅蓝的天空渐变"""
for i in range(150, -1, -1):
y = -200 + i * 2
r = max(0, 135 - i // 2)
g = max(0, 206 - i // 3)
b = 235
pen.penup()
pen.goto(-450, y)
pen.pendown()
pen.color(r, g, b)
pen.begin_fill()
pen.goto(450, y)
pen.goto(450, y + 2)
pen.goto(-450, y + 2)
pen.end_fill()

========== 远山 ==========

def draw_mountains():
"""绘制多层远山"""
mountains = [
(-400, -120, 250, 180, "#6B8E9B"),
(-250, -120, 200, 150, "#7A8B9B"),
(-100, -120, 220, 170, "#5C7A8C"),
(100, -120, 260, 190, "#6B8E9B"),
(280, -120, 230, 160, "#7A8B9B"),
]
for x, y, w, h, color in mountains:
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color(color)
pen.begin_fill()
pen.goto(x + w // 2, y + h)
pen.goto(x + w, y)
pen.goto(x, y)
pen.end_fill()

========== 草地 ==========

def draw_grassland():
"""绘制草地底色和纹理"""
# 草地底色
pen.penup()
pen.goto(-450, -120)
pen.pendown()
pen.color("#4CAF50")
pen.begin_fill()
for _ in range(2):
pen.forward(900)
pen.right(90)
pen.forward(280)
pen.right(90)
pen.end_fill()

# 草地纹理(小草)
pen.color("#2E7D32")
pen.pensize(2)
for _ in range(150):x = random.randint(-420, 420)y = random.randint(-110, 80)pen.penup()pen.goto(x, y)pen.pendown()pen.setheading(random.randint(60, 120))pen.forward(random.randint(8, 15))pen.backward(random.randint(8, 15))

========== 太阳 ==========

def draw_sun():
"""绘制带光晕的太阳"""
pen.penup()
pen.goto(200, 160)
pen.pendown()

# 光晕效果(多层叠加)
for r in range(80, 40, -10):pen.color(255, 255, 180)pen.begin_fill()pen.circle(r)pen.end_fill()pen.color(255, 255, 220)pen.begin_fill()pen.circle(r - 3)pen.end_fill()# 太阳主体
pen.color("#FFD700")
pen.begin_fill()
pen.circle(45)
pen.end_fill()# 太阳光线
pen.color("#FFA500")
pen.pensize(3)
for angle in range(0, 360, 20):pen.penup()pen.goto(200, 160)pen.setheading(angle)pen.forward(65)pen.pendown()pen.forward(25)

========== 云朵 ==========

def draw_cloud(x, y, size=1.0):
"""绘制蓬松云朵"""
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color("#FFFFFF")
pen.begin_fill()

radii = [25, 35, 30, 28, 22]
offsets = [0, 30, 55, 80, 105]
for r, off in zip(radii, offsets):pen.penup()pen.goto(x + off * size, y)pen.pendown()pen.circle(r * size)
pen.end_fill()# 云朵阴影
pen.penup()
pen.goto(x + 5 * size, y - 10 * size)
pen.pendown()
pen.color("#D3D3D3")
pen.begin_fill()
for r, off in zip(radii, offsets):pen.penup()pen.goto(x + (off + 3) * size, y - 12 * size)pen.pendown()pen.circle(r * size * 0.8)
pen.end_fill()

========== 树木 ==========

def draw_tree(x, y, size=1.0):
"""绘制带多层树叶的树木"""
# 树干
pen.penup()
pen.goto(x - 8 * size, y)
pen.pendown()
pen.color("#8B4513")
pen.begin_fill()
pen.setheading(90)
for _ in range(2):
pen.forward(45 * size)
pen.right(90)
pen.forward(16 * size)
pen.right(90)
pen.end_fill()

# 树叶(4层,由深到浅)
leaf_colors = ["#1B5E20", "#2E7D32", "#388E3C", "#4CAF50"]
leaf_y = [35, 50, 65, 80]
leaf_radius = [22, 20, 18, 15]for color, y_offset, r in zip(leaf_colors, leaf_y, leaf_radius):pen.penup()pen.goto(x, y + y_offset * size)pen.pendown()pen.color(color)pen.begin_fill()pen.circle(r * size)pen.end_fill()

========== 花朵 ==========

def draw_flower(x, y):
"""绘制六瓣花"""
petal_colors = ["#FF6B6B", "#FF8E8E", "#FFB5B5", "#FF6B6B", "#FF8E8E", "#FFB5B5"]

for angle, color in zip(range(0, 360, 60), petal_colors):pen.penup()pen.goto(x, y)pen.setheading(angle)pen.forward(12)pen.pendown()pen.color(color)pen.begin_fill()pen.circle(7)pen.end_fill()# 花蕊
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color("#FFD700")
pen.begin_fill()
pen.circle(5)
pen.end_fill()

========== 蝴蝶 ==========

def draw_butterfly(x, y):
"""绘制蝴蝶"""
pen.penup()
pen.goto(x, y)
pen.pendown()

# 左翅膀
pen.color("#FF69B4")
pen.begin_fill()
pen.setheading(-60)
pen.circle(15, 90)
pen.setheading(30)
pen.circle(12, 90)
pen.end_fill()# 右翅膀
pen.begin_fill()
pen.setheading(240)
pen.circle(15, -90)
pen.setheading(330)
pen.circle(12, -90)
pen.end_fill()# 身体
pen.color("#333333")
pen.pensize(3)
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.setheading(90)
pen.forward(8)
pen.backward(16)
pen.pensize(1)

========== 小房子 ==========

def draw_house(x, y):
"""绘制小房子"""
# 墙体
pen.penup()
pen.goto(x - 25, y)
pen.pendown()
pen.color("#DEB887")
pen.begin_fill()
for _ in range(2):
pen.forward(50)
pen.right(90)
pen.forward(40)
pen.right(90)
pen.end_fill()

# 屋顶
pen.penup()
pen.goto(x - 30, y + 40)
pen.pendown()
pen.color("#CD5C5C")
pen.begin_fill()
pen.goto(x, y + 65)
pen.goto(x + 30, y + 40)
pen.goto(x - 30, y + 40)
pen.end_fill()# 门
pen.penup()
pen.goto(x - 8, y)
pen.pendown()
pen.color("#8B4513")
pen.begin_fill()
pen.setheading(90)
pen.forward(25)
pen.right(90)
pen.forward(16)
pen.right(90)
pen.forward(25)
pen.end_fill()# 窗户
pen.penup()
pen.goto(x + 8, y + 20)
pen.pendown()
pen.color("#87CEEB")
pen.begin_fill()
for _ in range(4):pen.forward(12)pen.right(90)
pen.end_fill()

========== 小溪 ==========

def draw_river():
"""绘制蜿蜒的小溪"""
pen.penup()
pen.goto(-400, -80)
pen.pendown()
pen.color("#4FC3F7")
pen.pensize(20)
pen.setheading(10)
for _ in range(30):
pen.forward(15)
pen.right(random.uniform(-5, 5))

# 水纹
pen.color("#B3E5FC")
pen.pensize(2)
for i in range(20):x = -350 + i * 35y = -60 + random.randint(-10, 10)pen.penup()pen.goto(x, y)pen.pendown()pen.setheading(0)pen.forward(20)

========== 小鸟 ==========

def draw_bird(x, y):
"""绘制飞翔的小鸟"""
pen.penup()
pen.goto(x, y)
pen.pendown()
pen.color("#2C3E50")
pen.pensize(2)
pen.setheading(-40)
pen.circle(12, 70)
pen.penup()
pen.goto(x + 18, y + 6)
pen.pendown()
pen.setheading(40)
pen.circle(12, -70)

========== 主程序 ==========

def main():
"""主绘制函数"""
# 背景层(从后往前)
draw_gradient_sky()
draw_mountains()
draw_river()
draw_grassland()

# 天空元素
draw_sun()# 云朵
clouds = [(-350, 220, 1.2), (-100, 250, 1.0), (150, 230, 0.9), (300, 200, 1.1)]
for x, y, size in clouds:draw_cloud(x, y, size)# 树木
trees = [(-320, -80, 0.9), (-220, -85, 1.1), (-120, -90, 1.0),(20, -95, 1.2), (130, -85, 0.9), (230, -80, 1.1), (320, -75, 1.0)]
for x, y, size in trees:draw_tree(x, y, size)# 花朵
flowers = [(-350, -95), (-280, -100), (-180, -100), (-80, -105),(60, -100), (160, -95), (260, -100), (350, -90)]
for x, y in flowers:draw_flower(x, y)# 蝴蝶
butterflies = [(-200, 50), (100, 60), (280, 40)]
for x, y in butterflies:draw_butterfly(x, y)# 小房子
draw_house(-280, -95)
draw_house(280, -85)# 小鸟
birds = [(-250, 180), (-50, 200), (180, 190), (350, 210)]
for x, y in birds:draw_bird(x, y)# 完成绘制
pen.hideturtle()
screen.update()
screen.mainloop()

if name == "main":
main()

http://www.jsqmd.com/news/613652/

相关文章:

  • 自动化内容审核:OpenClaw+Qwen3-4B-Thinking搭建个人防火墙
  • DDD难落地?就让AI干吧! - cleanddd-skills介绍缕
  • STM32 Modbus通信学习笔记——通信流程
  • 无网环境方案:OpenClaw离线调用SecGPT-14B的实践
  • 3个技巧让你彻底告别项目管理中的重复操作?
  • Linux 虚拟机无法访问外网
  • 2026新疆青少年行为矫正学校/叛逆特训学校/青少年心理辅导推荐名单:七强 - 栗子测评
  • 2026贵阳工程门窗配套发布:推荐5家源头工厂直营名单 - 精选优质企业推荐榜
  • Mac开发者必备:OpenClaw+千问3.5-9B自动化开发流水线
  • Java面试高频考点:HashMap核心机制
  • 2026年优质螺旋机厂家推荐:金拓机械领衔,精选靠谱螺旋提升机厂家与螺旋式提升机厂家汇总 - 栗子测评
  • 2026年从纳米定位到力觉感知:高端精密定位系统的技术路径与核心供应商剖析 - 品牌推荐大师1
  • AI编程助手功能扩展与开发工具限制解除方案研究
  • 不用写代码_成品源码_修改 LOGO 即可运营
  • 2026最新AI营销服务商 TOP5评测!广州权威榜单发布,赋能多场景智能增长 - 十大品牌榜
  • Win11Debloat:Windows 11终极去臃肿指南,一键恢复系统流畅体验
  • OpenClaw_Claude_共享知识库
  • OpenClaw可视化监控:Qwen3-4B-Thinking任务执行看板搭建
  • 重百世纪卡可以回收吗?最新全国通用方法,选对平台至关重要! - 畅回收小程序
  • 3种Element Plus访问优化方案:从根本解决跨境访问难题
  • 2026最新智能体开发代理商/运营商TOP5评测!广州权威榜单发布,AI赋能全场景增长 - 十大品牌榜
  • 兰州水泥制品厂哪家强?一文看懂:强固U型水泥排水沟、水泥雨水箅子、混凝土化粪池、水泥管、水泥检查井定制优质厂家全推荐 - 栗子测评
  • 4个设计场景:ReplaceItems如何让设计系统维护效率提升80%
  • 2026贵阳工程门窗配套公司发布5家可靠名单 - 精选优质企业推荐榜
  • 万题斩App:你的口袋备考神器,随时随地高效刷题
  • Linux日常学习4
  • 恒星物联服务团队专业吗,提供哪些地区服务 - 工业设备
  • JavaScript判断字符是否为decimal
  • 全套源码资源_含前端 + 后端 + 数据库 + 部署文档
  • 两电平三相四桥臂逆变器(800V-380V整流)仿真:从搭建到动稳态性能的详细介绍与原理出处