class Hand(t.Turtle):
def __init__(self,length):
super().__init__()
self.shape('arrow')
self.shapesize(0.5,length)
self.tick()
def tick(self):
time0=datetime.datetime.today()
s=time0.second
angle=90-s*6
self.seth(angle)
t.ontimer(self.tick,1000)
这是小编创造的指针类,目前实例化后是秒针,小编还要继续编程。
在方法中增加if判断
def tick(self):
time0=datetime.datetime.today()
s=time0.second
if self.length==30:
angle=90-6*s
elif self.length==22:
angle=分针角度
else:
angle=时针角度
self.seth(angle)
t.ontimer(self.tick,1000)
在tick方法中,对length进行判断,再对不同类型的指针设置不同的旋转角度就可以了。
?length这个参数是从初始化方法中赋值的,在tick方法中并没有length这个属性。
?
??增加初始化属性呀!就是这么简单!
class Hand(t.Turtle):
def __init__(self,length=15):
super().__init__(self)
self.shape('arrow')
self.shapesize(0.5,length)
self.length=length ←就是他
self.tick()
在初始化方法中,新增一个属性,保存length的参数,这样才能在tick的方法中使用这个属性。
在方法中调用属性
对应的,在tick方法中,在length前加上self,就表示这是在调用对象自己的属性
?分针角度和时针角度计算一下
时针与分针的运动
获取时间
def tick(self):
time0=datetime.datetime.today()
s=time0.second
m=time0.minute
h=time0.hour-12
因为时24小时制的,但表盘只有12个刻度,所有要减去12。现在既然秒可以从时间获取,那时和分当然也可以了。
设置分钟角度
和秒钟相似,分钟每转一圈60分钟,所有每分钟运转的角度也是6度。所有我们也可以像秒针一样,通过分钟数计算得到分针角度:90 - 分钟数 *(乘) 6
分针角度算式
def tick(self):
time0=datetime.datetime.today()
s=time0.second
m=time0.minute
h=time0.hour - 12
if self.length==30:
angle=90-6*s
elif self.length==22:
angle=90-6*m
else:
angle=时针角度
?时针每小时转动一大格,一圈有12大格,所有每小时转动的度数:360/12=30于秒针类似,时针角度于小时数对应公式是:90 - 小时数 * 30。
真实的时针运动
?然而,时针并不是每小时直接跳一大格。比如4点12分钟,时针实际上在此比4点整多一小格的位置。所有,时针的角度,于分钟数也有关系。
?1大格等于5小格,时针每走一大格的时间是60分钟,那么走每小格的时间就是12分钟。也就是说,每过12分钟,时针就前进一小格,那么这个多出来的角度就是:
m / 12 * 6
= m?/?2
所有
?
完整的时针角度运算式
?这样,我们就得到了完整的时针角度运算式
直接完整代码走起
import turtle as t
import datetime
t.ht()
t.tracer(0)
class Hand(t.Turtle):
def __init__(self,length=15):
t.Turtle.__init__(self)
self.shape('arrow')
self.shapesize(0.5,length)
self.length=length
self.tick()
def tick(self):
time0=datetime.datetime.today()
s=time0.second
m=time0.minute
h=time0.hour
if self.length==30:
angle=90-6*s
elif self.length==22:
angle=90-6*m-0.1*s
else:
angle=90-30*h-0.5*m
t.tracer(0)
self.seth(angle)
t.tracer(1)
t.update()
t.ontimer(self.tick,1000)
sec=Hand(30)
sec.color('red')
minute=Hand(22)
minute.color('blue')
hour=Hand(16)
hour.color('green')
t.tracer(0)
clock=t.Turtle()
clock.shape('circle')
clock.dot(690,'grey90')
clock.dot(630,'white')
clock.penup()
clock.goto(300,0)
clock.seth(90)
for i in range(60):
if i%5==0:
clock.dot(20)
else:
clock.dot(5)
clock.circle(300,6)
clock.home()
t.tracer(1)
t.done()
我还设置了表盘呢!大家也可以自己添加图画,比如:
import turtle as t
import datetime
t.ht()
t.tracer(0)
class Hand(t.Turtle):
def __init__(self,length=15):
t.Turtle.__init__(self)
self.shape('arrow')
self.shapesize(0.5,length)
self.length=length
self.tick()
def tick(self):
time0=datetime.datetime.today()
s=time0.second
m=time0.minute
h=time0.hour
if self.length==30:
angle=90-6*s
elif self.length==22:
angle=90-6*m-0.1*s
else:
angle=90-30*h-0.5*m
t.tracer(0)
self.seth(angle)
t.tracer(1)
t.update()
t.ontimer(self.tick,1000)
sec=Hand(30)
sec.color('red')
minute=Hand(22)
minute.color('blue')
hour=Hand(16)
hour.color('green')
t.tracer(0)
clock=t.Turtle()
clock.shape('circle')
clock.dot(690,'grey90')
clock.dot(630,'white')
clock.penup()
clock.goto(300,0)
clock.seth(90)
import math as m
import random as r
def drawX(a, i):
angle = m.radians(i)
return a * m.cos(angle)
def drawY(b, i):
angle = m.radians(i)
return b * m.sin(angle)
t.bgcolor("#d3dae8")
t.setup(1000, 800)
t.penup()
t.goto(150, 0)
t.pendown()
t.pencolor("white")
t.begin_fill()
for i in range(360):
x = drawX(150, i)
y = drawY(60, i)
t.goto(x, y)
t.fillcolor("#fef5f7")
t.end_fill()
t.begin_fill()
for i in range(180):
x = drawX(150, -i)
y = drawY(70, -i)
t.goto(x, y)
for i in range(180, 360):
x = drawX(150, i)
y = drawY(60, i)
t.goto(x, y)
t.fillcolor("#f2d7dd")
t.end_fill()
t.pu()
t.goto(120, 0)
t.pd()
t.begin_fill()
for i in range(360):
x = drawX(120, i)
y = drawY(48, i)
t.goto(x, y)
t.fillcolor("#cbd9f9")
t.end_fill()
t.begin_fill()
t.pencolor("#fee48c")
for i in range(540):
x = drawX(120, i)
y = drawY(48, i) + 70
t.goto(x, y)
t.goto(-120, 0)
t.fillcolor("#cbd9f9")
t.end_fill()
t.pu()
t.goto(120, 70)
t.pd()
t.pencolor("#fff0f3")
t.begin_fill()
for i in range(360):
x = drawX(120, i)
y = drawY(48, i) + 70
t.goto(x, y)
t.fillcolor("#fff0f3")
t.end_fill()
t.pu()
t.goto(110, 70)
t.pd()
t.pencolor("#fff9fb")
t.begin_fill()
for i in range(360):
x = drawX(110, i)
y = drawY(44, i) + 70
t.goto(x, y)
t.fillcolor("#fff9fb")
t.end_fill()
t.pu()
t.goto(120, 0)
t.pd()
t.begin_fill()
t.pencolor("#ffa79d")
for i in range(180):
x = drawX(120, -i)
y = drawY(48, -i) + 10
t.goto(x, y)
t.goto(-120, 0)
for i in range(180, 360):
x = drawX(120, i)
y = drawY(48, i)
t.goto(x, y)
t.fillcolor("#ffa79d")
t.end_fill()
t.pu()
t.goto(120, 70)
t.pd()
t.begin_fill()
t.pensize(4)
t.pencolor("#fff0f3")
for i in range(1800):
x = drawX(120, 0.1 * i)
y = drawY(-18, i) + 10
t.goto(x, y)
t.goto(-120, 70)
t.pensize(1)
for i in range(180, 360):
x = drawX(120, i)
y = drawY(48, i) + 70
t.goto(x, y)
t.fillcolor("#fff0f3")
t.end_fill()
t.pu()
t.goto(80, 70)
t.pd()
t.begin_fill()
t.pencolor("#6f3732")
t.goto(80, 120)
for i in range(180):
x = drawX(80, i)
y = drawY(32, i) + 120
t.goto(x, y)
t.goto(-80, 70)
for i in range(180, 360):
x = drawX(80, i)
y = drawY(32, i) + 70
t.goto(x, y)
t.fillcolor("#6f3732")
t.end_fill()
t.pu()
t.goto(80, 120)
t.pd()
t.pencolor("#ffaaa0")
t.begin_fill()
for i in range(360):
x = drawX(80, i)
y = drawY(32, i) + 120
t.goto(x, y)
t.fillcolor("#ffaaa0")
t.end_fill()
t.pu()
t.goto(70, 120)
t.pd()
t.pencolor("#ffc3be")
t.begin_fill()
for i in range(360):
x = drawX(70, i)
y = drawY(28, i) + 120
t.goto(x, y)
t.fillcolor("#ffc3be")
t.end_fill()
t.pu()
t.goto(80, 120)
t.pd()
t.begin_fill()
t.pensize(3)
t.pencolor("#ffaaa0")
for i in range(1800):
x = drawX(80, 0.1 * i)
y = drawY(-12, i) + 80
t.goto(x, y)
t.goto(-80, 120)
t.pensize(1)
for i in range(180, 360):
x = drawX(80, i)
y = drawY(32, i) + 120
t.goto(x, y)
t.fillcolor("#ffaaa0")
t.end_fill()
t.pu()
t.goto(64, 120)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i) + 60
y = drawY(1, i) + 120
t.goto(x, y)
t.goto(64, 170)
for i in range(540):
x = drawX(4, i) + 60
y = drawY(1, i) + 170
t.goto(x, y)
t.goto(56, 120)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(64, 120 + 10 * i)
t.pu()
t.goto(56, 120 + 10 * i)
t.pd()
t.pu()
t.goto(60, 170)
t.pd()
t.goto(60, 180)
t.pensize(1)
t.pu()
t.goto(64, 190)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i) + 60
y = drawY(10, i) + 190
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
t.pu()
t.goto(-56, 120)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i) - 60
y = drawY(1, i) + 120
t.goto(x, y)
t.goto(-56, 170)
for i in range(540):
x = drawX(4, i) - 60
y = drawY(1, i) + 170
t.goto(x, y)
t.goto(-64, 120)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(-56, 120 + 10 * i)
t.pu()
t.goto(-64, 120 + 10 * i)
t.pd()
t.pu()
t.goto(-60, 170)
t.pd()
t.goto(-60, 180)
t.pensize(1)
t.pu()
t.goto(-56, 190)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i) - 60
y = drawY(10, i) + 190
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
t.pu()
t.goto(0, 130)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i)
y = drawY(1, i) + 130
t.goto(x, y)
t.goto(4, 180)
for i in range(540):
x = drawX(4, i)
y = drawY(1, i) + 180
t.goto(x, y)
t.goto(-4, 130)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(4, 130 + 10 * i)
t.pu()
t.goto(-4, 130 + 10 * i)
t.pd()
t.pu()
t.goto(0, 180)
t.pd()
t.goto(0, 190)
t.pensize(1)
t.pu()
t.goto(4, 200)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i)
y = drawY(10, i) + 200
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
t.pu()
t.goto(30, 110)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i) + 30
y = drawY(1, i) + 110
t.goto(x, y)
t.goto(34, 160)
for i in range(540):
x = drawX(4, i) + 30
y = drawY(1, i) + 160
t.goto(x, y)
t.goto(26, 110)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(34, 110 + 10 * i)
t.pu()
t.goto(26, 110 + 10 * i)
t.pd()
t.pu()
t.goto(30, 160)
t.pd()
t.goto(30, 170)
t.pensize(1)
t.pu()
t.goto(34, 180)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i) + 30
y = drawY(10, i) + 180
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
t.pu()
t.goto(-30, 110)
t.pd()
t.pencolor("#b1c9e9")
t.begin_fill()
for i in range(360):
x = drawX(4, i) - 30
y = drawY(1, i) + 110
t.goto(x, y)
t.goto(-26, 160)
for i in range(540):
x = drawX(4, i) - 30
y = drawY(1, i) + 160
t.goto(x, y)
t.goto(-34, 110)
t.fillcolor("#b1c9e9")
t.end_fill()
t.pencolor("white")
t.pensize(2)
for i in range(1, 6):
t.goto(-26, 110 + 10 * i)
t.pu()
t.goto(-34, 110 + 10 * i)
t.pd()
t.pu()
t.goto(-30, 160)
t.pd()
t.goto(-30, 170)
t.pensize(1)
#
t.pu()
t.goto(-26, 180)
t.pd()
t.pencolor("#f1add1")
t.begin_fill()
for i in range(360):
x = drawX(4, i) - 30
y = drawY(10, i) + 180
t.goto(x, y)
t.fillcolor("#f1add1")
t.end_fill()
t.seth(90)
t.pu()
t.goto(0, 0)
t.fd(210)
t.left(90)
t.fd(170)
t.pd()
for i in range(60):
if i%5==0:
clock.dot(20)
else:
clock.dot(5)
clock.circle(300,6)
clock.home()
t.tracer(1)
t.done()
运行效果:
?最后
|