Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1742984
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2015-11-25 10:38:00


点击(此处)折叠或打开

  1. import sys,random,math,pygame
  2. from pygame.locals import *
  3. from datetime import datetime,date,time

  4. def print_text(font,x,y,text,color=(255,255,255)):
  5.     imgText=font.render(text,True,color)
  6.     screen.blit(imgText,(x,y))

  7. def wrap_angle(angle):
  8.     return angle%360

  9. #main program begins
  10. pygame.init()
  11. screen=pygame.display.set_mode((600,500))
  12. pygame.display.set_caption("Analog Clock Demo")
  13. font=pygame.font.Font(None,36)
  14. orange=220,180,0
  15. white=255,255,255
  16. yellow=255,255,0
  17. pink=255,100,100

  18. pos_x=300
  19. pos_y=250
  20. radius=250
  21. angle=360

  22. #repeating loop
  23. while True:
  24.     for event in pygame.event.get():
  25.         if event.type==QUIT:
  26.             sys.exit()
  27.     keys=pygame.key.get_pressed()
  28.     if keys[K_ESCAPE]:
  29.         sys.exit()

  30.     screen.fill((0,0,100))

  31.     #draw one step around the circle
  32.     pygame.draw.circle(screen,white,(pos_x,pos_y),radius,6)

  33.     #draw the clock numbers 1-12
  34.     for n in range(1,13):
  35.         angle=math.radians(n*(360/12)-90)
  36.         x=math.cos(angle)*(radius-20)-10
  37.         y=math.sin(angle)*(radius-20)-10
  38.         print_text(font,pos_x+x,pos_y+y,str(n))

  39.     #get the time of day
  40.     today=datetime.today()
  41.     hours=today.hour%12
  42.     minutes=today.minute
  43.     seconds=today.second

  44.     #draw the hours hand
  45.     hour_angle=wrap_angle(hours*(360/12)-90+30*minutes/60)
  46.     hour_angle=math.radians(hour_angle)
  47.     hour_x=math.cos(hour_angle)*(radius-80)
  48.     hour_y=math.sin(hour_angle)*(radius-80)
  49.     target=(pos_x+hour_x,pos_y+hour_y)
  50.     pygame.draw.line(screen,pink,(pos_x,pos_y),target,25)

  51.     #draw the miutes hand
  52.     min_angle=wrap_angle(minutes * (360/60)-90)
  53.     min_angle=math.radians(min_angle)
  54.     min_x=math.cos(min_angle)*(radius-60)
  55.     min_y=math.sin(min_angle)*(radius-60)
  56.     target=(pos_x+min_x,pos_y+min_y)
  57.     pygame.draw.line(screen,orange,(pos_x,pos_y),target,12)

  58.     #draw the seconds hand
  59.     sec_angle=wrap_angle(seconds*(360/60)-90)
  60.     sec_angle=math.radians(sec_angle)
  61.     sec_x=math.cos(sec_angle)*(radius-40)
  62.     sec_y=math.sin(sec_angle)*(radius-40)
  63.     target=(pos_x+sec_x,pos_y+sec_y)
  64.     pygame.draw.line(screen,yellow,(pos_x,pos_y),target,6)

  65.     #cover the center
  66.     pygame.draw.circle(screen,white,(pos_x,pos_y),20)
  67.     print_text(font,0,0,str(hours)+':'+str(minutes)+":"+str(seconds))
  68.     
  69.     pygame.display.update()

阅读(2260) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~