-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathmass_spring.py
More file actions
49 lines (34 loc) · 839 Bytes
/
mass_spring.py
File metadata and controls
49 lines (34 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pygame, sys, math
pygame.init()
FPS = 10 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
screen = pygame.display.set_mode((700, 600), 0, 32)
pygame.display.set_caption('Mass Spring System')
BLACK = (10, 105, 255)
WHITE = (255, 0, 0)
delta_t = 0.01
m = 1
k = 1 #Spring Constant
x = 0
y = 250
vx = 30
vy = 0
while True:
#screen.fill(BLACK)
fx = 0
fy = -k * (y-150)
vx = vx + (fx / m) * delta_t
vy = vy + (fy / m) * delta_t
x = x + vx * delta_t
y = y + vy * delta_t
if x > 700:
screen.fill(BLACK)
x = 0
pygame.draw.circle(screen, WHITE, (int(x), int(y)), 3)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(FPS)