Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Bouncing particle swarm animation
- # Copyright 2021, 2023 Roland Richter [Processing.py]
- from __future__ import print_function
- # Each particle holds data about
- # - its look, i.e. the text to display, and its color ("face" and "color")
- # - its current horizontal and vertical position ("x" and "y")
- # - its current horizontal and vertical velocity ("vx" and "vy")
- # TRY to add a field "size" which determines how large the particle is.
- particles = [
- {"face": "O.O", "colour": color(120, 80, 80), "x": 450, "y": 300, "vx": -2, "vy": 5, },
- {"face": "^l^", "colour": color(240, 80, 80), "x": 150, "y": 500, "vx": 5, "vy": 2, },
- {"face": ">^_^<", "colour": color(360, 80, 80), "x": 550, "y": 100, "vx": -5, "vy": -2, },
- ]
- def create_particle(face):
- """Create a particle with the given face; all other fields are set to random values."""
- new_particle = {"face": face}
- new_particle["colour"] = color(random(0, 360), 80, 80)
- new_particle["x"] = random(50, width-50)
- new_particle["y"] = random(50, height-50)
- new_particle["vx"] = random(2, 8)
- new_particle["vy"] = random(2, 8)
- return new_particle
- def setup():
- size(900, 600)
- colorMode(HSB, 360, 100, 100)
- textSize(26)
- textAlign(CENTER, CENTER)
- # Add nine particles to the global list
- global particles
- for k in range(9):
- new_particle = create_particle("(-;")
- particles.append(new_particle)
- frameRate(30)
- def draw():
- background("#00BFFF") # "Deep sky blue"
- global particles
- # Update position and velocity of each particle
- for p in particles:
- # Compute new x/y position by adding x/y velocity
- p["x"] += p["vx"]
- p["y"] += p["vy"]
- # Bounce off the edge if necessary: reverse the horizontal or
- # vertical part of velocity vector (i.e. flip its sign)
- # TRY to change the particles' colour with each bounce
- if p["x"] < 25 or p["x"] > width-25:
- p["vx"] = -p["vx"]
- if p["y"] < 25 or p["y"] > height-25:
- p["vy"] = -p["vy"]
- # Draw each particle as a colored cirlce, write text in white
- for p in particles:
- fill(p["colour"])
- circle(p["x"], p["y"], 50)
- fill("#FFFFFF")
- text(p["face"], p["x"], p["y"]-4)
- # TRY to create a new particle at mouse click
- # ----------------------------------------------------------------------
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement