-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGravity2D.py
89 lines (71 loc) · 2.49 KB
/
Gravity2D.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import pygame
import numpy as np
import PhysicsCore as phys
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
WIDTH = 1080
HEIGHT = 1080
FPS = 30
SCROLLSPEED = 1.03
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Gravity2D")
clock = pygame.time.Clock()
model = phys.Scene(screen)
model.addBody()
running = True
while running:
clock.tick(FPS)
#events block:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#keys events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_g:
model.GRAVITY = not model.GRAVITY
print("GRAVITY: %s"%model.GRAVITY)
if event.key == pygame.K_t:
if model.TRACE:
model.clearTraces()
model.TRACE = not model.TRACE
print("TRACE: %s"%model.TRACE)
if event.key == pygame.K_SPACE:
model.PAUSE = not model.PAUSE
print("SIMULATION: %s"%model.PAUSE)
if event.key == pygame.K_p:
model.PREDICT = not model.PREDICT
print("PREDICTION: %s"%model.PREDICT)
#if event.key == pygame.K_f: -- FOCUS MODE!
#mouse events:
if event.type == pygame.MOUSEBUTTONDOWN:
if event.button == 4:
model.k = model.k * SCROLLSPEED
elif event.button == 5:
model.k = model.k / SCROLLSPEED
if event.button == 3:
prevPosition = np.array(pygame.mouse.get_pos())
#keys pushes:
if not model.PAUSE:
if pygame.key.get_pressed()[pygame.K_RIGHT]:
model.bodies[0].velocity[0] += float(model.bodies[0].force) / model.bodies[0].mass / float(FPS)
if pygame.key.get_pressed()[pygame.K_LEFT]:
model.bodies[0].velocity[0] -= float(model.bodies[0].force) / model.bodies[0].mass / float(FPS)
if pygame.key.get_pressed()[pygame.K_UP]:
model.bodies[0].velocity[1] += float(model.bodies[0].force) / model.bodies[0].mass / float(FPS)
if pygame.key.get_pressed()[pygame.K_DOWN]:
model.bodies[0].velocity[1] -= float(model.bodies[0].force) / model.bodies[0].mass / float(FPS)
#mouse pushes:
if pygame.mouse.get_pressed()[2]:
model.center = model.center - model.ScreenCoordsToPhysics(np.array(pygame.mouse.get_pos())) + model.ScreenCoordsToPhysics(prevPosition)
prevPosition = np.array(pygame.mouse.get_pos())
#calculations:
model.move(1./FPS)
#drawing:
screen.fill(BLACK)
model.draw()
pygame.display.flip()
pygame.quit()