Advertisement
here2share

# Tk_id_ani.py

Sep 26th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. # Tk_id_ani.py
  2.  
  3. from Tkinter import *
  4.  
  5. def animation():
  6.     for v in aliens:
  7.         x1, y1, x2, y2 = canvas.bbox(v)
  8.         if x1 < 0 or x2 > 300:
  9.             vx[v] *= -1
  10.         if y1 < 0 or y2 > 300:
  11.             vy[v] *= -1
  12.         canvas.move(v, vx[v], vy[v])
  13.     canvas.after(10, animation)
  14.  
  15. root = Tk()
  16. canvas = Canvas(root, width=300, height=300)
  17. canvas.pack()
  18.  
  19. aliens =    [
  20.             [(40, 80, 240, 280), 'purple', 2, -3],
  21.             [(40, 20, 80, 60), 'orange', 9, 6]
  22.             ]
  23. vx = {}
  24. vy = {}
  25. for alien in range(len(aliens)):
  26.     xy,color,x,y=aliens[alien]
  27.     obj = canvas.create_oval(xy, width=0, fill=color)
  28.     aliens[alien] = obj
  29.     vx[obj] = x
  30.     vy[obj] = y
  31. animation()
  32. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement