Created
April 18, 2018 05:22
-
-
Save KosayJabre/7ad6e4faf51b5864791714d1aa1047b0 to your computer and use it in GitHub Desktop.
Click the mouse to generate a particle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame, random, math | |
WHITE = (255,255,255) | |
BLACK = (0,0,0) | |
SCREENSIZE = (500,500) | |
pygame.init() | |
screen = pygame.display.set_mode(SCREENSIZE) | |
clock = pygame.time.Clock() | |
list_of_points = [] | |
def checkPointCollisions(): | |
for i in range(0, len(list_of_points)): | |
for j in range(i+1, len(list_of_points)): | |
if list_of_points[i] == 0 or list_of_points[j] == 0: | |
continue | |
if abs(list_of_points[i].coordinates[0] - list_of_points[j].coordinates[0]) < list_of_points[i].size + list_of_points[j].size and abs(list_of_points[i].coordinates[1] - list_of_points[j].coordinates[1]) < list_of_points[i].size + list_of_points[j].size: | |
new_coordinates = [(list_of_points[i].coordinates[0] + list_of_points[j].coordinates[0] / 2), (list_of_points[i].coordinates[1] + list_of_points[j].coordinates[1] / 2)] | |
new_point = Point(new_coordinates, | |
[(list_of_points[i].speed[0] + list_of_points[j].speed[0])/2, (list_of_points[i].speed[1] + list_of_points[j].speed[1])/2], | |
list_of_points[i].color, | |
list_of_points[i].size + list_of_points[j].size) | |
list_of_points[i] = 0 | |
list_of_points[j] = 0 | |
list_of_points.append(new_point) | |
class Point(object): | |
def __init__(self, coordinates, speed, color, size): | |
self.coordinates = coordinates | |
self.speed = speed | |
self.color = color | |
self.size = size | |
def checkCollision(self): | |
if SCREENSIZE[0] - self.coordinates[0] - self.size < 5 or self.coordinates[0] < 5: | |
self.speed[0] = self.speed[0] * -1 | |
if SCREENSIZE[1] - self.coordinates[1] - self.size < 5 or self.coordinates[1] < 5: | |
self.speed[1] = self.speed[1] * -1 | |
def createPoint(n): | |
for i in range(n): | |
random_coordinates = [random.randint(0,SCREENSIZE[0]), | |
random.randint(0,SCREENSIZE[1])] | |
random_speed = [random.randint(-5,5), random.randint(-5,5)] | |
if random_speed[0] == 0: | |
random_speed[0] += 1 | |
elif random_speed[1] == 0: | |
random_speed[1] += 1 | |
random_color = (random.randint(0,255), | |
random.randint(0,255), | |
random.randint(0,255)) | |
random_size = random.randint(3, 10) | |
list_of_points.append(Point(random_coordinates, random_speed, random_color, random_size)) | |
while True: | |
screen.fill(BLACK) | |
for event in pygame.event.get(): | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
createPoint(1) | |
checkPointCollisions() | |
for point in list_of_points: | |
if point == 0: | |
continue | |
else: | |
point.checkCollision() | |
point.coordinates[0] += point.speed[0] | |
point.coordinates[1] += point.speed[1] | |
for point in list_of_points: | |
if point != 0: | |
pygame.draw.rect(screen, | |
point.color, | |
(point.coordinates[0], point.coordinates[1], | |
point.size, point.size)) | |
pygame.display.update() | |
clock.tick(60) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment