Skip to content

Instantly share code, notes, and snippets.

@basicxman
Created September 8, 2011 01:19
Show Gist options
  • Select an option

  • Save basicxman/1202363 to your computer and use it in GitHub Desktop.

Select an option

Save basicxman/1202363 to your computer and use it in GitHub Desktop.
from Tkinter import *
import random
import colorsys
import sys
class Bubble(object):
def __init__(self, cv, min_size, max_size, x_bound, y_bound):
self.size = min_size + random.randrange(0, max_size - min_size)
self.x = random.randrange(0, x_bound)
self.y = random.randrange(0, y_bound)
self.iteration = 0
self.cv = cv
# Using an oval instead of an arc because the user should be able to select the inner circle.
self.obj = cv.create_oval(self.x, self.y, self.x + self.size, self.y + self.size, fill="", outline=self.get_colour(), width="1")
self.cv.pack()
self.cv.after(200, self.next_iteration)
def get_colour(self):
value = (self.size - self.iteration) / 100.0
h, l, s = 0.8 - value / 10.0, value, 0.6
r, g, b = map(self.rgbify, colorsys.hls_to_rgb(h, l, s))
return "#%02x%02x%02x" % (r, g, b)
def rgbify(self, value):
x = value * 255 - 15
return x if x > 0 else 0
def next_iteration(self):
self.iteration += 1
self.cv.delete(self.obj)
if self.iteration < self.size / 2:
self.cv.after(200, self.next_iteration)
x1 = self.x + self.iteration
y1 = self.y + self.iteration
x2 = self.x + self.size - self.iteration
y2 = self.y + self.size - self.iteration
self.obj = self.cv.create_oval(x1, y1, x2, y2, fill="", outline=self.get_colour(), width="1")
class App(object):
def __init__(self):
self.min_size = 20
self.max_size = 70
self.frequency = 30
if len(sys.argv) == 2:
self.frequency = int(sys.argv[1])
self.root = Tk()
self.root.title("Bubbles!")
self.root.attributes("-alpha", 0.9)
self.cv = Canvas(self.root, width=600, height=400)
self.cv.pack(expand=True, fill=BOTH)
self.root.after(self.frequency, self.new_bubble)
self.root.bind("<Configure>", self.resize)
self.root.bind("f", lambda e: self.cv.delete("all"))
self.root.bind("=", lambda e: self.change_freq(1))
self.root.bind("-", lambda e: self.change_freq(-1))
self.root.bind("q", lambda e: e.widget.quit())
self.root.mainloop()
def resize(self, event):
self.w = event.width
self.h = event.height
def change_freq(self, value):
self.frequency -= value
def new_bubble(self):
Bubble(self.cv, self.min_size, self.max_size, self.w, self.h)
self.root.after(self.frequency, self.new_bubble)
App()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment