Particle Line System Full Code (python Graphics)

This Is Full Code Here : - 



from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.graphics import Color,Line,Ellipse 
from kivy.clock import Clock 
from kivy.core.window import Window 

import numpy as np 
import math 
import random 

w,h = Window.size 

class ParticleSystem(Widget):
    def __init__(self, **kwargs):
        super().__init__(**kwargs) 

        self.particles = []
        self.moves = []
        self.num_particles = 30 

        with self.canvas :
            self.color_group = []

            for i in range(self.num_particles):
                c = Color(1,0,1,1, mode = "rgba")
                e = Ellipse(pos = (random.randint(0,w),random.randint(0,h)),size = (10,10))

                self.particles.append(e)
                self.moves.append((random.uniform(-2,2),random.uniform(-2,2)))
                self.color_group.append(c)

        self.line_instructions = []
        Clock.schedule_interval(self.update,1/30)

    def update(self,dt):
        for i,p in enumerate(self.particles):
            x,y = p.pos 
            dx,dy = self.moves[i]

            x+=dx 
            y+=dy 

            if x<0 or="" x="">=w:
                dx *= -1
            if y<0 or="" y="">=h:
                dy *= -1

            self.moves[i] = (dx,dy)
            p.pos = (x,y)
        #remove previous Lines 
        for instr in self.line_instructions:
            self.canvas.remove(instr)
        self.line_instructions.clear()

        #Draw lines vetween close particles 
        
        for i in range(self.num_particles):
            x1,y1 = self.particles[i].pos 
            x1 += self.particles[i].size[0]/2
            y1 += self.particles[i].size[1]/2

            for j in range(i+1,self.num_particles):
                x2,y2 = self.particles[j].pos 
                x2 += self.particles[i].size[0]/2
                y2 += self.particles[i].size[1]/2

                distance = math.hypot(x2-x1,y2-y1)
                if distance < 150 :
                    with self.canvas:
                        c = Color(0,1,1,1-distance/150,mode = "rgba")
                        
                        l = Line(points = [x1,y1,x2,y2], width=2)
                        self.line_instructions.extend([c,l])


class myapp(App):
    def build(self):
        return ParticleSystem()
    
if __name__ == "__main__":
    myapp().run()

Comments

Popular posts from this blog

Android WebView App Using Python Kivy

Particle Names App Download

3D Cube Create With Python