python turtle 画几株草

python turtle 画几株草

import turtle as tl
import random as rd
import math as mt


def write(n, t):
    print(n)
    tl.pensize(mt.sqrt(n+1)/10)
    tl.pencolor(0.3, 0.9, 0.3)
    tl.right(50 / (n+1))
    tl.forward(n/20)

    def pt(n):
        d = rd.randint(0, 1)
        if d:
            tl.left(15*n/t)
            tl.forward(10*n/t)
            tl.backward(10*n/t)
            tl.right(15*n/t)
        else:
            tl.right(15 * n / t)
            tl.forward(10 * n/t)
            tl.backward(10 * n/t)
            tl.left(15 * n / t)

    if n > 0:
       pt(n)
       write(n-1, t)


def move(x, y):
    tl.penup()
    tl.goto(x,y)
    tl.pd()


def m(n, t, Ysize, angle):
    Y = Ysize
    tl.right(50 / (n+1))
    tl.pensize(mt.sqrt(n)/5)
    tl.pencolor(0.1, 0.7, 0.1)
    tl.forward(25*n/t)
    if( n > 0 ):
        q1 = 45 * n / t
        q2 = 45 * n / t
        list = tl.position()
        x1 = list[0]
        y1 = list[1]
        angle = angle-50 / (n+1)
        tl.left(q1)
        write(Y*n, Y*n)
        move(x1, y1)
        tl.setheading(angle)
        tl.right(q2)
        write(Y*n, Y*n)
        move(x1, y1)
        tl.setheading(angle)
        #tl.right(180-45 * n / t)
        m(n-1, t, Ysize, angle)


def _main_():
    tl.screensize(canvwidth=1000, canvheight=1000)
    tl.speed(0)
    tl.bgcolor(1, 1, 1)
    tl.left(90)
    m(15, 15, 2.5, 90)
    move(-200, 0)
    tl.setheading(90)
    m(10, 10, 2, 90)
    move(200,0)
    tl.setheading(90)
    m(20, 20, 3, 90)
    tl.hideturtle()
    tl.mainloop()
    
    
_main_()

在这里插入图片描述

  • 9
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是用Python编写的turtle草地的代码: ```python import turtle import random turtle.setup(800, 800) turtle.speed(0) turtle.bgcolor('sky blue') def draw_grass(x, y): turtle.penup() turtle.goto(x, y) turtle.pendown() turtle.color('green') turtle.begin_fill() turtle.forward(20) turtle.right(90) turtle.forward(40) turtle.right(90) turtle.forward(20) turtle.left(90) turtle.forward(20) turtle.right(90) turtle.forward(20) turtle.left(90) turtle.forward(20) turtle.right(90) turtle.forward(20) turtle.left(90) turtle.forward(20) turtle.right(90) turtle.forward(20) turtle.left(90) turtle.forward(20) turtle.right(90) turtle.forward(20) turtle.left(90) turtle.forward(20) turtle.right(90) turtle.forward(20) turtle.left(90) turtle.forward(20) turtle.right(90) turtle.forward(20) turtle.left(90) turtle.forward(10) turtle.right(90) turtle.forward(40) turtle.right(90) turtle.forward(10) turtle.end_fill() while True: x = random.randint(-400, 400) y = random.randint(-400, -200) draw_grass(x, y) ``` 运行这段代码,你就能看到一个天蓝色的布上,随机生成的绿色草地的效果。这个程序使用了Pythonturtle模块来绘制图形,函数`draw_grass`用来绘制一株,通过传入参数`x`和`y`来确定的位置。的形状比较复杂,需要用到多个`forward`和`right`或`left`命令,颜色使用了绿色,并且使用了`turtle.begin_fill()`和`turtle.end_fill()`命令来填充颜色。最后,通过不断生成随机数来实现的随机位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值