34

当我给女同学用python画了个圣诞树后……

 3 years ago
source link: https://blog.csdn.net/weixin_45920495/article/details/121963902
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

当我给女同学用python画了个圣诞树后……

专栏收录该内容
1 篇文章 0 订阅

嗨,彦祖们,不会过圣诞了还是一个人吧?今天我们来讲一下如何用python来画一个圣诞树,学会就快给那个她发过去吧,我的朋友圈已经让圣诞树刷屏了!这不就来给各位彦祖们教了,看完记得给个三连哦!

请添加图片描述请添加图片描述

请添加图片描述
请添加图片描述
当然都是好看的,但是码神莫,就是要搞点不一样的用,python来画一个!或者搞个前端来画!

这次我们用的是python中的turtle(海龟)库,是python中的一个内置库不需要额外安装

setup()函数,其中的参数为width,height,startx=None,starty=None,分别表示窗口宽,高,及,窗口在电脑屏幕上的横坐标和纵坐标。

turtle.setup(800,600)

在这里插入图片描述

三个属性函数

pensize(<width>) #粗细
speed(speed) #运动速度
color(color) #设置画笔颜色

这就要提到海龟的坐标体系了,以窗口中心为原点,以右为正方向,上方为y轴正方向。

  1. forward(distance) #向前移动
  2. backward(distance) #向后移动
  3. goto(x,y=None)# 移动到相应的位置

和上面的移动大同小异,都是3项控制
4. right(degree)#向右转动
5. left(degree)#向左转动
6. seth(degree)#转动到某个方向

图形绘制,图像填充

这个今天用不到,也就不说了,欢迎爱好的彦祖们,评论区见!

from turtle import *
import random #随机,用来搞小圆点
import time

n = 100.0

speed("fastest") #速度快
screensize(bg='seashell')
left(90)
forward(3 * n)
color("orange", "yellow")
begin_fill()
left(126)

for i in range(5):
    forward(n / 5)
    right(144)
    forward(n / 5)
    left(72)
end_fill()
right(126)

color("dark green")
backward(n * 4.8)


def tree(d, s):
    if d <= 0: return
    forward(s)
    tree(d - 1, s * .8)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    tree(d - 3, s * .5)
    right(120)
    backward(s)


tree(15, n)
backward(n / 2)

for i in range(200):
    a = 200 - 400 * random.random()
    b = 10 - 20 * random.random()
    up()
    forward(b)
    left(90)
    forward(a)
    down()
    if random.randint(0, 1) == 0:
        color('tomato')
    else:
        color('wheat')
    circle(2)
    up()
    backward(a)
    right(90)
    backward(b)

time.sleep(60) #进程推迟时间

在这里插入图片描述

本文纯属标题党,如有雷同,必然不是码神,哈哈哈


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK