python時鐘原始碼

2021-10-04 21:40:40 字數 3435 閱讀 6793

話不多說,**如下

# encoding: utf-8

"""@version: ??

@author:

@file: 時鐘.py

@time: 2020/4/9 21:43

"""import sys, random, math, pygame

from pygame.

locals

import

*from datetime import datetime, date, time

defprint_text

(font, x, y, text, color=

(255

,255

,255),

):imgtext = font.render(text,

true

, color,

) screen.blit(imgtext,

(x, y)

)def

wrap_angle

(angle)

:return angle %

360# main program begins

pygame.init(

)screen = pygame.display.set_mode(

(600

,500))

pygame.display.set_caption(

'analog clock demo'

)font = pygame.font.font(

none,36

)white =

255,

255,

255pos_x =

300pos_y =

250radius =

250angle =

360# repeating loop

while

true

:for event in pygame.event.get():

if event.

type

== quit:

sys.exit(

) keys = pygame.key.get_pressed(

)if keys[k_escape]

: sys.exit(

) screen.fill((0

,0,100))

# draw one step around the circle

pygame.draw.circle(screen, white,

(pos_x, pos_y)

, radius,6)

# draw the clock numbers 1-12

for n in

range(1

,13):

angle = math.radians(n *

(360/12

)-90)

# 將角度轉化成弧度,然後由於度數的起點在圓的正東方向,所以要-90度,表的刻度0點在正北方向

x = math.cos(angle)

*(radius -20)

-10# 計算x,y的方式 math.cos(angle)*radius -20 -10是用來調節美觀的

y = math.sin(angle)

*(radius -20)

-10print_text(font, pos_x + x, pos_y + y,

str(n)

)# get the time of day

today = datetime.today(

) hours = today.hour %

12 minutes = today.minute

seconds = today.second

# draw the hours hand

hour_angle = wrap_angle(hours *

(360/12

)-90)

hour_angle = math.radians(hour_angle)

hour_x = math.cos(hour_angle)

*(radius -80)

hour_y = math.sin(hour_angle)

*(radius -80)

target =

(pos_x + hour_x, pos_y + hour_y)

pygame.draw.line(screen, white,

(pos_x, pos_y)

, target,25)

# draw the minutes hand

min_angle = wrap_angle(minutes *

(360/60

)-90)

min_angle = math.radians(min_angle)

min_x = math.cos(min_angle)

*(radius -60)

min_y = math.sin(min_angle)

*(radius -60)

target =

(pos_x + min_x, pos_y + min_y)

pygame.draw.line(screen, white,

(pos_x, pos_y)

, target,12)

# draw the seconds hand

sec_angle = wrap_angle(seconds *

(360/60

)-90)

sec_angle = math.radians(sec_angle)

sec_x = math.cos(sec_angle)

*(radius -40)

sec_y = math.sin(sec_angle)

*(radius -40)

target =

(pos_x + sec_x, pos_y + sec_y)

pygame.draw.line(screen, white,

(pos_x, pos_y)

, target,6)

# cover the center

pygame.draw.circle(screen, white,

(pos_x, pos_y),20

) print_text(font,0,

0,str(hours)

+":"

+str

(minutes)

+":"

+str

(seconds)

) pygame.display.update(

)

這是效果圖

抖音時鐘效果原始碼

找所有的東西標籤函式 let findsiblings tag return sb 去掉所有兄弟的類 let removesiblingclass tag 初始化月份函式 let initmonth 初始化日期 let initdate 初始化小時,分鐘,秒 let inithour span.in...

python原始碼剖析 Python原始碼剖析

第頁共 頁python 原始碼剖析 物件機制 1.物件 在python 的世界中,一切都是物件,乙個整數是乙個物件,乙個字串也是 乙個物件,更為奇妙的是,型別也是乙個物件,整數型別是乙個物件,字串類 型也是乙個物件。從 年guido 在那個聖誕節揭開 python 世界的大幕開始,一直到現在,pyt...

python原始碼理解

1,一切都是物件 2,typedef struct objectpyobject pyobject是不可變長度的物件 3,typedef structpyvarobject 可變長度的物件 4,每乙個物件都對應乙個型別物件 pytype type 5,每乙個物件都有乙個引用計數器 6,在python...