Skip to content

Instantly share code, notes, and snippets.

@sfan5 sfan5/binclock.py
Last active Dec 27, 2015

Embed
What would you like to do?
Binary Clock using PyGame; Shows month, day, hour, minute and second
import pygame, time
from pygame.locals import *
OFFCOLOR = (25,25,25)
ONCOLOR = (42,65,89)
BGCOLOR = (36,36,36)
FIELDSIZE = 25
def rect(s, c, r): # We need to use this because it isn't drawn for some unknown reason when using pygame.draw.rect
for xx in range(r[0], r[2]):
for yy in range(r[1], r[3]):
s.set_at((xx, yy), c)
def drawbin(n, i, b):
basex = n*FIELDSIZE+n
for j in range(0, b):
r = (j*FIELDSIZE+j, basex, (j*FIELDSIZE+j)+FIELDSIZE, basex+FIELDSIZE)
if i & 1 << j:
rect(screen, ONCOLOR, r)
else:
rect(screen, OFFCOLOR, r)
pygame.init()
pygame.display.set_mode((6*FIELDSIZE+6, 5*FIELDSIZE+5))
pygame.display.set_caption("BinClock")
screen = pygame.display.get_surface()
while True:
t = time.localtime()
screen.fill(BGCOLOR)
drawbin(0, t.tm_mon, 4)
drawbin(1, t.tm_mday, 5)
drawbin(2, t.tm_hour, 6)
drawbin(3, t.tm_min, 6)
drawbin(4, t.tm_sec, 6)
pygame.display.flip()
time.sleep(0.5)
event = pygame.event.poll()
if event.type == pygame.QUIT:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.