Skip to content

Commit fc1ff0c

Browse files
new effect!
1 parent 8262fcb commit fc1ff0c

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ The following Python programs HAVE been turned into YouTube videos:
4242
* `square migration.py` in: [[2 hour version] Rainbow Matrix of Squares](https://youtu.be/iOO4KsO17LU)
4343
* `square gradient.py` in: [[2 hour version] 4K Square Rainbow Mosaic](https://youtu.be/7QWb9KULsB8)
4444
* `tri angular.py` in: [[1 hour version] 4K 4-Point Color Space](https://youtu.be/kjc7mQ5ldto)
45-
* `line dance.py` in: To be released!
46-
* `perlin spline.py` in: To be released!
45+
* `line dance.py` in: [[1 hour version] 4K Dancing Lines](https://youtu.be/aLU456XVQWY)
46+
* `perlin spline.py` in: [[1 hour version] 4K Colored Perlin Lines](https://youtu.be/KD6DiJURHX8)
47+
* `ball creation.py` in: To be released!
4748

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

src/ball creation.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import pmma
2+
import random
3+
import time
4+
import pygame
5+
import math
6+
7+
pmma.init()
8+
pygame.init()
9+
10+
display = pygame.display.set_mode((1920, 1080), pygame.FULLSCREEN)
11+
clock = pygame.time.Clock()
12+
13+
class Ball:
14+
def __init__(self, x):
15+
self.x = x
16+
self.y = display.get_height() + 300
17+
self.radius = random.randint(1, 100)
18+
self.time = time.perf_counter()
19+
self.color_converter = pmma.ColorConverter()
20+
self.color = self.color_converter.generate_random_color()
21+
self.destroy = False
22+
self.size_change_time = 3
23+
self.distance_moved = 0
24+
self.do_once = False
25+
26+
def collision(self, other_ball):
27+
# Distance between centers
28+
distance = math.sqrt((self.x - other_ball.x)**2 + (self.y - other_ball.y)**2)
29+
if distance <= self.radius + other_ball.radius:
30+
# Adjust positions slightly to avoid overlap
31+
overlap = self.radius + other_ball.radius - distance
32+
angle = math.atan2(other_ball.y - self.y, other_ball.x - self.x)
33+
self.x -= math.cos(angle) * overlap / 2
34+
self.y -= math.sin(angle) * overlap / 2
35+
other_ball.x += math.cos(angle) * overlap / 2
36+
other_ball.y += math.sin(angle) * overlap / 2
37+
38+
def compute(self):
39+
self.y -= 3
40+
41+
if self.y + self.radius <= 0:
42+
self.destroy = True
43+
44+
def to_destroy(self):
45+
return self.destroy
46+
47+
def render(self):
48+
if not self.destroy:
49+
pygame.draw.circle(display, self.color, (self.x, int(self.y)), self.radius)
50+
51+
balls = []
52+
for i in range(150):
53+
balls.append(Ball(random.randint(0, display.get_width())))
54+
55+
while pmma.get_application_running():
56+
#display.fill(pygame.transform.average_color(display))
57+
58+
for event in pygame.event.get():
59+
if event.type == pygame.QUIT:
60+
pmma.set_application_running(False)
61+
62+
for ball in balls:
63+
ball.render()
64+
ball.compute()
65+
for other_ball in balls:
66+
if ball != other_ball:
67+
ball.collision(other_ball)
68+
if ball.to_destroy():
69+
balls.remove(ball)
70+
71+
for i in range(0, 150-len(balls)):
72+
balls.append(Ball(random.randint(0, display.get_width())))
73+
74+
pmma.compute()
75+
76+
pygame.display.update()
77+
clock.tick(60)

0 commit comments

Comments
 (0)