-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_Gravity.py
73 lines (58 loc) · 1.82 KB
/
3_Gravity.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
import pygame, sys
SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 600
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAVITATIONAL_CONSTANT = 9.8
pygame.init()
pygame.display.set_caption("3. Gravity")
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
class Object:
def __init__(self, pos : pygame.Vector2):
self.position = pos
self.velocity = pygame.Vector2(0, 0)
self.acceleration = pygame.Vector2(0, 0)
class Circle(Object):
def __init__(self, pos : pygame.Vector2, radius : float):
super(Circle, self).__init__(pos)
self.radius = radius
self.color = BLACK
def update(self, dt):
# IMPLEMENTING GRAVITY
self.acceleration.y += GRAVITATIONAL_CONSTANT
# USING SEMI-IMPLICIT EULER METHOD
self.velocity += self.acceleration * dt
self.position += self.velocity * dt
def draw(self):
pygame.draw.circle(screen, self.color, self.position, self.radius)
circle = Circle(pygame.Vector2(0, 200), 20)
circle.acceleration = pygame.Vector2(500, -150)
tracing_count = 0
traces = []
while True:
deltaTime = clock.tick(60) / 1000.0
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# UPDATING SECTION
if (circle.position.y + circle.radius > SCREEN_HEIGHT):
pass
else:
if (tracing_count % 10 == 0):
c = Circle(pygame.Vector2(circle.position), 10)
c.color = RED
traces.append(c)
circle.update(deltaTime)
print("Velocity =", circle.velocity)
# DRAWING SECTION
screen.fill(WHITE)
for i in range(len(traces)):
traces[i].draw()
circle.draw()
pygame.display.update()
tracing_count += 1
pygame.quit()
sys.exit()