#!/usr/bin/env python ''' binary analog clock! to disable the second hands, comment out the first line of HAND_CONF, and remove '%S ' from the time.strftime call. ''' try: import psyco psyco.profile() except: pass import os, sys, time, random, math, colorsys, sysio try: import pygame from pygame.locals import * except: if 'pythonw' in sys.executable: from Tkinter import * tk = Tk() Label(tk, text='you need pygame to play pycopter.\nget it here:').pack() s = StringVar() s.set('http://pygame.org') Entry(tk, textvariable=s).pack() tk.mainloop() else: print "you need pygame to play this game: http://www.pygame.org/" sys.exit(1) html_color_to_int_tuple = lambda s: tuple([int(s[i:i+2], 16) for i in range(0, 5, 2)]) ############################ Configuration Variables ############################ WIDTH = 200 # width of total game window HEIGHT = 200 HAND_WIDTH = 3 FPS = 80 FONT = ('courier', 26, True) # "import pygame;pygame.init();pygame.font.get_fonts()" to see a list of available fonts. TEXT_HEIGHT = 23 BG_COLOR = 'aed9ff' TEXT_COLOR = '000000' HOUR_COLORS, MINUTE_COLORS, SECOND_COLORS = [[[ int(256 * f) for f in colorsys.hsv_to_rgb(hue, .5, i/10.0) ] for i in xrange(2, 8) ] for hue in (0, 0.333, 0.666) ] SECOND_LENGTHS = [85, 80, 75, 70, 75, 60] MINUTE_LENGTHS = [55, 50, 45, 40, 35, 30] HOUR_LENGTHS = [25, 20, 15, 10] ################################################################################# HEIGHT += 2 * TEXT_HEIGHT CENTER = ((WIDTH / 2), (HEIGHT / 2)) BG_COLOR = html_color_to_int_tuple(BG_COLOR) TEXT_COLOR = html_color_to_int_tuple(TEXT_COLOR) HAND_CONF = ( zip(SECOND_LENGTHS, SECOND_COLORS), zip(MINUTE_LENGTHS, MINUTE_COLORS), zip(HOUR_LENGTHS, HOUR_COLORS), ) def rectangular(r, theta): return ((CENTER[0] + int(r * math.cos(theta))), (CENTER[1] - TEXT_HEIGHT + int(r * math.sin(theta)))) def DEBUG(x): print x return x pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption('BinAna') pygame.mouse.set_visible(0) bg = pygame.Surface((WIDTH, (HEIGHT - (2 * TEXT_HEIGHT)))).convert() font = pygame.font.SysFont(*FONT) clock = pygame.time.Clock() screen.fill(BG_COLOR) txt = font.render('0', 1, TEXT_COLOR) screen.blit(txt, txt.get_rect(top=0, centerx=CENTER[0])) txt = font.render('1', 1, TEXT_COLOR) screen.blit(txt, txt.get_rect(bottom=HEIGHT, centerx=CENTER[0])) while True: events = pygame.event.get() bg.fill(BG_COLOR) for conf, x in zip(HAND_CONF, time.strftime('%S %M %I').split()): for bit_i, (hand_length, hand_color) in enumerate(conf): pygame.draw.line(bg, hand_color, CENTER, rectangular(hand_length, (math.pi * ((float(x) / (1 << bit_i)) % 2))), HAND_WIDTH) screen.blit(bg, (0, TEXT_HEIGHT)) pygame.display.flip() #time.sleep(.001)