Created
July 26, 2011 15:25
-
-
Save rkulla/1107006 to your computer and use it in GitHub Desktop.
Binary Animation with Pygame
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Description: Binary animation screensaver | |
Usage: python binanim.py | |
Strike a key or move the mouse to exit. | |
You can pass the class constructor a dictionary of config values: | |
BinaryAnimation({'resolution': (800, 600), 'bin_len': 8, | |
'delay_ms': 3000, 'background_color': (0, 0, 90)}) | |
Requirements: the Pygame library from www.pygame.org. | |
""" | |
try: | |
import sys | |
import re | |
from random import choice | |
import pygame | |
from pygame.locals import K_ESCAPE, KEYDOWN, MOUSEMOTION, QUIT, FULLSCREEN | |
except ImportError, err: | |
print >> sys.stderr, "Couldn't load module. %s" % (err) | |
sys.exit(2) | |
class PygText(object): | |
def __init__(self, surface): | |
self.surface = surface | |
def show(self, msg, font_size=12, pos='center', font_color=(255, 255, 255)): | |
""" | |
Write text on a surface. | |
By default, the text is positioned fully centered. | |
Position values: bottom, bottomleft, bottomright, center, left, | |
midbottom, midleft, midright, midtop, right, top, | |
topleft, topright. | |
You can also supply a tuple of x/y coordinates. | |
""" | |
font = pygame.font.Font(None, font_size) | |
ren = font.render(msg, 1, font_color) # renders text with transparent bg | |
ren_rect = ren.get_rect() | |
# Position the text: | |
if isinstance(pos, tuple): | |
ren_rect = ren.get_rect() | |
ren_rect[0] = pos[0] | |
ren_rect[1] = pos[1] | |
else: | |
# Set a named position attribute: | |
setattr(ren_rect, pos, getattr(self.surface.get_rect(), pos)) | |
# Display the text: | |
self.surface.blit(ren, ren_rect) | |
pygame.display.update(ren_rect) | |
def clear(self, color=(0, 0, 0)): | |
self.surface.fill(color) | |
pygame.display.update() | |
class BinaryAnimation(object): | |
def __init__(self, config={}): | |
if config: | |
self.config = config | |
else: | |
self.config = {'resolution': (600, 400), 'bin_len': 32, | |
'delay_ms': 1000, 'background_color': (0, 0, 0)} | |
self.config.update(config) | |
self.resolution = self.config['resolution'] | |
self.delay_ms = self.config['delay_ms'] | |
self.bin_len = self.config['bin_len'] # Amount of bits to display. | |
self.background_color = self.config['background_color'] | |
pygame.init() | |
pygame.font.init() | |
pygame.display.set_caption('Binary Animation') | |
self.screen = pygame.display.set_mode(self.resolution, FULLSCREEN) | |
pygame.mouse.set_visible(False) | |
# Generate a string of bits, separated every "half-octect": | |
self.bits = ''.join(['1010 ' for x in xrange(self.bin_len / 4)]) | |
def __shuffle(self): | |
# Randomize only the bits, not the spaces. | |
self.bits = re.sub('\d', lambda ignored: choice('10'), self.bits) | |
def __delay(self): | |
pygame.time.wait(self.delay_ms) | |
def start(self): | |
t = PygText(self.screen) | |
motion = 0 | |
while True: | |
for event in pygame.event.get(): | |
# Require a certain amount of motion to stop the screensaver. | |
if event.type == MOUSEMOTION: | |
motion = motion + 1 | |
if motion >= 10 or event.type == QUIT or (event.type == KEYDOWN): | |
pygame.quit() | |
return | |
# Example of displaying the binary number animations: | |
t.clear(self.background_color) # Erase text before overwriting. | |
t.show(self.bits, 15, 'topright', (105, 105, 105)) | |
t.show(self.bits, 15, 'topleft', (105, 105, 105)) | |
self.__shuffle() | |
t.show(self.bits, 25, 'center', (0, 255, 0)) | |
self.__shuffle() | |
t.show(self.bits, 15, 'bottomleft', (105, 105, 105)) | |
t.show(self.bits, 15, 'bottomright', (105, 105, 105)) | |
self.__shuffle() | |
self.__delay() | |
if __name__ == '__main__': | |
animation = BinaryAnimation({'delay_ms': 1100, | |
'background_color': (0, 0, 0)}) | |
animation.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment