第一个绘图
第一个绘图
编程题
请将以下代码复制到program.py中,点击「运行」按钮,查看这段代码的执行结果。
import turtle
alpha = turtle.Turtle()
alpha.color('red')
alpha.forward(100)
alpha.left(90)
绘图演示2
绘图演示2
编程题
这里我们换了另一段代码,它能画出来什么呢?
想知道答案么?
那就点击 运行代码,让它把图形画出来吧。
点击运行代码,看看会发生什么。
你会发现,画布上面画了一朵玫瑰花;你可以尝试将第 11 行的 red 改为 purple,来看看玫瑰花会发生什么变化。
import turtle
turtle.setup(350, 600)
alpha = turtle.Turtle()
alpha.penup()
alpha.left(90)
alpha.fd(200)
alpha.pendown()
alpha.right(90)
alpha.fillcolor("red")
alpha.begin_fill()
alpha.circle(10, 180)
alpha.circle(25, 110)
alpha.left(50)
alpha.circle(60, 45)
alpha.circle(20, 170)
alpha.right(24)
alpha.fd(30)
alpha.left(10)
alpha.circle(30, 110)
alpha.fd(20)
alpha.left(40)
alpha.circle(90, 70)
alpha.circle(30, 150)
alpha.right(30)
alpha.fd(15)
alpha.circle(80, 90)
alpha.left(15)
alpha.fd(45)
alpha.right(165)
alpha.fd(20)
alpha.left(155)
alpha.circle(150, 80)
alpha.left(50)
alpha.circle(150, 90)
alpha.end_fill()
alpha.left(150)
alpha.circle(-90, 70)
alpha.left(20)
alpha.circle(75, 105)
alpha.setheading(60)
alpha.circle(80, 98)
alpha.circle(-90, 40)
alpha.left(180)
alpha.circle(90, 40)
alpha.circle(-80, 98)
alpha.setheading(-83)
alpha.fd(30)
alpha.left(90)
alpha.fd(25)
alpha.left(45)
alpha.fillcolor("green")
alpha.begin_fill()
alpha.circle(-80, 90)
alpha.right(90)
alpha.circle(-80, 90)
alpha.end_fill()
alpha.right(135)
alpha.fd(60)
alpha.left(180)
alpha.fd(85)
alpha.left(90)
alpha.fd(80)
alpha.right(90)
alpha.right(45)
alpha.fillcolor("green")
alpha.begin_fill()
alpha.circle(80, 90)
alpha.left(90)
alpha.circle(80, 90)
alpha.end_fill()
alpha.left(135)
alpha.fd(60)
alpha.left(180)
alpha.fd(60)
alpha.right(90)
alpha.circle(200, 60)
蟒蛇绘制
蟒蛇绘制
编程题
使用turtle库,绘制一个蟒蛇形状的图形。
import turtle
turtle.setup(650,350,200,200)
turtle.penup()
turtle.fd(-250)
turtle.pendown()
turtle.pensize(25)
turtle.pencolor("purple")
turtle.seth(-40)
for i in range(4):
turtle.circle(40,80)
turtle.circle(-40,80)
turtle.circle(40,80/2)
turtle.fd(40)
turtle.circle(16,180)
turtle.fd(40* 2/3)
turtle.down()
关于turtle库
绝对0度方向
完善绘图程序
完善绘图程序
编程题
通过之前 alpha 绘图的效果我们了解到:
alpha.color(‘red’)是让 alpha 的颜色变成 红 色;
alpha.forward(100)是让 alpha 前进 100 个单位;
alpha.left(90)是让 alpha 左转 90 度;
现在使用这些代码让 alpha 画出一个完整的正方形吧。
import turtle
alpha = turtle.Turtle()
alpha.color('red')
alpha.forward(100)
alpha.left(90)
# 完善代码,画一个正方形。
alpha.forward(100)
alpha.left(90)
alpha.forward(100)
alpha.left(90)
alpha.forward(100)
alpha.left(90)
八边形
八边形
编程题
使用turtle库,绘制一个八边形。
import turtle as t
t.pensize(2)
for i in range(8):
t.fd(60)
t.left(45)
八角形
八角形
编程题
使用turtle库,绘制一个八角图形。
import turtle as t
t.pensize(2)
for i in range(8):
t.fd(150)
t.left(135)
叠边形绘制
叠边形绘制
编程题
使用turtle库,绘制一个叠边形,其中,叠边形内角为100度。
import turtle as t
for i in range(9):
t.fd(100)
t.left(80)
正确引用turtle中的函数
使用循环绘图
使用循环绘图
编程题
循环能将大量重复的代码,缩减到只用几行代码,就能得到与之前相同的效果。
创建循环,让 turtle 循环画出五角星的五条边。
import turtle
alpha = turtle.Turtle()
steps = 100
angle1 = (180 * (5 - 2)) / 5
angle2 = 180 - angle1
angle = 2 * angle2
alpha.forward(steps)
alpha.right(angle)
alpha.forward(steps)
alpha.right(angle)
alpha.forward(steps)
alpha.right(angle)
alpha.forward(steps)
alpha.right(angle)
alpha.forward(steps)
alpha.right(angle)
风轮绘制
风轮绘制
编程题
使用turtle库,绘制一个风轮效果,其中,每个风轮内角为45度,风轮边长150像素。
import turtle
turtle.setup(600,600)
#第四象限
turtle.seth(-45)
turtle.fd(150)
turtle.seth(45)
turtle.circle(150,45)
turtle.seth(-180)
turtle.fd(150)
#第一象限
turtle.seth(45)
turtle.fd(150)
turtle.seth(135)
turtle.circle(150,45)
turtle.seth(-90)
turtle.fd(150)
#第二象限
turtle.seth(135)
turtle.fd(150)
turtle.seth(-135)
turtle.circle(150,45)
turtle.seth(0)
turtle.fd(150)
#第三象限
turtle.seth(-135)
turtle.fd(150)
turtle.seth(-45)
turtle.circle(150,45)
turtle.seth(90)
turtle.fd(150)
自由绘制
彩虹糖
from turtle import *
from random import random
import contextlib
def draw_circle(r):
a, b, c = random(), random(), random()
#pencolor(a, b, c)
fillcolor(a, b, c)
begin_fill()
circle(r)
end_fill()
def pen_skip(step):
penup()
forward(step)
pendown()
speed(5)
setup(width=800,height=600)
screensize(600,400, "gray")
long = 600
high = 450
left(180)
pen_skip(250)
left(90)
pen_skip(200)
left(90)
high_start = 50
high_step = 50
long_start = 50
long_step = 50
for i in range(high_start,high,high_step):
for j in range(long_start,long,long_step):
if (i//high_step)%2 ==1:
if j == long-long_step:
draw_circle(long_step//2)
continue
draw_circle(long_step // 2)
pen_skip(long_step)
else:
if j == long-long_step:
draw_circle(-long_step // 2)
continue
draw_circle(-long_step // 2)
pen_skip(long_step)
if (i//50)%2 == 1:
left(90)
pen_skip(high_step)
left(90)
else:
right(90)
pen_skip(high_step)
right(90)
exitonclick()
阴阳图
from turtle import *
def yin(radius, color1, color2):
width(3)
color("black", color1)
begin_fill()
circle(radius/2., 180)
circle(radius, 180)
left(180)
circle(-radius/2., 180)
end_fill()
left(90)
up()
forward(radius*0.35)
right(90)
down()
color(color1, color2)
begin_fill()
circle(radius*0.15)
end_fill()
left(90)
up()
backward(radius*0.35)
down()
left(90)
def main():
reset()
yin(200, "black", "white")
yin(200, "white", "black")
ht()
return "Done!"
if __name__ == '__main__':
main()
mainloop()
花纹设计
from turtle import Turtle, mainloop
from time import clock
# wrapper for any additional drawing routines
# that need to know about each other
class Designer(Turtle):
def design(self, homePos, scale):
self.up()
for i in range(5):
self.forward(64.65 * scale)
self.down()
self.wheel(self.position(), scale)
self.up()
self.backward(64.65 * scale)
self.right(72)
self.up()
self.goto(homePos)
self.right(36)
self.forward(24.5 * scale)
self.right(198)
self.down()
self.centerpiece(46 * scale, 143.4, scale)
self.getscreen().tracer(True)
def wheel(self, initpos, scale):
self.right(54)
for i in range(4):
self.pentpiece(initpos, scale)
self.down()
self.left(36)
for i in range(5):
self.tripiece(initpos, scale)
self.left(36)
for i in range(5):
self.down()
self.right(72)
self.forward(28 * scale)
self.up()
self.backward(28 * scale)
self.left(54)
self.getscreen().update()
def tripiece(self, initpos, scale):
oldh = self.heading()
self.down()
self.backward(2.5 * scale)
self.tripolyr(31.5 * scale, scale)
self.up()
self.goto(initpos)
self.setheading(oldh)
self.down()
self.backward(2.5 * scale)
self.tripolyl(31.5 * scale, scale)
self.up()
self.goto(initpos)
self.setheading(oldh)
self.left(72)
self.getscreen().update()
def pentpiece(self, initpos, scale):
oldh = self.heading()
self.up()
self.forward(29 * scale)
self.down()
for i in range(5):
self.forward(18 * scale)
self.right(72)
self.pentr(18 * scale, 75, scale)
self.up()
self.goto(initpos)
self.setheading(oldh)
self.forward(29 * scale)
self.down()
for i in range(5):
self.forward(18 * scale)
self.right(72)
self.pentl(18 * scale, 75, scale)
self.up()
self.goto(initpos)
self.setheading(oldh)
self.left(72)
self.getscreen().update()
def pentl(self, side, ang, scale):
if side < (2 * scale): return
self.forward(side)
self.left(ang)
self.pentl(side - (.38 * scale), ang, scale)
def pentr(self, side, ang, scale):
if side < (2 * scale): return
self.forward(side)
self.right(ang)
self.pentr(side - (.38 * scale), ang, scale)
def tripolyr(self, side, scale):
if side < (4 * scale): return
self.forward(side)
self.right(111)
self.forward(side / 1.78)
self.right(111)
self.forward(side / 1.3)
self.right(146)
self.tripolyr(side * .75, scale)
def tripolyl(self, side, scale):
if side < (4 * scale): return
self.forward(side)
self.left(111)
self.forward(side / 1.78)
self.left(111)
self.forward(side / 1.3)
self.left(146)
self.tripolyl(side * .75, scale)
def centerpiece(self, s, a, scale):
self.forward(s); self.left(a)
if s < (7.5 * scale):
return
self.centerpiece(s - (1.2 * scale), a, scale)
def main():
t = Designer()
t.speed(0)
t.hideturtle()
t.getscreen().delay(0)
t.getscreen().tracer(0)
at = clock()
t.design(t.position(), 2)
et = clock()
return "runtime: %.2f sec." % (et-at)
if __name__ == '__main__':
msg = main()
print(msg)
mainloop()
时钟
from turtle import *
from datetime import datetime
def jump(distanz, winkel=0):
penup()
right(winkel)
forward(distanz)
left(winkel)
pendown()
def hand(laenge, spitze):
fd(laenge*1.15)
rt(90)
fd(spitze/2.0)
lt(120)
fd(spitze)
lt(120)
fd(spitze)
lt(120)
fd(spitze/2.0)
def make_hand_shape(name, laenge, spitze):
reset()
jump(-laenge*0.15)
begin_poly()
hand(laenge, spitze)
end_poly()
hand_form = get_poly()
register_shape(name, hand_form)
def clockface(radius):
reset()
pensize(7)
for i in range(60):
jump(radius)
if i % 5 == 0:
fd(25)
jump(-radius-25)
else:
dot(3)
jump(-radius)
rt(6)
def setup():
global second_hand, minute_hand, hour_hand, writer
mode("logo")
make_hand_shape("second_hand", 125, 25)
make_hand_shape("minute_hand", 130, 25)
make_hand_shape("hour_hand", 90, 25)
clockface(160)
second_hand = Turtle()
second_hand.shape("second_hand")
second_hand.color("gray20", "gray80")
minute_hand = Turtle()
minute_hand.shape("minute_hand")
minute_hand.color("blue1", "red1")
hour_hand = Turtle()
hour_hand.shape("hour_hand")
hour_hand.color("blue3", "red3")
for hand in second_hand, minute_hand, hour_hand:
hand.resizemode("user")
hand.shapesize(1, 1, 3)
hand.speed(0)
ht()
writer = Turtle()
#writer.mode("logo")
writer.ht()
writer.pu()
writer.bk(85)
def wochentag(t):
wochentag = ["Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Sunday"]
return wochentag[t.weekday()]
def datum(z):
monat = ["Jan.", "Feb.", "Mar.", "Apr.", "May", "June",
"July", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."]
j = z.year
m = monat[z.month - 1]
t = z.day
return "%s %d %d" % (m, t, j)
def tick():
t = datetime.today()
sekunde = t.second + t.microsecond*0.000001
minute = t.minute + sekunde/60.0
stunde = t.hour + minute/60.0
try:
tracer(False) # Terminator can occur here
writer.clear()
writer.home()
writer.forward(65)
writer.write(wochentag(t),
align="center", font=("Courier", 14, "bold"))
writer.back(150)
writer.write(datum(t),
align="center", font=("Courier", 14, "bold"))
writer.forward(85)
tracer(True)
second_hand.setheading(6*sekunde) # or here
minute_hand.setheading(6*minute)
hour_hand.setheading(30*stunde)
tracer(True)
ontimer(tick, 100)
except Terminator:
pass # turtledemo user pressed STOP
def main():
tracer(False)
setup()
tracer(True)
tick()
return "EVENTLOOP"
if __name__ == "__main__":
mode("logo")
msg = main()
print(msg)
mainloop()
wikipedia
from turtle import Screen, Turtle, mainloop
from time import clock, sleep
def mn_eck(p, ne,sz):
turtlelist = [p]
#create ne-1 additional turtles
for i in range(1,ne):
q = p.clone()
q.rt(360.0/ne)
turtlelist.append(q)
p = q
for i in range(ne):
c = abs(ne/2.0-i)/(ne*.7)
# let those ne turtles make a step
# in parallel:
for t in turtlelist:
t.rt(360./ne)
t.pencolor(1-c,0,c)
t.fd(sz)
def main():
s = Screen()
s.bgcolor("black")
p=Turtle()
p.speed(0)
p.hideturtle()
p.pencolor("red")
p.pensize(3)
s.tracer(36,0)
at = clock()
mn_eck(p, 36, 19)
et = clock()
z1 = et-at
sleep(1)
at = clock()
while any([t.undobufferentries() for t in s.turtles()]):
for t in s.turtles():
t.undo()
et = clock()
return "runtime: %.3f sec" % (z1+et-at)
if __name__ == '__main__':
msg = main()
print(msg)
mainloop()
转载:https://blog.csdn.net/qq_43575044/article/details/116603511
查看评论