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 了

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