Advertisement
rric

quickened_swarm

Dec 11th, 2023 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.10 KB | None | 0 0
  1. # Bouncing particle swarm animation
  2. # Copyright 2021, 2023 Roland Richter                    [Processing.py]
  3.  
  4. from __future__ import print_function
  5.  
  6. # Each particle holds data about
  7. # - its look, i.e. the text to display, and its color ("face" and "color")
  8. # - its current horizontal and vertical position ("x" and "y")
  9. # - its current horizontal and vertical velocity ("vx" and "vy")
  10. # TRY to add a field "size" which determines how large the particle is.
  11. particles = [
  12.     {"face": "O.O", "colour": color(120, 80, 80), "x": 450, "y": 300, "vx": -2, "vy": 5, },
  13.     {"face": "^l^", "colour": color(240, 80, 80), "x": 150, "y": 500, "vx": 5, "vy": 2, },
  14.     {"face": ">^_^<", "colour": color(360, 80, 80), "x": 550, "y": 100, "vx": -5, "vy": -2, },
  15. ]
  16.  
  17. def create_particle(face):
  18.     """Create a particle with the given face; all other fields are set to random values."""
  19.     new_particle = {"face": face}
  20.     new_particle["colour"] = color(random(0, 360), 80, 80)
  21.     new_particle["x"] = random(50, width-50)
  22.     new_particle["y"] = random(50, height-50)
  23.     new_particle["vx"] = random(2, 8)
  24.     new_particle["vy"] = random(2, 8)
  25.  
  26.     return new_particle
  27.  
  28.  
  29. def setup():
  30.     size(900, 600)
  31.     colorMode(HSB, 360, 100, 100)
  32.    
  33.     textSize(26)
  34.     textAlign(CENTER, CENTER)
  35.    
  36.     # Add nine particles to the global list
  37.     global particles
  38.     for k in range(9):
  39.         new_particle = create_particle("(-;")
  40.         particles.append(new_particle)
  41.  
  42.     frameRate(30)
  43.  
  44.  
  45. def draw():
  46.     background("#00BFFF")   # "Deep sky blue"
  47.    
  48.     global particles
  49.     # Update position and velocity of each particle
  50.     for p in particles:
  51.         # Compute new x/y position by adding x/y velocity
  52.         p["x"] += p["vx"]
  53.         p["y"] += p["vy"]
  54.        
  55.         # Bounce off the edge if necessary: reverse the horizontal or
  56.         # vertical part of velocity vector (i.e. flip its sign)
  57.         # TRY to change the particles' colour with each bounce
  58.         if p["x"] < 25 or p["x"] > width-25:
  59.             p["vx"] = -p["vx"]
  60.         if p["y"] < 25 or p["y"] > height-25:
  61.             p["vy"] = -p["vy"]
  62.  
  63.     # Draw each particle as a colored cirlce, write text in white
  64.     for p in particles:
  65.         fill(p["colour"])
  66.         circle(p["x"], p["y"], 50)
  67.         fill("#FFFFFF")
  68.         text(p["face"], p["x"], p["y"]-4)
  69.  
  70.  
  71. # TRY to create a new particle at mouse click
  72.        
  73.  
  74. # ----------------------------------------------------------------------
  75. # This program is free software: you can redistribute it and/or modify
  76. # it under the terms of the GNU General Public License as published by
  77. # the Free Software Foundation, either version 3 of the License, or
  78. # (at your option) any later version.
  79. #
  80. # This program is distributed in the hope that it will be useful,
  81. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  82. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  83. # GNU General Public License for more details.
  84. #
  85. # You should have received a copy of the GNU General Public License
  86. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement