Skip to content

Instantly share code, notes, and snippets.

@Ephraim-Bryski
Created June 5, 2018 15:05
Show Gist options
  • Save Ephraim-Bryski/fb3a39497a7db5e5db48d3684257aa0c to your computer and use it in GitHub Desktop.
Save Ephraim-Bryski/fb3a39497a7db5e5db48d3684257aa0c to your computer and use it in GitHub Desktop.
from tkinter import *
import random
import time
import math
tk=Tk()
WIDTH=500
HEIGHT=400
canvas=Canvas(tk,width=WIDTH,height=HEIGHT)
tk.title('Spring')
canvas.pack()
dt=.0001
k=1000
mass=1
length=40
num=10
class Ball:
def __init__(self,pos):
self.shape=canvas.create_oval(pos,100,pos+10,110,fill='blue')
self.vel=0
def move(self,left,right,i):
acc=k*(right-pos-length)-k*(pos-left-length)
self.vel+=acc*dt
#if i==0:
#self.vel=0
canvas.move(self.shape,self.vel*dt,0)
balls=[]
for x in range (num):
balls.append(Ball(x*50+100))
for x in range(100000):
for i in range(len(balls)):
if i==0:
left=canvas.coords(balls[i].shape)
left=left[0]-length
else:
left=canvas.coords(balls[i-1].shape)
left=left[0]
if i==len(balls)-1:
right=canvas.coords(balls[i].shape)
right=right[0]+length
else:
right=canvas.coords(balls[i+1].shape)
right=right[0]
pos=canvas.coords(balls[i].shape)[0]
balls[i].move(left,right,i)
tk.update()
tk.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment