用户:
源码极客查看:0 回复:0 评论:0 创建时间:2021-05-18T13:18:56
昨天晚上我望着星空,萌生出了用turtle来画一个星空的想法,今天中午我狂敲了一阵代码,终于完工了(一些代码我是仿照书里写的),因为时间有限,变量名和函数名都没起好,都是些字母,请谅解! importturtleast fromrandomimportrandint,random,choice defdraw_star(a,b,c,x,y): t.penup() t.goto(x,y) t.pendown() d=180-(180/a) t.color(c) t.begin_fill() foriinrange(a): t.forward(b) t.right(d) t.end_fill() defdraw_xx(col,col2,x,y): t.penup() t.goto(x,y) t.pendown() t.color(col) t.begin_fill() t.circle(10) t.end_fill() t.penup() t.goto(x-20,y+10) t.pendown() t.color(col2) t.begin_fill() t.forward(42) t.end_fill() #星光夜空 xk=input('你想让星空\n只有星星\n只有行星\n啥都有\n请选择:') t.speed(0) t.Screen().bgcolor('darkblue') whileTrue: e=randint(2,5)*2+1 f=randint(10,50) g=(choice(['blue','green','red','orange','yellow','pink'])) h=randint(-350,300) i=randint(-250,250) g2=(choice(['blue','green','red','orange','yellow','pink'])) g3=(choice(['blue','green','red','orange','yellow','pink'])) h2=randint(-350,300) i2=randint(-250,250) ifxk=='只有星星': draw_star(e,f,g,h,i) ifxk=='只有行星': draw_xx(g2,g3,h2,i2) ifxk=='啥都有': draw_star(e,f,g,h,i) draw_xx(g2,g3,h2,i2) 我比较得意的是画行星和星星的函数,特别是行星(draw_xx),这可是我试了好久的错才弄出来的!