Advertisement
here2share

# Tk_fizzle.py

Sep 6th, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. # Tk_fizzle.py
  2.  
  3. import Tkinter as tk
  4. import random
  5.  
  6. def create_bubbles(canvas):
  7.     bubbles = []
  8.     for __ in range(30):
  9.         x = random.randint(0, 400)
  10.         y = random.randint(0, 400)
  11.         r = random.randint(5, 30)
  12.         bubble = canvas.create_oval(x-r, y-r, x+r, y+r)
  13.         bubbles.append( [bubble, r] )
  14.     return bubbles
  15.  
  16.  
  17. def moves_bubbles(canvas, bubbles):
  18.     for bubble, r in bubbles:
  19.         x1, y1, x2, y2 = canvas.coords(bubble)
  20.         sz = 15-int(r/2)
  21.         y1 -= 1+sz
  22.         y2 -= 1+sz
  23.         if y2 <= 0:
  24.             y1 = 300
  25.             y2 = y1 + 2*r
  26.  
  27.         # set position
  28.         canvas.coords(bubble, x1, y1, x2, y2)
  29.  
  30.     root.after(40, moves_bubbles, canvas, bubbles)
  31.  
  32. # --- main ---
  33.  
  34. root = tk.Tk()
  35.  
  36. canvas = tk.Canvas(root)
  37. canvas.pack()
  38.  
  39. bubbles = create_bubbles(canvas)
  40.  
  41. # run after 40ms - it gives 25FPS
  42. root.after(40, moves_bubbles, canvas, bubbles)
  43.  
  44. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement