Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_Gravity.py
- from Tkinter import *
- import random
- import time
- root = Tk()
- root.resizable(0,0)
- ww = 500
- hh = 400
- canvas = Canvas(root, width=ww, height=hh)
- def ready():
- canvas.create_text(ww/2, hh/2-60, text='Click Here', font=("Helvetica", "32"),
- anchor=CENTER, fill='red')
- ready()
- canvas.pack()
- root.wm_title("Gravity")
- n = 120
- x = [None]*n
- y = [None]*n
- vy = [None]*n
- vx = [None]*n
- id = [None]*n
- g = 0.05
- cor = 0.7
- fr = 0.95
- r = 16
- xx = 300
- yy = 100
- def init():
- canvas.delete(ALL)
- for i in range(n):
- # canvas.delete(id[i])
- x[i] = xx
- y[i] = yy
- vx[i] = 16*(random.random()-.5)
- vy[i] = 2*(random.random()-.5)
- id[i] = canvas.create_oval(300-8, 25, 300+8, 25+16, fill="red")
- def updateY(i):
- if ((y[i]+r) < hh):
- vy[i] += g
- else:
- vy[i] = -vy[i]*cor
- vx[i] *= fr
- y[i] += vy[i]
- if ((y[i]+r) > hh):
- y[i] = hh-r
- #
- def updateX(i):
- if x[i]+r >= ww or x[i]-r <= 0:
- vx[i] = -vx[i]*cor
- x[i] += vx[i]
- if x[i]+r > ww:
- x[i] = ww-r
- elif x[i]-r < 0:
- x[i] = r
- #
- def on_canvas_click(ev):
- global action
- init()
- action=1
- #
- canvas.bind('<Button-1>', on_canvas_click)
- start=time.time()
- go=1
- action=0
- while go:
- if action:
- inMotion=''
- if time.time() > start + 0.015:
- start=time.time()
- for i in range(n):
- updateX(i)
- updateY(i)
- if abs(vx[i])+abs(vy[i]) > 0.2:
- canvas.move(id[i], vx[i], vy[i])
- inMotion=True
- if not inMotion: ready()
- root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement