入门教程——pygame自制简易时钟

个人经验

本品制作的是一个仿手表式的软件!

模拟时钟在程序界,也就相当于入门级的难度。现在非编程专业人士遇到的很多python培训机构良莠不齐,五花八门。想要收获学习的人可能无形之间就上了他们的当!

就好比一个小侠客,招式各门各派都有,各种武功眼花缭乱,但内力深厚的人自然有掩盖不了的实力。

python使用的人越来越多,使用时能导入各种各样的包,导致之后的显示窗口也是不尽相同,但无论你怎么学,若一个显示时钟都做不了,那就证明你的功力没怎么入门!

此程序是用pygame写的,需要包的加我联系。
在这里插入图片描述

要求

为了实现上述功能,我将每一步的要求都记录下来,逐一实现,最好融合到达既定的要求!

1、炫酷外表小程序:幅度角+1,随机颜色,轮回旋转.。
2、内置小程序:单体幕布,画出数字,设置时间,绘制时针、分针、秒针。
3、时间小程序:显示“中国标准时间”,日期及星期几。
4、切换模式:按下enter键后,立即跳转时间显示界面。

程序

import sys,random,math,pygame,datetime
from datetime import *
import time
from pygame.locals import*
class shizhong (object):
    def __init__(self):
        self.pos_x=300
        self.pos_y=230
        self.radius=200
        self.angle=0
        self.color=0,0,255
        self.failed=False
        self.flag=False
        self.start=0
        self.seconds=10
        self.vel_y=0.7
        self.date_y=450
        

    def look(self):
        self.angle+=1
        if self.angle>=360:
            self.angle=0
            r=random.randint(0,255)
            g=random.randint(0,255)
            b=random.randint(0,255)
            self.color=r,g,b#七彩颜色
        x=math.cos(math.radians(self.angle))*self.radius
        y=math.sin(math.radians(self.angle))*self.radius
        r=10
        pos=(int(self.pos_x+x),int(self.pos_y+y))#轮回旋转
        pygame.draw.circle(screen,self.color,pos,r,0)
        

    def inside(self):
        r2=190
        pos2=(self.pos_x,self.pos_y)
        pygame.draw.circle(screen,black,pos2,r2,0)#刷新幕布
        #pygame.draw.circle(screen,black,pos2,300,90)
        
        for n in range(1,13):#画出数字
            angle1=math.radians(n*(360/12)-90)
            x=self.pos_x+math.cos(angle1)*(self.radius-20)-10
            y=self.pos_y+math.sin(angle1)*(self.radius-20)-10
            print_t(font,x,y,str(n),white)

        today=datetime.today()
        hours=today.hour%12#设置时间值
        hour_s=wrap_a(hours*(360/12)-90)#12点为起始 ,hour
        hour_s=math.radians(hour_s)
        hour_x=math.cos(hour_s)*(self.radius-80)
        hour_y=math.sin(hour_s)*(self.radius-80)
        target=(self.pos_x+hour_x,self.pos_y+hour_y)
        pygame.draw.line(screen,grey,(self.pos_x,self.pos_y),target,25)
                
        minute=today.minute
        min_s=wrap_a(minute*(360/60)-90)#设置分钟值,minute
        min_s=math.radians(min_s)
        min_x=math.cos(min_s)*(self.radius-60)
        min_y=math.sin(min_s)*(self.radius-60)
        target=(self.pos_x+min_x,self.pos_y+min_y)
        pygame.draw.line(screen,grey,(self.pos_x,self.pos_y),target,12)
                
        seconds=today.second
        sec_a=wrap_a(seconds*(360/60)-90)#设置秒值,seconds
        sec_a=math.radians(sec_a)
        sec_x=math.cos(sec_a)*(self.radius-40)
        sec_y=math.sin(sec_a)*(self.radius-40)
        target=(self.pos_x+sec_x,self.pos_y+sec_y)
        pygame.draw.line(screen,red,(self.pos_x,self.pos_y),target,6)

        r1=20
        pos1=(self.pos_x,self.pos_y)
        pygame.draw.circle(screen,white1,pos1,r1,0)


    def time(self):
        screen.fill(black)
        today=datetime.today()
        h1=today.hour
        m1=today.minute
        s1=today.second
        
        current=time.clock()-self.start
        if self.seconds-current<0:
            self.failed=False
        elif current<=self.seconds:
#解决“yy”格式问题,此处应该有掌声!         
            text=geshi[h1]+":"+geshi[m1]+":"+geshi[s1]
            print_t(font1,160,100,text,self.color)
            #print_t(font,0,80,"time:"+str(int(current)))
            

    def date(self):
        today=time.localtime()
        y2=today.tm_year
        m2=today.tm_mon
        d2=today.tm_mday
        w2=time.strftime("%w",today)
        date_x=220
        text2=str(m2)+"月"+str(d2)+"日星期"+xingqi[int(w2)]
        #print_t(font_h1,0,0,"星期:"+str(w2))
        print_t(font_h1,date_x+10,self.date_y+35,text2,grey)
        print_t(font_h2,date_x,self.date_y,"中国标准时间",white1)
        if self.failed:
            if self.date_y>200:
                self.date_y-=self.vel_y
        else:
            self.date_y=450
         
pygame.init()
screen=pygame.display.set_mode((600,500))
pygame.display.set_caption("能不能支持汉字")
font=pygame.font.Font(None,24)
font1=pygame.font.Font(None,100)
font_h1=pygame.font.SysFont('SimHei',20)
font_h2=pygame.font.SysFont('SimHei',24)
geshi=['00','01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60']
xingqi=("日","一","二","三","四","五","六")

#       rgb
black=0,0,0
blue=0,0,100
white=255,255,255
white1=200,200,200
grey=100,100,100
red=255,0,0
a=shizhong()
screen.fill(black)
while True:
    for event in pygame.event.get():
        if event.type==QUIT:
            sys.exit()
        elif event.type==KEYDOWN:
            if event.key==K_RETURN:
                #a.flag=not a.flag
                a.failed=not a.failed
                a.start=time.clock()
#       此处调整了一个bug,巡查按键超级灵活
    keys=pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()

    if a.failed:
        a.time()
        a.date()
    else:
        a.look()
        a.inside()
        a.date()
 
    pygame.display.update()

总结经验

我学习python显示窗口写的第一个程序便是时钟,疫情特殊时期入的门,之后删删改改十几天才完成。

现在回头在看,觉得自己以前的程序还是有所欠缺,换作是你,也肯定不会满意。

写代码最重要的,是要描述如何实现自己想要的功能,虽然现在看自己以前水平不咋地,但跌跌撞撞还是实现了内心的想法,整个过程中收获最多的便是这自我肯定的喜悦!

猜你喜欢

转载自blog.csdn.net/weixin_46921175/article/details/107541597