python+pygame制作一個可自定義的動態時鍾和詳解


1.效果圖

2.完整代碼

#第1步:導出模塊
import sys, random, math, pygame
from pygame.locals import *
from datetime import datetime, date, time

#第2步:初始化和窗扣大小的設置
pygame.init() #這一步必須放在模塊之后,對pygame很重要
#screen = pygame.display.set_mode((800, 600)) #定義窗口大小
screen = pygame.display.set_mode((1500, 1000),RESIZABLE,0,32)
'''
set_mode(size =(0,0),flags = 0,depth = 0)
默認flags = 0,depth = 32
size:(0,0)參數是一對表示寬度和高度的數字
depth:參數表示用於顏色的位數(通常最好不要傳遞深度參數。它將默認為系統的最佳和最快顏色深度),默認32,一般8~32
flags:參數控制您想要的顯示類型。
有幾種可供選擇,您甚至可以使用按位或運算符(管道“|”字符)組合多種類型。
如果傳遞0或沒有flags參數,它將默認為軟件驅動的窗口。
FULLSCREEN=全屏,小程序不建議,大型游戲可以考慮;RESIZABLE=窗口可調節大小,不設置就是不能調節窗口大小
'''
pygame.display.set_caption("動態時鍾DIY") #定義標題

#第3步:字體設置
#font = pygame.font.Font(None, 36) #定義字體,注意如果是中文字體,需要額外設置
#如果游戲界面內有中文,比如‘分數’等顯示時,需要設置中文,可以指定字體和路徑,如下本機設置
font = pygame.font.Font('/home/xgj/Desktop/simsun/simsun.ttf', 36)  #顯示中文的設置和字體,及路徑

#第4步:顏色設置
#pygame中的顏色采用RGB數值,故定義顏色名稱=具體數值,會使代碼簡潔
#當然也可以直接寫數值,比如caogreen=0, 100, 100
orange = 220, 180, 0
white = 255, 255, 255
yellow = 255, 255, 0
pink = 255, 100, 100
caogreen=0, 100, 100  #草綠色
deepskyblue=30,144,255
black=0,0,0

#第5步:定義全局變量
px=700 #px=pos_x=初始的x位置,也就是水平移動700,從左往中間(右→)移動
py=250 #250就是時鍾的圓點距離頂格水平線的距離=半徑radius=250
radius = 250
angle = 360 #一圈正好360°


'''
注意:曾在本機python3.8報錯,下面有注釋和改進后的說明
DeprecationWarning: an integer is required (got type float).  
Implicit conversion to integers using __int__ is deprecated, 
and may be removed in a future version of Python.
報錯的bug1、2、3的原因是target元組里的數是float,需要轉換成int整數
'''

#第6步:定義打印文字print_text,可多次使用,依次放置,注意位置,后面有舉例
#def print_text(font, x, y, text, color=(255, 255, 255)):
#這里設置了打印字體的顏色了,后面再單獨設置就不行,除非自己再單獨定義函數print_text2
def print_text(font, x, y, text, color=white):
    imgText = font.render(text, True, white)
    #screen.blit(imgText,x,y) #報錯,修改前
    screen.blit(imgText, (int(float(x)),int(float(y))))  #bug0,修改后,將float轉換成int,改進了,搞定了

#第7步:定義綁定的角度
def wrap_angle(angle):
    return angle % 360

#第8步:循環定義,必須必的
while True:
    #退出循環的方法有2種
    #方法一
    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, 100, 100)) #直接數值法,代碼顯得繁瑣
    screen.fill(deepskyblue)

    #畫圈,屏幕上,粉紅色圈線,位置,半徑,粗細為6
    pygame.draw.circle(screen, pink, (px, py), radius, 6)

    for n in range(1, 13): #定義時鍾上面的1~12的小時刻度
        angle = math.radians(n * (360 / 12) - 90) #角度
        x = math.cos(angle) * (radius - 20) - 10  #x坐標
        y = math.sin(angle) * (radius - 20) - 10  #y坐標
        print_text(font, px + x, py + y, str(n))  #打印出來

    #獲取今天的時間
    today = datetime.today()
    #定義年月日
    years=today.year
    months=today.month
    days=today.day
    #定義時分秒
    #hours = today.hour % 12 #顯示12h制
    hours = today.hour % 24 #顯示24h制
    minutes = today.minute
    seconds = today.second

    #定義時針
    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 = (px + hour_x, py + hour_y) #修改前的bug
    target = (int(float(px + hour_x)), int(float(py + hour_y)))    #修改后,將float轉換成int
    pygame.draw.line(screen, pink, (px, py), target, 12)    #本來bug1,后來不提示;時針設置,粉紅色

    #定義分針
    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 = (px + min_x, py + min_y)  #修改前的bug
    target = (int(float(px + min_x)), int(float(py + min_y)))   #修改后,將float轉換成int
    pygame.draw.line(screen, orange, (px, py), target, 6)    #本來bug2,后來不提示,秒針設置,橘色
    
    #定義秒針
    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 = (px + sec_x, py + sec_y)  #修改前的bug
    target = (int(float(px + sec_x)), int(float(py + sec_y)))   #修改后,將float轉換成int
    pygame.draw.line(screen, black, (px, py), target, 3) #本來提示bug3,后來不提示;分針設置,黃色

    pygame.draw.circle(screen, white, (px, py), 20) #鍾面中心的圓點設置,白色,粗20
    #顯示當前時間,x=400,y=550,為坐標
    print_text(font, 400, 550, '當前時間:'+str(years) +''+str(months) +''+str(days) 
    +''+str(hours) +''+ ":" + str(minutes) + ''+":" + str(seconds)+'') 
    print_text(font,400,600,'上聯:火神山、雷神山、鍾南山,三座大山鎮魔妖')
    print_text(font,400,650,'下聯:軍民心、醫護心、愛國心,萬眾一心戰新冠')
    print_text(font,400,700,'橫批:庚子新冠')
    pygame.display.update() #循環內的顯示要不斷地刷新,因為是動態的
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM