-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_Euler_Method.py
55 lines (44 loc) · 1.4 KB
/
1_Euler_Method.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
import pygame, sys
SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 600
RED = (255, 0, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
pygame.init()
pygame.display.set_caption("1. Euler Method")
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;
def update(self, dt):
# USING EULER METHOD
self.position += self.velocity * dt
self.velocity += self.acceleration * dt
def draw(self):
pygame.draw.circle(screen, BLACK, self.position, self.radius)
circle = Circle(pygame.Vector2(1024/2, 0), 50)
circle.acceleration = pygame.Vector2(0, 100)
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
circle.update(deltaTime)
if (circle.position.y - circle.radius > SCREEN_HEIGHT):
circle.position.y = 0
print("Velocity =", circle.velocity)
# DRAWING SECTION
screen.fill(WHITE)
circle.draw()
pygame.display.update()
pygame.quit()
sys.exit()