-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicPhysics.py
More file actions
71 lines (56 loc) · 1.74 KB
/
Copy pathBasicPhysics.py
File metadata and controls
71 lines (56 loc) · 1.74 KB
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
# A physics simulation in Python using Pygame with interactive controls
import pygame
import sys
# Initialize Pygame
pygame.init()
# Constants
WIDTH, HEIGHT = 800, 600
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GRAVITY = 0.5 # Reduced gravity
BOUNCINESS = 0.7 # Floor bounciness
# Setup the display
screen = pygame.display.set_mode((WIDTH, HEIGHT))
clock = pygame.time.Clock()
# Physics variables
position = pygame.Vector2(WIDTH // 2, HEIGHT // 2)
velocity = pygame.Vector2(-5, 0) # Initial push to the left
acceleration = pygame.Vector2()
# Slider variables
gravity_slider = pygame.Rect(10, 10, 100, 10)
bounciness_slider = pygame.Rect(10, 30, 100, 10)
# Main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Mouse interaction
mouse_down = pygame.mouse.get_pressed()[0]
mouse_pos = pygame.mouse.get_pos()
if mouse_down:
acceleration = (pygame.Vector2(mouse_pos) - position) * 0.1
# Update physics
velocity += acceleration
position += velocity + 0.5 * acceleration
acceleration *= 0 # Reset acceleration
# Boundary checks
if position.y >= HEIGHT:
velocity.y *= -BOUNCINESS
position.y = HEIGHT
if position.y <= 0:
velocity.y *= -BOUNCINESS
position.y = 0
if position.x <= 0 or position.x >= WIDTH:
velocity.x *= -BOUNCINESS
position.x = max(0, min(position.x, WIDTH))
# Drawing
screen.fill(WHITE)
pygame.draw.circle(screen, BLUE, (int(position.x), int(position.y)), 20)
# Sliders
pygame.draw.rect(screen, (180, 180, 180), gravity_slider)
pygame.draw.rect(screen, (180, 180, 180), bounciness_slider)
pygame.display.flip()
clock.tick(60)
pygame.quit()
sys.exit()