Skip to content

Instantly share code, notes, and snippets.

@basicxman
Created August 27, 2011 02:47
Show Gist options
  • Select an option

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

Select an option

Save basicxman/1174894 to your computer and use it in GitHub Desktop.
from Tkinter import *
import random
def animation_threshold():
global i
if i == 50000:
i = 0
return False
else:
i += 1
return True
def change_dir(item):
global objects
objects[item][0] *= -1
cv.move(objects[item][1], objects[item][0], 0)
cv.update()
def update_objects():
global objects
for i in range(0, len(objects)):
x = cv.coords(objects[i][1])[0]
if x < 5:
change_dir(i)
elif x > 470:
change_dir(i)
else:
cv.move(objects[i][1], objects[i][0], 0)
cv.update()
def add_object(event = None):
global objects
x, y = random.randint(0, 475), random.randint(25, 475)
objects.append([5, cv.create_oval(x, y, x + 25, y + 25, fill = "blue")])
cv.update()
root = Tk()
w = 500
h = 500
cv = Canvas(width = w, height = h)
cv.pack()
cv.update()
objects = []
i = 0
root.bind("a", add_object)
add_object()
while True:
if animation_threshold(): continue
update_objects()
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment