Skip to content

Commit 41d5e61

Browse files
new fx
1 parent cdd1ea8 commit 41d5e61

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ The following Python programs HAVE been turned into YouTube videos:
4949
* `3D spinning ball.py` in: [[1 hour version] 4K Rainbow Lava Ball](https://youtu.be/CCIVQxMbuWg) and [[1 hour version] 4K Rainbow Plasma](https://youtu.be/GOypjQRR1B8)
5050
* `24 Christmas SPecial.py` in [[1 hour version] 4K Cosmic Snowflake - Christmas Special!](https://youtu.be/XhryrwTfaz0)
5151
* `sphere sphere.py` in: [[5 hour version] 4K Rainbow Spheres](https://youtu.be/ukyNrSWsPN4)
52+
* `circle ascension.py` in: To be released
53+
* `gravity.py` in: To be released
5254

5355
The following Python programs WILL LIKELY be turned into YouTube videos soon:
5456
* `Colored Pixels.py`

src/circle ascension.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import pygame
2+
import pmma
3+
import random
4+
5+
pygame.init()
6+
pmma.init()
7+
8+
now_time = 0
9+
class Circle:
10+
def __init__(self, x, y):
11+
self.position = [x, 150+y]
12+
self.inner_radius = pmma.Perlin()
13+
self.outer_radius_delta = random.randint(10, 50)
14+
self.inner_color = pmma.ColorConverter()
15+
self.outer_color = pmma.ColorConverter()
16+
17+
def render(self):
18+
inner_color = self.inner_color.generate_color_from_perlin_noise(now_time)
19+
outer_color = self.outer_color.generate_color_from_perlin_noise(now_time)
20+
inner_radius = self.inner_radius.generate_1D_perlin_noise(now_time*2, new_range=[0, 100])
21+
pygame.draw.circle(screen, inner_color, self.position, inner_radius)
22+
pygame.draw.circle(screen, outer_color, self.position, inner_radius+self.outer_radius_delta, 2)
23+
24+
screen = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN)
25+
26+
circles = []
27+
x = 0
28+
N = 20
29+
for i in range(20):
30+
circles.append(Circle(x, random.randint(0, 1080-150)))
31+
x += int(1920/N)
32+
33+
surface = pygame.Surface((1920, 1080))
34+
while True:
35+
surface.blit(screen, (0, 0))
36+
screen.fill([0, 0, 0])
37+
screen.blit(surface, (0, 5))
38+
39+
for event in pygame.event.get():
40+
if event.type == pygame.QUIT:
41+
pygame.quit()
42+
quit()
43+
44+
for circle in circles:
45+
circle.render()
46+
47+
pygame.display.update()
48+
now_time = pmma.get_application_run_time() / 5

src/gravity.py

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import pygame
2+
import random
3+
import math
4+
import pmma
5+
6+
# Initialize Pygame
7+
pygame.init()
8+
pmma.init()
9+
10+
# Screen dimensions
11+
WIDTH, HEIGHT = 1920, 1080
12+
screen = pygame.display.set_mode((WIDTH, HEIGHT), pygame.FULLSCREEN)
13+
pygame.display.set_caption("Particle System")
14+
15+
# Colors
16+
BLACK = (0, 0, 0)
17+
WHITE = (255, 255, 255)
18+
19+
class Wipe:
20+
def __init__(self):
21+
self.position = [WIDTH / 2, HEIGHT / 2]
22+
self.radius = 0
23+
self.do_wipe = False
24+
25+
def draw(self):
26+
if self.do_wipe:
27+
pygame.draw.circle(screen, BLACK, (int(self.position[0]), int(self.position[1])), int(self.radius))
28+
pygame.draw.circle(screen, WHITE, (int(self.position[0]), int(self.position[1])), int(self.radius), width=10)
29+
d = 60 - clock.get_fps()
30+
if d > 2:
31+
self.radius += d*1.5
32+
else:
33+
self.radius += 2
34+
35+
if self.radius > 2250:
36+
self.do_wipe = False
37+
self.radius = 0
38+
39+
# Particle class
40+
class Particle:
41+
def __init__(self, x, y, size=1, vx=0, vy=0):
42+
self.x = x
43+
self.y = y
44+
self.size = size
45+
self.vx = vx
46+
self.vy = vy
47+
self.mass = size # Mass is proportional to size
48+
color = pmma.ColorConverter()
49+
self.color = color.generate_random_color(format=pmma.Constants.RGB)
50+
self.explosion_size = random.randint(100, 250)
51+
52+
def move(self):
53+
self.x += self.vx
54+
self.y += self.vy
55+
56+
# Bounce off the edges of the screen
57+
if self.x - self.size < 0 or self.x + self.size > WIDTH:
58+
self.vx = -self.vx
59+
if self.y - self.size < 0 or self.y + self.size > HEIGHT:
60+
self.vy = -self.vy
61+
62+
def draw(self, screen):
63+
pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.size)
64+
65+
def attract(self, other, G=0.5):
66+
# Calculate distance and direction
67+
dx = other.x - self.x
68+
dy = other.y - self.y
69+
distance = math.sqrt(dx**2 + dy**2)
70+
71+
if distance > 0 and distance < 1920: # Limit the interaction range
72+
force = G * (self.mass * other.mass) / distance**2
73+
fx = force * (dx / distance)
74+
fy = force * (dy / distance)
75+
76+
# Apply force to velocities
77+
self.vx += fx / self.mass
78+
self.vy += fy / self.mass
79+
other.vx -= fx / other.mass
80+
other.vy -= fy / other.mass
81+
82+
def combine(self, other):
83+
# Check for collision (simple circle overlap)
84+
dx = other.x - self.x
85+
dy = other.y - self.y
86+
distance = math.sqrt(dx**2 + dy**2)
87+
88+
if distance < self.size + other.size:
89+
# Combine particles
90+
total_mass = self.mass + other.mass
91+
self.vx = (self.vx * self.mass + other.vx * other.mass) / total_mass
92+
self.vy = (self.vy * self.mass + other.vy * other.mass) / total_mass
93+
self.size = int(math.sqrt(self.size**2 + other.size**2))
94+
self.mass = total_mass
95+
red = self.color[0] + other.color[0]
96+
green = self.color[1] + other.color[1]
97+
blue = self.color[2] + other.color[2]
98+
self.color = (red/2, green/2, blue/2)
99+
return True
100+
return False
101+
102+
# Initialize particles
103+
particles = [
104+
Particle(random.randint(50, WIDTH - 50), random.randint(50, HEIGHT - 50),
105+
size=random.randint(1, 5),
106+
vx=random.uniform(-1, 1), vy=random.uniform(-1, 1))
107+
for _ in range(250)
108+
]
109+
110+
# Main loop
111+
running = True
112+
clock = pygame.time.Clock()
113+
114+
surface = pygame.Surface((WIDTH, HEIGHT))
115+
surface.set_alpha(250)
116+
wiper = Wipe()
117+
while running:
118+
surface.blit(screen, (0, 0))
119+
screen.fill(BLACK)
120+
screen.blit(surface, (0, 0))
121+
122+
for event in pygame.event.get():
123+
if event.type == pygame.QUIT:
124+
running = False
125+
126+
# Update particles
127+
for i, p1 in enumerate(particles):
128+
for p2 in particles[i + 1:]:
129+
p1.attract(p2)
130+
if p1.combine(p2):
131+
particles.remove(p2)
132+
p1.move()
133+
p1.draw(screen)
134+
135+
if p1.mass > 100:
136+
particles.remove(p1)
137+
# Explosion logic
138+
num_new_particles = int(p1.explosion_size / 3) # Number of smaller particles
139+
for _ in range(num_new_particles):
140+
angle = random.uniform(0, 2 * math.pi) # Random angle for direction
141+
speed = random.uniform(1, 3) # Speed of the smaller particles
142+
new_vx = math.cos(angle) * speed
143+
new_vy = math.sin(angle) * speed
144+
particles.append(Particle(p1.x + 20 * math.cos(angle), p1.y + 20 * math.sin(angle), size=random.randint(1, 5), vx=new_vx, vy=new_vy))
145+
146+
if ((p1.x - WIDTH/2)**2 + (p1.y - HEIGHT/2)**2)**0.5 < wiper.radius and wiper.do_wipe:
147+
try:
148+
particles.remove(p1)
149+
except:
150+
pass
151+
152+
while len(particles) < 75 and wiper.do_wipe is False:
153+
particles.append(
154+
Particle(random.randint(50, WIDTH - 50), random.randint(50, HEIGHT - 50),
155+
size=random.randint(1, 5),
156+
vx=random.uniform(-1, 1), vy=random.uniform(-1, 1))
157+
)
158+
159+
wiper.draw()
160+
161+
pygame.display.flip()
162+
clock.tick(60)
163+
if clock.get_fps() < 10:
164+
wiper.do_wipe = True
165+
166+
pygame.quit()

0 commit comments

Comments
 (0)