-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.gitignore
More file actions
61 lines (49 loc) · 1.53 KB
/
.gitignore
File metadata and controls
61 lines (49 loc) · 1.53 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
import pygame
import random
import math
# --- Constants ---
WIDTH, HEIGHT = 800, 600
GRAVITY = 0.5
FRICTION = 0.99
NUM_OBJECTS = 10
# --- Object Class ---
class Particle:
def __init__(self):
self.radius = random.randint(10, 30)
self.x = random.uniform(self.radius, WIDTH - self.radius)
self.y = random.uniform(self.radius, HEIGHT - self.radius)
self.vx = random.uniform(-5, 5)
self.vy = random.uniform(-5, 5)
self.color = random.choice([(255, 0, 0), (0, 255, 0), (0, 0, 255)])
def update(self):
self.vy += GRAVITY
self.vx *= FRICTION
self.vy *= FRICTION
self.x += self.vx
self.y += self.vy
# Bounce off walls
if self.x <= self.radius or self.x >= WIDTH - self.radius:
self.vx *= -1
if self.y >= HEIGHT - self.radius:
self.vy *= -0.9 # bounce
self.y = HEIGHT - self.radius
def draw(self, screen):
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
# --- Main Loop ---
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Random Physics Engine")
particles = [Particle() for _ in range(NUM_OBJECTS)]
clock = pygame.time.Clock()
running = True
while running:
screen.fill((30, 30, 30))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for p in particles:
p.update()
p.draw(screen)
pygame.display.flip()
clock.tick(60)
pygame.quit()