|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +@author: 闲欢 |
| 5 | +""" |
| 6 | + |
| 7 | +import turtle as t |
| 8 | +from turtle import * |
| 9 | +import random as r |
| 10 | + |
| 11 | + |
| 12 | +n = 100.0 |
| 13 | + |
| 14 | +speed(10000) # 定义速度 |
| 15 | +pensize(5) # 画笔宽度 |
| 16 | +screensize(800, 800, bg='black') # 定义背景颜色,可以自己换颜色 |
| 17 | +left(90) |
| 18 | +forward(250) # 开始的高度 |
| 19 | +color("orange", "yellow") # 定义最上端星星的颜色,外圈是orange,内部是yellow |
| 20 | +begin_fill() |
| 21 | +left(126) |
| 22 | + |
| 23 | + |
| 24 | +# 画五角星 |
| 25 | +for i in range(5): |
| 26 | + forward(n / 5) |
| 27 | + right(144) # 五角星的角度 |
| 28 | + forward(n / 5) |
| 29 | + left(72) # 继续换角度 |
| 30 | +end_fill() |
| 31 | +right(126) |
| 32 | + |
| 33 | + |
| 34 | +# 定义画彩灯的方法 |
| 35 | +def drawlight(): |
| 36 | + if r.randint(0, 50) == 0: # 如果觉得彩灯太多,可以把取值范围加大一些,对应的灯就会少一些 |
| 37 | + color('tomato') # 定义第一种颜色 |
| 38 | + circle(3) # 定义彩灯大小 |
| 39 | + elif r.randint(0, 30) == 1: |
| 40 | + color('orange') # 定义第二种颜色 |
| 41 | + circle(4) # 定义彩灯大小 |
| 42 | + elif r.randint(0, 50) == 2: |
| 43 | + color('blue') # 定义第三种颜色 |
| 44 | + circle(2) # 定义彩灯大小 |
| 45 | + elif r.randint(0, 30) == 3: |
| 46 | + color('white') # 定义第四种颜色 |
| 47 | + circle(4) # 定义彩灯大小 |
| 48 | + else: |
| 49 | + color('dark green') # 其余的随机数情况下画空的树枝 |
| 50 | + |
| 51 | + |
| 52 | +color("dark green") # 定义树枝的颜色 |
| 53 | +backward(n * 4.8) |
| 54 | + |
| 55 | +# 画树 |
| 56 | +def tree(d, s): |
| 57 | + speed(100) |
| 58 | + if d <= 0: return |
| 59 | + forward(s) |
| 60 | + tree(d - 1, s * .8) |
| 61 | + right(120) |
| 62 | + tree(d - 3, s * .5) |
| 63 | + drawlight() # 同时调用小彩灯的方法 |
| 64 | + right(120) |
| 65 | + tree(d - 3, s * .5) |
| 66 | + right(120) |
| 67 | + backward(s) |
| 68 | + |
| 69 | + |
| 70 | +tree(15, 100) |
| 71 | +backward(50) |
| 72 | + |
| 73 | + |
| 74 | +# 循环画最底端的小装饰 |
| 75 | +for i in range(200): |
| 76 | + a = 200 - 400 * r.random() |
| 77 | + b = 10 - 20 * r.random() |
| 78 | + up() |
| 79 | + forward(b) |
| 80 | + left(90) |
| 81 | + forward(a) |
| 82 | + down() |
| 83 | + if r.randint(0, 1) == 0: |
| 84 | + color('tomato') |
| 85 | + else: |
| 86 | + color('wheat') |
| 87 | + circle(2) |
| 88 | + up() |
| 89 | + backward(a) |
| 90 | + right(90) |
| 91 | + backward(b) |
| 92 | + |
| 93 | + |
| 94 | +# 画雪人 (n,m)是头和身子交点的坐标,a是头的大小,m是身体的大小 |
| 95 | +def drawsnowman(n,m,a,b): |
| 96 | + speed(100) |
| 97 | + t.goto(n, m) |
| 98 | + t.pencolor("white") |
| 99 | + t.pensize(2) |
| 100 | + t.fillcolor("white") |
| 101 | + t.seth(0) |
| 102 | + t.begin_fill() |
| 103 | + t.circle(a) |
| 104 | + t.end_fill() |
| 105 | + t.seth(180) |
| 106 | + t.begin_fill() |
| 107 | + t.circle(b) |
| 108 | + t.end_fill() |
| 109 | + t.pencolor("black") |
| 110 | + t.fillcolor("black") |
| 111 | + t.penup() # 右眼睛 |
| 112 | + t.goto(n-a/4, m+a) |
| 113 | + t.seth(0) |
| 114 | + t.pendown() |
| 115 | + t.begin_fill() |
| 116 | + t.circle(2) |
| 117 | + t.end_fill() |
| 118 | + t.penup() # 左眼睛 |
| 119 | + t.goto(n+a/4, m+a) |
| 120 | + t.seth(0) |
| 121 | + t.pendown() |
| 122 | + t.begin_fill() |
| 123 | + t.circle(2) |
| 124 | + t.end_fill() |
| 125 | + t.penup() # 画嘴巴 |
| 126 | + t.goto(n, m+a/2) |
| 127 | + t.seth(0) |
| 128 | + t.pendown() |
| 129 | + t.fd(5) |
| 130 | + t.penup() # 画扣子 |
| 131 | + t.pencolor("red") |
| 132 | + t.fillcolor("red") |
| 133 | + t.goto(n, m-b/4) |
| 134 | + t.pendown() |
| 135 | + t.begin_fill() |
| 136 | + t.circle(2) |
| 137 | + t.end_fill() |
| 138 | + t.penup() |
| 139 | + t.pencolor("yellow") |
| 140 | + t.fillcolor("yellow") |
| 141 | + t.goto(n, m-b/2) |
| 142 | + t.pendown() |
| 143 | + t.begin_fill() |
| 144 | + t.circle(2) |
| 145 | + t.end_fill() |
| 146 | + t.penup() |
| 147 | + t.pencolor("orange") |
| 148 | + t.fillcolor("orange") |
| 149 | + t.goto(n, m-(3*b)/4) |
| 150 | + t.pendown() |
| 151 | + t.begin_fill() |
| 152 | + t.circle(2) |
| 153 | + t.end_fill() |
| 154 | + |
| 155 | +drawsnowman(-200, -200, 20, 30) |
| 156 | +drawsnowman(-250, -200, 30, 40) |
| 157 | + |
| 158 | +t.up() |
| 159 | +t.goto(100, 200) |
| 160 | +t.down() |
| 161 | +t.color("dark red", "red") # 定义字体颜色 |
| 162 | +t.penup() |
| 163 | +t.write("Merry Christmas, My Love!", font=("Comic Sans MS", 16, "bold")) # 定义文字、位置、字体、大小 |
| 164 | +t.end_fill() |
| 165 | + |
| 166 | + |
| 167 | +# 画雪花 |
| 168 | +def drawsnow(): |
| 169 | + t.ht() # 隐藏笔头,ht=hideturtle |
| 170 | + t.pensize(2) # 定义笔头大小 |
| 171 | + for i in range(200): # 画多少雪花 |
| 172 | + t.pencolor("white") # 定义画笔颜色为白色,其实就是雪花为白色 |
| 173 | + t.pu() # 提笔,pu=penup |
| 174 | + t.setx(r.randint(-350, 350)) # 定义x坐标,随机从-350到350之间选择 |
| 175 | + t.sety(r.randint(-100, 350)) # 定义y坐标,注意雪花一般在地上不会落下,所以不会从太小的纵座轴开始 |
| 176 | + t.pd() # 落笔,pd=pendown |
| 177 | + dens = 6 # 雪花瓣数设为6 |
| 178 | + snowsize = r.randint(1, 10) # 定义雪花大小 |
| 179 | + for j in range(dens): # 就是6,那就是画5次,也就是一个雪花五角星 |
| 180 | + # t.forward(int(snowsize)) #int()取整数 |
| 181 | + t.fd(int(snowsize)) |
| 182 | + t.backward(int(snowsize)) |
| 183 | + # t.bd(int(snowsize)) #注意没有bd=backward,但有fd=forward,小bug |
| 184 | + t.right(int(360 / dens)) # 转动角度 |
| 185 | + |
| 186 | + |
| 187 | +drawsnow() # 调用画雪花的方法 |
| 188 | +t.done() # 完成,否则会直接关闭 |
| 189 | + |
| 190 | + |
0 commit comments