Python物理沙盒粒子效果

仔细看这个粒子效果,粒子之间也有碰撞。

"""用物理沙盒制作的粒子效果"""

from pyphysicssandbox import *
from random import randint
    
def observer_func(keys):
    """每帧运行一次的函数,keys是按键,加上帧计数器就可以变成每隔n帧运行一次"""
    rgb = Color(randint(0, 255), randint(0, 255), randint(0, 255))    
    p = ColoredParticle(rgb)
    p.hit()
    
class Particle:
    def __init__(self):
        
        self.ball = ball((200+randint(-1,1), 350+randint(-1,1)), 2)
        self.direction = randint(-500,500),randint(-3000,-2000)
          
    def hit(self):        
        self.ball.hit(self.direction,(0,0))
        
class ColoredParticle(Particle):
    def __init__(self,c):
        super().__init__()       
        self.color = c 
        self.ball.color = self.color
        
window("Python物理沙盒粒子效果", 400, 400)

gravity(0.0, 60.0)

# 这是起装饰作用的矩形,用来做背景色 
c = cosmetic_box((0,0),400,400)
c.color = (0,0,0)       # 黑色
 
add_observer(observer_func)

run()