Grifter

Particle System

Aug 14th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.52 KB | None | 0 0
  1. #Libraries
  2. import pygame,sys
  3. from pygame.locals import *
  4. from pygame import gfxdraw
  5. from random import randint as randint
  6. import math
  7.        
  8. #Initialization of pygame and variables
  9. pygame.init()
  10. xres = int(input("X Resolution:"))
  11. yres = int(input("Y Resolution:"))
  12. g = float(input("Acceleration due to gravity:"))
  13. r = int(input("Particle Radius:"))
  14.  
  15. #Class containing world settings and info
  16. class wld():
  17.     def __init__(self,x,y,g,r):
  18.         self.x = x
  19.         self.y = y
  20.         self.num = int(input("How Many Balls? (0 for random):"))
  21.         if (self.num == 0):
  22.             self.num = randint(1,1000)
  23.         self.screen = pygame.display.set_mode((x,y))
  24.         self.clock = pygame.time.Clock()
  25.         self.FPS = 60      
  26.         self.g = g
  27.         self.bls = []
  28.         self.graphing = False
  29.  
  30. #Class for particles
  31. class Balls():
  32. #Initialization of particle variables
  33.     def __init__(self):
  34.         self.radius = r
  35.         self.ydisplacement = (world.y/2)
  36.         self.xdisplacement = (world.x/2)
  37.         self.yvelocity = randint(0,50)
  38.         self.xvelocity = randint(-150,150)
  39.         self.xlist = []
  40.         self.ylist = []
  41.         self.time = 0
  42.         self.falltime = 0
  43.         self.bouncetime = 0
  44.        
  45. #Calculating Motion of particles
  46.     def get_motion(self):
  47.         self.time = self.time + 0.001
  48.         self.falltime = self.falltime + 0.001
  49.         self.bouncetime = self.bouncetime + 0.001
  50.         self.yvelocity = self.yvelocity + 2 * world.g * self.falltime
  51.         self.ydisplacement = self.ydisplacement + self.yvelocity * self.falltime + 0.5 * world.g * self.falltime
  52.         self.xdisplacement = self.xdisplacement + self.xvelocity * self.bouncetime
  53.         if self.xvelocity >= -0.2 and self.xvelocity <= 0.2:
  54.                 self.xvelocity = 0
  55.  
  56. #Calculating bounces along Y axis
  57.     def calc_ybounce(self):
  58.         if self.ydisplacement >= world.y - self.radius:
  59.             self.yvelocity = (-1) * self.yvelocity
  60.             self.ydisplacement = world.y - 1 - self.radius    
  61.             if not self.xvelocity == 0 and self.xvelocity >= 0:
  62.                 self.xvelocity = self.xvelocity - self.xvelocity/25        
  63.             elif not self.xvelocity == 0 and self.xvelocity <= 0:
  64.                 self.xvelocity = self.xvelocity - self.xvelocity/25          
  65.         if self.ydisplacement <= 0+self.radius:
  66.             self.yvelocity = (-1) * self.yvelocity
  67.             self.ydisplacement = 0+self.radius
  68.            
  69. #Calculating bounces along X axis
  70.     def calc_xbounce(self):
  71.         if self.xdisplacement >= world.x - self.radius:
  72.             self.xvelocity = (-1) * self.xvelocity
  73.             self.xdisplacement = world.x - self.radius    
  74.         if self.xdisplacement <= 0 + self.radius:
  75.             self.xvelocity = (-1) * self.xvelocity
  76.             self.xdisplacement = 0 + self.radius
  77.            
  78. #Collect particle metrics for plotting
  79.     def tracking(self):
  80.         self.xlist.append(self.xdisplacement + self.radius*2)
  81.         self.ylist.append(self.ydisplacement + self.radius*2)
  82.         if (len(self.xlist)>=2 and world.graphing):
  83.             for x in range(0,len(self.xlist)-2):
  84.                 pygame.draw.line(world.screen,(0,255,30),(self.xlist[x],self.ylist[x]),(self.xlist[x+1],self.ylist[x+1]),1)
  85.                
  86. #Drawing Particles
  87.     def draw(self):
  88.         pygame.gfxdraw.filled_circle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(30,0,0))
  89.         pygame.gfxdraw.aacircle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(100,0,0))
  90.            
  91. #Tick event to call other class functions in sequence
  92.     def tick(self):
  93.         self.get_motion()
  94.         self.calc_ybounce()
  95.         self.calc_xbounce()
  96.         self.tracking()
  97.         self.draw()
  98.  
  99. #Call world and ball classes to initialize and store particles in table.
  100. world = wld(xres,yres,g,r)
  101. for i in range(0,world.num):
  102.     world.bls.append(Balls())
  103.  
  104. #Main Loop
  105. while True:
  106. #Clock and background
  107.     tick = world.clock.tick(world.FPS)
  108.     world.screen.fill((200,200,200))
  109. #Quit handler
  110.     for event in pygame.event.get():
  111.         if event.type == QUIT:
  112.             pygame.quit()
  113.             sys.exit()
  114.         if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE:
  115.             if world.graphing:
  116.                 world.graphing = False
  117.             else:
  118.                 world.graphing = True
  119. #Tick for all particles and update display
  120.     for i in range(0,world.num):
  121.         world.bls[i].tick()
  122.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment