⽤python画星空-python3的turtle画模仿3d星空,运动的恒星⼩
宇宙
2.代码实现条件
python3
3.第1步:
#---第1步---导⼊模块---
from turtle import *
from random import random,randint
4.第2步:
#---第2步---初始化定义---
#---定义屏幕,窗⼝⼤⼩,标题,背景颜⾊
screen = Screen()
#---⼤⼀点效果好⼀点---
width ,height = 1600,1200
screen.setup(width,height)
screen.title('浪漫星空')
screen.bgcolor("black")
#设置或返回以毫秒为单位的绘制延迟,延迟越⼤,绘图越慢
screen.delay(0)
5.第3步:
#---第3步---定义3种不同颜⾊的星球,⼤⼩、速度、位置、形状不同---
#shape():设置乌龟的图形形状,取值:"arrow”, "turtle”, "circle”, "square”, "triangle”, "classic”
#---星球---⽩⾊星星---
t = Turtle(visible = False,shape='circle')
t.pencolor("white")
#海龟的颜⾊,也就是飞动的星球的颜⾊
t.fillcolor("blue")
t.penup()
#旋转⾓度
t.setheading(-10)
#坐标是随机的
<(width/2,randint(-height/2,height/2))
#---星球2---绿⾊远处⼩星星---
t2 = Turtle(visible = False,shape='turtle')
#海龟的颜⾊,也就是飞动的星球的颜⾊
t2.fillcolor("green")
t2.penup()
t2.setheading(-50)
#坐标是随机的
<(width,randint(-height,height))
#---星球3---近的红⾊恒星---
t3 = Turtle(visible = False,shape='circle')
#海龟的颜⾊,也就是飞动的星球的颜⾊
t3.fillcolor("red")
t3.penup()
t3.setheading(-90)
#坐标是随机的
<(width*2,randint(-height*2,height*2))
6.第4步:
#---第4步---定义星球列表---⽤于存放---
stars = []
stars2 = []
stars3 = []
7.第5步:
#---第5步---定义3种星球的⼤⼩、速度、位置并存放各⾃列表中---#---注意200为画200个各⾃星球就退出,注意太多了要卡死的---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)
for i in range(200):
star2 = t2.clone()
#决定星球的⼤⼩
s2= random()/2
star2.shapesize(s2,s2)
star2.speed(int(s*10))random在python中的意思
star2.setx(width/2 + randint(1,width))
star2.sety(randint(-height/2,height/2)) star2.showturtle()
stars2.append(star2)
for i in range(200):
star3 = t3.clone()
#决定星球的⼤⼩
s3= random()*5
star3.shapesize(10*s3,10*s3)
star3.speed(int(s3*10))
star3.setx(width*2 + randint(1,width))
star3.sety(randint(-height*2,height*2)) star3.showturtle()
stars3.append(star3)
8.第6步:
#---第6步---游戏循环---各⾃星球的启动
while True:
for star in stars:
star.() - 3 * star.speed())
()<-width/2:
star.hideturtle()
star.setx(width/2 + randint(1,width)) star.sety( randint(-height/2,height/2)) star.showturtle()
for star2 in stars2:
star2.() - 3 * star2.speed()) ()<-width/2:
star2.hideturtle()
star2.setx(width/2 + randint(1,width))
star2.sety( randint(-height/2,height*2))
star2.showturtle()
for star3 in stars3:
star3.() - 3 * star3.speed())
()<-width*2:
star3.hideturtle()
star3.setx(width*2 + randint(1,width))
star3.sety( randint(-height*2,height*2))
star3.showturtle()
9.效果图
2.代码实现条件
python3
3.第1步:
#---第1步---导⼊模块---
from turtle import *
from random import random,randint
4.第2步:
#---第2步---初始化定义---
#---定义屏幕,窗⼝⼤⼩,标题,背景颜⾊
screen = Screen()
#---⼤⼀点效果好⼀点---
width ,height = 1600,1200
screen.setup(width,height)
screen.title('浪漫星空')
screen.bgcolor("black")
#设置或返回以毫秒为单位的绘制延迟,延迟越⼤,绘图越慢
screen.delay(0)
5.第3步:
#---第3步---定义3种不同颜⾊的星球,⼤⼩、速度、位置、形状不同---
#shape():设置乌龟的图形形状,取值:"arrow”, "turtle”, "circle”, "square”, "triangle”, "classic”#---星球---⽩⾊星星---
t = Turtle(visible = False,shape='circle')
t.pencolor("white")
#海龟的颜⾊,也就是飞动的星球的颜⾊
t.fillcolor("blue")
t.penup()
#旋转⾓度
t.setheading(-10)
#坐标是随机的
<(width/2,randint(-height/2,height/2))
#---星球2---绿⾊远处⼩星星---
t2 = Turtle(visible = False,shape='turtle')
#海龟的颜⾊,也就是飞动的星球的颜⾊
t2.fillcolor("green")
t2.penup()
t2.setheading(-50)
#坐标是随机的
<(width,randint(-height,height))
#---星球3---近的红⾊恒星---
t3 = Turtle(visible = False,shape='circle')
#海龟的颜⾊,也就是飞动的星球的颜⾊
t3.fillcolor("red")
t3.penup()
t3.setheading(-90)
#坐标是随机的
<(width*2,randint(-height*2,height*2))
6.第4步:
#---第4步---定义星球列表---⽤于存放---
stars = []
stars2 = []
stars3 = []
7.第5步:
#---第5步---定义3种星球的⼤⼩、速度、位置并存放各⾃列表中---#---注意200为画200个各⾃星球就退出,注意太多了要卡死的---for i in range(200):
star = t.clone()