---------原创,引用请注明出处liurm.mypm.net-----------------
2 classClock.py 这是时钟的类,处理时针、分针、秒针的刷屏更新
#
#the class of clock
import pygame,os,time
from pygame.locals import *
import pyfunction
class MyClock(pygame.sprite.Sprite):
"""draw a clock on the screen. it can spin the fingers.
"""
def __init__(self):
#call Sprite intializer
pygame.sprite.Sprite.__init__(self)
#load Images:hour min sec
self.oriImageHour, self.rectHour = pyfunction.loadImage('hour.bmp', -1)
self.imageHour = self.oriImageHour
self.oriImageMin, self.rectMin = pyfunction.loadImage('minute.bmp', -1)
self.imageMin = self.oriImageMin
self.oriImageSec, self.rectSec = pyfunction.loadImage('second.bmp', -1)
self.imageSec = self.oriImageSec
#screen position
screen = pygame.display.get_surface()
self.area = screen.get_rect()
background = pygame.Surface(screen.get_size())
self.width = background.get_width()
self.height = background.get_height()
if self.width > self.height :
self.handlength = self.height
else:
self.handlength = self.width
self.center = (self.width/2, self.height/2)
def updateHour(self): self._tickhour()
def updateMin(self): self._tickmin()
def updateSec(self): self._ticksec()
def _transImage(self,hand,digi,scale):
rotozoom = pygame.transform.rotozoom
self.image = rotozoom(hand, digi, scale)
self.rect = self.image.get_rect(center=self.center)
def _tickhour(self):
"tick tick hour"
hour = time.localtime().tm_hour\
+ time.localtime().tm_min / 60.0 # must be 60.0, if 60, devide=0
rotozoom = pygame.transform.rotozoom
self._transImage(self.oriImageHour, -30 * hour, self.handlength * .0005 )
def _tickmin(self):
"tick tick minute"
min = time.localtime().tm_min
rotozoom = pygame.transform.rotozoom
self._transImage(self.oriImageMin, -6 * min, self.handlength * .0006)
def _ticksec(self):
"tick tick second"
sec = time.localtime().tm_sec
rotozoom = pygame.transform.rotozoom
self._transImage(self.oriImageSec, -6 * sec, self.handlength * .004)