圣诞树代码版
from turtle import *
import random as r
import time
import turtle
screensize(bg=‘black’)
color(“orange”, “yellow”)
begin_fill()
left(126)
screen=turtle.Screen()
screen.setup(375,700)
circle=turtle.Turtle()
circle.shape(‘circle’)
circle.color(‘red’)
circle.speed(‘fastest’)
circle.up()
square=turtle.Turtle()
square.shape(‘square’)
square.color(‘green’)
square.speed(‘fastest’)
square.up()
circle.goto(0,280)
circle.stamp()
k=0
for i in range(1,13):
y=30i
for j in range(i-k):
x=30j
square.goto(x,-y+280)
square.stamp()
square.goto(-x,-y+280)
square.stamp()
if i%4==0:
x=30*(j+1)
circle.color(‘red’)
circle.goto(-x,-y+280)
circle.stamp()
circle.goto(x,-y+280)
circle.stamp()
k+=3
if i%4 ==3:
x=30*(j+1)
circle.color(‘yellow’)
circle.goto(-x,-y+280)
circle.stamp()
circle.goto(x,-y+280)
circle.stamp()
square.color(‘brown’)
for i in range(13,17):
y=30i
for j in range(2):
x=30j
square.goto(x,-y+ 280)
square.stamp()
square.goto(-x,-y+280)
square.stamp()
turtle.color(“dark red”,“red”)#定義字體顏色
turtle.write(“Merry Christmas”,align =“center”,font=(“Comic Sans MS”,40,“bold”))#定義文字、位置、字體、大小
def drawsnow():#定義畫雪花的方法
turtle.ht() #隱藏筆頭,ht=hideturtle
turtle.pensize(2) #定義筆頭大小
for i in range(200): #畫多少雪花
turtle.pencolor(“white”) #定義畫筆顏色為白色,其實就是雪花為白色
turtle.pu() #提筆,pu=penup
turtle.setx(r.randint(-350,350)) #定義x坐標,隨機從-350到350之間選擇
turtle.sety(r.randint(-100,350)) #定義y坐標,注意雪花一般在地上不會落下,所以不會從太小的縱座軸開始
turtle.pd() #落筆,pd=pendown
dens = 6 #雪花瓣數設為6
snowsize = r.randint(1,10) #定義雪花大小
for j in range(dens): #就是6,那就是畫5次,也就是一個雪花五角星
#t.forward(int(snowsize)) #int()取整數
turtle.fd(int(snowsize))
turtle.backward(int(snowsize))
#t.bd(int(snowsize)) #注意沒有bd=backward,但有fd=forward,小bug
turtle.right(int(360/dens)) #轉動角度
drawsnow()#調用畫雪花的方法
turtle.done() # 完成,否則會直接關閉
總結
- 上一篇: python使用win32后台鼠标点击梦
- 下一篇: 黑客攻击常见方法及安全策略制订(转)