Created
October 10, 2011 14:37
-
-
Save j0hn/1275476 to your computer and use it in GitHub Desktop.
j0hn's hamster buto
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 | |
# coding: utf-8 | |
""" | |
j0hn's hamster buto. | |
Math bitches (?) | |
""" | |
import gtk | |
import math | |
import gobject | |
from lib import graphics | |
from lib.pytweener import Easing | |
BAR_HEIGHT = 300 | |
BAR_WIDTH = 10 | |
class VisualizerBar(graphics.Sprite): | |
def __init__(self, x, y): | |
graphics.Sprite.__init__(self, x=x, y=y) | |
self.interactive = True | |
self.width = BAR_WIDTH | |
self.height = 0 | |
self.connect("on-render", self.on_render) | |
def on_render(self, sprite): | |
color_r = 0 | |
color_g = 0 | |
color_b = (150.0/100.0) * abs(self.height) | |
self.graphics.rectangle(0, -self.height, self.width, self.height, 0) | |
self.graphics.fill((color_r, color_g, color_b)) | |
class VisualizerScene(graphics.Scene): | |
def __init__(self): | |
graphics.Scene.__init__(self) | |
self.bars = [] | |
self.bar_turn = 0 | |
self.sin_val = 1 | |
for i in xrange(560 / BAR_WIDTH): | |
self.add_bar(25 + (BAR_WIDTH * i), 200) | |
gobject.timeout_add(50, self.move_bars) | |
def add_bar(self, x, y): | |
bar = VisualizerBar(x, y) | |
self.bars.append(bar) | |
self.add_child(bar) | |
def move_bars(self): | |
bar = self.bars[self.bar_turn] | |
newheight = math.cos(self.sin_val/18.0)/2 * BAR_HEIGHT | |
self.animate(bar, height=newheight, easing=Easing.Bounce.ease_out) | |
self.sin_val += 1 | |
self.bar_turn = (self.bar_turn + 1) % len(self.bars) | |
return True | |
class MainWindow(gtk.Window): | |
def __init__(self): | |
gtk.Window.__init__(self) | |
self.set_default_size(600, 400) | |
self.connect("destroy", gtk.main_quit) | |
self.add(VisualizerScene()) | |
self.show_all() | |
if __name__ == "__main__": | |
MainWindow() | |
gtk.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment