python之turtle模拟星空
预备知识
小海龟(Turtle)是Python中画图的一个重要的内置模块,具体介绍可见https://blog.csdn.net/cnds123/article/details/108252863
画一个N边形源码:
import turtle
#画一个n边形函数
def draw_polygon(n):
for i in range(n):
turtle.forward(50)
turtle.right(360/n)
draw_polygon(6) #调用函数画一个6边形
turtle.up() #抬起画笔
turtle.goto(50,100)
turtle.down() #放下画笔
draw_polygon(5) #调用函数画一个6边形
turtle.hideturtle() #隐藏小海龟
turtle.done()
运行效果:
画五角星
五角星每个角的度数都是 36 度,用 180 - 36 得到对应的补角为 144 度。五角星的五条线的长度是一样的,并且角度也一样。那么,我们就可以用循环来画五角星。五角星要画 5 条线,因此要循环 5 次。每次循环都除了画一条线,还要旋转特定的角度。
源码如下:
import turtle
for i in range(5):
turtle.forward(100)#向前走100像素
turtle.right(144) #右转144度
turtle.hideturtle() #隐藏小海龟
运行效果:
若画的五角星比较多,可以把画五角星的代码封装成一个函数:
import turtle
#画五角星函数
def draw_star():
turtle.color('red') #设置画笔颜色和填充颜色都设为红色
turtle.begin_fill() #开始填充
for i in range(5):
turtle.forward(10)#向前走10像素,即以像素为单位的线长度
turtle.right(144) #右转144度
turtle.end_fill() #填充结束
#调用画五角星函数
draw_star()
为了进一步增加灵活性,设置一个线长度参数,改进上面的画五角星函数的代码如下:
import turtle
#画五角星函数,x代表以像素为单位的线长度
def draw_star(x):
turtle.color('red') #设置画笔颜色和填充颜色都设为红色
turtle.begin_fill() #开始填充
for i in range(5):
turtle.forward(x) #向前走的以像素为单位的线长度
turtle.right(144) #右转144度
turtle.end_fill() #填充结束
#调用画五角星函数
draw_star(15)
漫天小星星
源码如下:
draw_star(15)
现在给出漫天小星星代码如下:
import turtle
from random import randint
screen = turtle.Screen()
screen.bgcolor("black")
turtle.goto(-50, 130)
turtle.color('blue')
turtle.pendown()
turtle.write('小星星', font = ('SimHei', 18, 'bold'))
turtle.up
#一颗星星的代码
def draw_star():
turtle.color('silver') #银色silver
turtle.hideturtle()
turtle.begin_fill()
for i in range(5):
turtle.forward(10)
turtle.right(144)
turtle.end_fill()
# 改成你想画的星星个数
for i in range(60):
turtle.speed(0)
turtle.penup()
x = randint(-150, 150)
y = randint(-100, 100)
turtle.goto(x, y)
turtle.pendown()
draw_star()
turtle.done()
运行效果:
繁星
源码如下:
#引入库
import turtle as t
import random
#创建画板,画笔和颜色
t.setup(800,600)
t.colormode(255)
#t.bgcolor('lavender')
t.bgcolor('navy')
t.speed(0)
colorlist=['silver','lightgoldenrodyellow','floralwhite',
'slategrey','lightsteelblue','pink']
colorback=['black','navy']
randomColor=['pink','lavender','lightsteelblue','silver',
'lightgoldenrodyellow','floralwhite','slategrey']
angle=60
#画大星星
def drawFiveStar (x,y,angle,step,color):
t.penup()
t.goto(x,y)
t.left(angle)
t.pendown()
t.color(color)
t.fillcolor(color)
t.begin_fill()
for x in range(5):
t.forward(step)
t.right(144)
t.end_fill()
for i in range(50):
step=random.randint(20,40)
x=random.randint(-400,400)
y=random.randint(0,300)
angle=random.randint(0,36)*10
color=random.choice(colorlist)
drawFiveStar(x,y,angle,step,color)
t.up()
t.goto(0,0)
t.pendown()
#画小星星
for j in range (200):
back1 =random.choice(colorback)
t.bgcolor(back1)
temp1 =random.choice(colorlist)
t.fillcolor(temp1)
t.color(temp1)
t.begin_fill()
temp2 =random.randint(5,15)
li2= [5,7,9]
temp3= random.choice(li2)
for i in range (temp3):
t.forward(temp2)
t.left(180-180/temp3)
t.end_fill()
t.hideturtle()
t.penup()
for k in range(2):
t.left(random.randint(10,120))
t.forward(random.randint(10,50))
t.up()
t.goto(0,0)
t.pendown()
t.color('purple')
t.goto(0,0)
t.down()
t.bgcolor('black')
t.done()
运行效果:
动态星空
源码如下:
from turtle import *
from random import random,randint
screen = Screen()
width ,height = 800,600
screen.setup(width,height)
screen.title("动态星空")
screen.bgcolor("black")
screen.mode("logo")
screen.delay(0) #这里要设为0,否则很卡
t = Turtle(visible = False,shape='circle')
t.pencolor("white")
t.fillcolor("white")
t.penup()
t.setheading(-90)
t.goto(width/2,randint(-height/2,height/2))
stars = []
for i in range(200):
star = t.clone()
s =random() /3
star.shapesize(s,s)
star.speed(int(s*10))
star.setx(width/2 + randint(1,width))
star.sety( randint(-height/2,height/2))
star.showturtle()
stars.append(star)
while True:
for star in stars:
star.setx(star.xcor() - 3 * star.speed())
if star.xcor()<-width/2:
star.hideturtle()
star.setx(width/2 + randint(1,width))
star.sety( randint(-height/2,height/2))
star.showturtle()
运行效果: