欢 迎 光 临
's BLog
这就是我
最新公告
站点日历
最新日志
最新回复
最新留言
 日志搜索

友情链接
其他信息
·研究python时自己写的一个时钟DEMO 3of4     -|liurm 发表于 2007/5/15 9:32:00

---------原创,引用请注明出处liurm.mypm.net-----------------

3 clock.py 主程序,实现初始化和运行的主代码

#!/usr/bin/env python

"""
 draw a ticktick clock
 """
#Import Modules

import os, pygame, math, time
from pygame.locals import *
import pyfunction, classClock
if not pygame.font: print 'Warning, fonts disabled'
if not pygame.mixer: print 'Warning, sound disabled'

#Initialize Everything

pygame.init()
screen = pygame.display.set_mode((240,240))
pygame.display.set_caption('Raman Clock')
pygame.mouse.set_visible(1)


#Create The Background
##back, rect = pyfunction.loadImage('background.jpg', 1) #trans 1 as colorkey
#if empty background
back = pygame.Surface(screen.get_size())
back = back.convert()
back.fill((250,250,250))

def _initDigi():
    #Put Text On The Background
    if pygame.font:
        font = pygame.font.Font(None, screen.get_width()/10)
        x=screen.get_width()/2
        y=screen.get_height()/2
       
        #Put Number On The Background
        digital = range(1,13)   #[1,2,3,4,5,6,7,8,9,10,11,12]
        for i in digital:
            t = font.render(str(i),1,(0,0,0))
            x1 = math.sin(-(i+6)*math.pi*30/180)
            y1 = math.cos(-(i+6)*math.pi*30/180)
            tpos = t.get_rect(centerx=x+ (x-20) * x1,centery=y + (y-20) * y1)
            back.blit(t,tpos)

        #Add dot on the background
        dot = range(1,61)
       
        for i in dot:
            t = font.render('.',1,(100,0,100))
            x1 = math.sin(-(i+30)*math.pi*6/180)
            y1 = math.cos(-(i+30)*math.pi*6/180)
            # to adjust position, adds "- 5" at the end of line
            tpos = t.get_rect(centerx=x + (x-20) * x1,centery=y + (y-20) * y1 - 5)
            back.blit(t,tpos)

#main
def main():
    """this function is called when the program starts.
       it initializes everything it needs, then runs in
       a loop until the function returns."""

    #init digital   
    _initDigi()

    #Prepare Game Objects
    myclock1 = classClock.MyClock()
    myclock2 = classClock.MyClock()
    myclock3 = classClock.MyClock()
    allsprites = pygame.sprite.RenderPlain((myclock1,myclock2,myclock3))
   
    clock = pygame.time.Clock()
    #Main Loop
    while 1:
        clock.tick(3)
        #Handle Input Enents for event in pygame.event.get():
        for event in pygame.event.get():
            if event.type == QUIT:
                return
            elif event.type == KEYDOWN and event.key == K_ESCAPE:
                return

##        if pygame.font:
##            fontD = pygame.font.Font(None,24)
##            t = fontD.render(str(time.localtime()),1,(0,0,0))
##            tpos = t.get_rect(centerx = screen.get_width() /2 ,\
##                              centery = screen.get_height() /2 + 20)
##            back.blit(t,tpos)
               
       
        #reflesh clock hands
        myclock1.updateHour()
        myclock2.updateMin()
        myclock3.updateSec()
        allsprites.update()
       
        #Draw Everything
        screen.blit(back, (0, 0))
        allsprites.draw(screen)
        pygame.display.flip()


#this calls the 'main' function when this script is executed
if __name__ == '__main__': main()

[阅读全文 | 回复(0) | 引用通告 | 编辑 | 收藏该日志]

发表评论:

    昵称:
    密码:
    主页:
    标题: