Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tk_pendulum.py
- from math import sin, cos, degrees, atan2, radians
- from tkinter import *
- from time import time
- ww = 500
- hh = 500
- root = Tk()
- root.title('Pendulum')
- canvas = Canvas(width=ww, height=hh, background='#343434')
- canvas.pack()
- canvas.noMousePress = 1
- r = 30
- rPend = int(ww * 0.5 - r * 2)
- gravity = 0.9995
- mid = int(ww * 0.5)
- penh = 50
- velocity = 120
- angle = 0
- acc = 1.0
- swing = 180
- def xy2deg(
- x1,
- y1,
- x2,
- y2,
- ):
- return degrees(atan2(x2 - x1, y2 - y1)) % 360
- def deg2xy(angle, r):
- a = radians(angle)
- return (sin(a) * r, cos(a) * r)
- YVEL = 1 / 180.0
- def y2vel(z=angle):
- return 180 - abs(180 - z)
- def mouse(e):
- global velocity, acc, swing
- x = e.x
- y = e.y
- canvas.noMousePress = 0
- swing = 180
- velocity = y2vel(xy2deg(mid, penh, x, y))
- if 'ButtonRelease' in str(e):
- canvas.noMousePress = 1
- acc = abs(acc)
- if x < mid:
- acc *= -1
- velocity *= -1
- canvas.bind('<Button-1>', mouse)
- canvas.bind('<B1-Motion>', mouse)
- canvas.bind('<ButtonRelease-1>', mouse)
- while 1:
- endtime = time() + 0.01
- canvas.delete('pendulum')
- swing += acc
- swing %= 360
- if canvas.noMousePress:
- angle = cos(radians(swing)) * velocity * -1
- else:
- angle = velocity
- (ballX, ballY) = deg2xy(angle, rPend)
- velocity *= gravity
- canvas.create_line(
- mid,
- penh,
- mid + ballX,
- penh + ballY,
- width=5,
- fill='orange',
- tags='pendulum',
- )
- canvas.create_oval(
- mid + ballX - r,
- penh + ballY - r,
- mid + ballX + r,
- penh + ballY + r,
- fill='white',
- outline='orange',
- width=5,
- tags='pendulum',
- )
- canvas.update()
- while endtime > time():
- 0
- print(velocity)
Add Comment
Please, Sign In to add comment