python pygame--倒计时

import pygame,sys,time,datetime

class decTime(object): 
    #将秒转化为时分秒
    def __init__(self,totalTime):
        self.sec = totalTime
        self.hour = int(self.sec / 3600)
        self.sec = self.sec % 3600
        self.minute = int(self.sec / 60)
        self.sec = int(self.sec % 60)
    #时间减    
    def subTime(self):
        if self.sec > 0:
            self.sec -=  1
        else:
            if self.minute > 0:
                self.minute -= 1
                self.sec = 59
            else:
                if self.hour > 0:
                    self.hour -= 1
                    self.minute = 59
                    self.sec = 59
                else:
                    return 0
if __name__=='__main__':
    pygame.init()
  #窗口宽,高
    size = width,height = 600,100
    screen = pygame.display.set_mode(size)
  #title
    pygame.display.set_caption("倒计时……")
  #背景色
   bg = (255,255,255)
  #字体
    my_font = pygame.font.Font(None,40)
  #行高
    line_height = my_font.get_linesize()
    position = 0
    screen.fill(bg)
  strtime = '2017-7-19 00:00:00'
    now = datetime.datetime.now()
    deadline = datetime.datetime.strptime(strtime,'%Y-%m-%d %H:%M:%S')

    subtime = (deadline - now).seconds

    temp = decTime(subtime)

    while (temp.hour>0) or (temp.minute>0) or (temp.sec>0):
        screen.fill(bg)
        ch = str(temp.hour)+':'+str(temp.minute)+':'+str(temp.sec)
        screen.blit(my_font.render(ch,True,(0,255,0)),(0,position))
        temp.subTime()
        time.sleep(1)
     #休眠1秒刷新屏幕
        pygame.display.flip()
        
效果图:
posted @ 2017-07-18 20:49  朤尧  阅读(4090)  评论(3编辑  收藏  举报