Doing a binary watch with python and pygame
There are 10 different types of people in this world - those that can read binary and those that can`t !
I presented the outcome of the code below to around 5 people. Only 2 understood what it was. I must say however that all 5 supposedly are IT professionals.
Here is a small solution you can copy and use to create some binary watch. It should be easy to add some features like config files or change colors or layout. Two screens to demonstrate how it may look like
It was just a hack, taking some hour or so.
import pygame, ConfigParser from pygame import * from time import localtime class BinaryTile: def __init__(self, position, size=48): self.image = pygame.Surface((size, size)).convert() self.position=position self.setToOff() def setToOff(self): self.color = Color('gray') self.image.fill(self.color) def setToOn(self): self.color = Color('white') self.image.fill(self.color) def draw(self, screen): screen.blit(self.image, self.position) class BinaryWatch(): """ a binary watch """ def __init__(self, screensize, dotsize): pygame.time.set_timer(USEREVENT, 1000) #self.time=localtime() self.binaryOn={'1':[0], '2':[1], '3':[0, 1], '4':[2], '5':[0, 2], \ '6':[1, 2], '7':[0, 1, 2], '8':[3], '9':[0, 3]} width_step=screensize[0]/10 height_step=screensize[1]/10 xStart=screensize[0]*1/5 yStart=screensize[1]*3/4-dotsize self.bts=[] for i in range(0, 2): self.appendTile(i,0,xStart, yStart, width_step, height_step, dotsize) for i in range(0, 4): self.appendTile(i,1,xStart, yStart, width_step, height_step, dotsize) for i in range(0, 3): self.appendTile(i,2,xStart, yStart, width_step, height_step, dotsize) for i in range(0, 4): self.appendTile(i,3,xStart, yStart, width_step, height_step, dotsize) for i in range(0, 3): self.appendTile(i,4,xStart, yStart, width_step, height_step, dotsize) for i in range(0, 4): self.appendTile(i,5,xStart, yStart, width_step, height_step, dotsize) def appendTile(self,index,column, xStart, yStart, width_step, height_step, dotsize): divider=0 if index>0: divider = 10 self.bts.append(BinaryTile((xStart+column*width_step, yStart-index*(height_step+divider)), dotsize)) def setTilesOnTime(self, time, tilestarttens, tilestartones): ones=time % 10 tens=time / 10 set=self.binaryOn.get(str(ones)) if set != None: for tile in set: self.bts[tile+tilestartones].setToOn() set=self.binaryOn.get(str(tens)) if set != None: for tile in set: self.bts[tile+tilestarttens].setToOn() def update(self, event): if event.type == USEREVENT: self.time=localtime() for bt in self.bts: bt.setToOff() time=self.time[5] self.setTilesOnTime(time, 13, 16) time=self.time[4] self.setTilesOnTime(time, 6, 9) time=self.time[3] self.setTilesOnTime(time, 0, 2) def draw(self, screen): for bt in self.bts: bt.draw(screen) def main(): pygame.init() flags = 0 #screensize=(1024, 800) #screensize=(800, 600) screensize=(640, 480) screen = pygame.display.set_mode(screensize, flags) timer = pygame.time.Clock() binary_watch=BinaryWatch(screensize, dotsize=48) pygame.mouse.set_visible(False) while 1: timer.tick(25) for e in pygame.event.get(): if e.type == QUIT: raise SystemExit, "QUIT" elif e.type == KEYDOWN and e.key == K_ESCAPE: raise SystemExit, "ESCAPE" else: binary_watch.update(e) screen.fill([0, 0, 0, 0]) binary_watch.draw(screen) pygame.display.flip() if(__name__ == "__main__"): main()




By: Kerim in
on 25 September 2009 at 14:19 brave said …
Nice idea.
on 2 February 2011 at 04:00 ch1zra said …
hmm.. I'm getting :
Traceback (most recent call last):
File "C:\py\watcher.py", line 109, in <module>
main()
File "C:\py\watcher.py", line 88, in main
screen = pygame.display.set_mode(screensize, flags)
error: No available video device
im still somewhat new to Python, so I'm probably doing something wrong :)
just c/p-ed the code and hit F5.
on 2 February 2011 at 13:25 Kerim Mansour said …
Hi ch1zra,
has been a while since i last ran the code...
But what you write seems to be a problem with your environment.
I found several problems with this error message, all related to the combination of operating system, pythin/pygame version and the underlying sdl.
For example windows: http://pygame.motherhamster.org/bugzilla/show_bug.cgi?id=31
One advice i also found (again for windows):
import pygame, os
os.environ['SDL_VIDEODRIVER'] = 'windib'
pygame.display.init()
pygame.mixer.init()