Skip to content

Instantly share code, notes, and snippets.

@smeschke
Created August 26, 2016 18:53
Show Gist options
  • Save smeschke/635d464aaf33bc2fc2bdf8f2785692fb to your computer and use it in GitHub Desktop.
Save smeschke/635d464aaf33bc2fc2bdf8f2785692fb to your computer and use it in GitHub Desktop.
Particle Tutorial - Ants leave the colony. (from http://www.petercollingridge.co.uk/)
import pygame
import random
import math
background_colour = (255,255,255)
(width, height) = (500, 500)
class Particle():
def __init__(self, (x, y), size):
self.x = x
self.y = y
self.size = size
self.colour = (0, 0, 255)
self.thickness = 1
self.speed = 0
self.angle = 0
def display(self):
pygame.draw.circle(screen, self.colour, (int(self.x), int(self.y)), self.size, self.thickness)
def move(self):
self.x += math.sin(self.angle) * self.speed
self.y -= math.cos(self.angle) * self.speed
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 4')
number_of_particles = 100
my_particles = []
for n in range(number_of_particles):
size = random.randint(10, 20)
x = random.randint(size, width-size)
y = random.randint(size, height-size)
particle = Particle((x, y), size)
particle.speed = random.random()
particle.angle = random.uniform(0, math.pi*2)
my_particles.append(particle)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(background_colour)
for particle in my_particles:
particle.move()
particle.display()
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment