Python技能操作5:用代码做个能挂墙上的大钟表

天给大家分享 1 个非常实用的 python 技能——用 Python 做个能挂墙上的大钟表,先上成果视频:

 

本项目用到的库主要有 pygame 、 math 、 datetime 等,另外还用到一些数学知识,勾股定理搞不明白的朋友就不要往下看了,可能会头晕。

第一步:用 pygame 画个圆

1. 初始化画布参数

设置画布大小,添加标题,设置圆的原点、半径和角度:

加python学习qq群:775690737  送python零基础入门学习资料+99个源码
pygame.init()

screen = pygame.display.set_mode((

600 , 500

))

pygame.display.set_caption(

"挂墙上的大钟表"

)

pos_x =

300 ; pos_y = 250

#原点座标

radius = 200 

#半径

angle = 360   #角度

2. 画 1 个黑色边框的圆 :

while True

:

screen.fill((

255 , 255 , 255 )) 

# 画布底色为白色



for event in

pygame.event.get():



if

event.type== QUIT:

sys.exit()

keys = pygame.key.get_pressed()



if

keys[K_ESCAPE]:

sys.exit()

angle +=

1



if angle== 360

:

angle =

0



pygame.draw.circle(screen , ( 0 , 0 , 0 ) , ( int (pos_x) , int (pos_y)) , radius , 10

)

pygame.display.update()
加python学习qq群:775690737  送python零基础入门学习资料+99个源码

第二步:在圆上添加数字和刻度

1. 添加数字

把圆周等分为 12 份,那么每份的角度为 360/12 ,因为 0 点在垂直方向,所以再减去 90 度,由勾股定理, x= 半径 *cos (角度), y= 半径 *sin( 角度 ) ,因此代码设置如下:

for n in range ( 1 , 13

):

angle = math.radians(n * (

360 / 12 ) - 90

)

x = math.cos(angle) * (radius-

30 )-

10



y = math.sin(angle) * (radius- 30 )-

10



print_text(font , pos_x+x , pos_y+y , str (n))
加python学习qq群:775690737  送python零基础入门学习资料+99个源码

2. 添加刻度

原理同上,只不过把圆分成了 60 份

for n in range ( 1 , 61

):

angle = math.radians(n * (

360 / 60 ) - 90

)

x = math.cos(angle) * (radius-

15

)

y = math.sin(angle) * (radius-

15

)



if n% 5 == 0

:

print_text(pygame.font.Font(

None, 15 ) , pos_x+x , pos_y+y , str ( 0

))



else

:

print_text(pygame.font.Font(

None, 10 ) , pos_x + x , pos_y + y , str ( 0 ))
加python学习qq群:775690737  送python零基础入门学习资料+99个源码

第三步:画时针、分针和秒针

1. 把当天的日期提取出来

我们需要用 python 的 datetime 库:

from datetime import

datetime

today = datetime.today()

print (today)

2020-04-1218:48:57.733000

再把月、日、时、分、秒提取出来:

month = today.month

day = today.day

hour = today.hour

minute = today.minute

second = today.second

2. 开始画线,让指针动起来

先在中间画个半径为 20 的小圆圈

pygame.draw.circle(screen , ( 220 , 220 , 220 ) , (pos_x , pos_y) , 20 , 6 )

再画时针,主要用到 pygame 的 draw.line 函数:

hour = int (today.time().hour)%

12

hour_angle = math.radians(hour*( 30 )- 90

)

hour_x = math.cos(hour_angle)*(radius-

80

)

hour_y = math.sin(hour_angle)*(radius-

80

)

hour_end = (pos_x+hour_x

,

pos_y+hour_y)

pygame.draw.line(screen

, ( 0 , 0 , 0 ) , ( int (pos_x) , int (pos_y)) , hour_end , 15 )
加python学习qq群:775690737  送python零基础入门学习资料+99个源码

效果图:

分针和秒针的的划线原理同时针,区别在于长度和粗细,加上分针和秒针的图为:

第四步:添加日期和星期

先将日期转化为星期,主要用到 datetime 及 date 函数:

def get_week_day

():



from datetime import datetime ,

date

today =

str (datetime.today().date()).split( '-'

)



import

datetime

day = date.weekday(datetime.datetime(

int (today[ 0 ]) , int (today[ 1 ]) , int (today[ 2

])))

week_day_dict = {



0 : '星期一'

,



1 : '星期二'

,



2 : '星期三'

,



3 : '星期四'

,



4 : '星期五'

,



5 : '星期六'

,



6 : '星期日'

}



return week_day_dict[day]

调用该函数就会返回当天星期几

把当天的日期及星期加到钟表的合适位置:

print_text(pygame.font.Font( 'C:\Windows\Fonts\AdobeHeitiStd-egular.otf' , 30 ) , 245 , 300 , str(today.date().month) + "月" + str (today.date().day) + "日"

)

print_text(pygame.font.Font(

'C:\Windows\Fonts\AdobeHeitiStd-egular.otf' , 30 ) , 250 , 330 , get_week_day())
加python学习qq群:775690737  送python零基础入门学习资料+99个源码

钟表最终效果如下:

第五步:把钟表挂在墙上

这时你需要准备一个显示器,然后连接你的电脑,把运行程序后显示的钟表投到显示器上,最后把显示器挂在墙上就 ok 了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章