Grifter

Particle System V2

Aug 18th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame,sys
  2. from pygame.locals import *
  3. from pygame import gfxdraw
  4. from random import randint
  5. import math        
  6. pygame.init()
  7.  
  8. options = input("Default Settings> (y/n):")
  9.  
  10. class wld():
  11.     def __init__(self,options):
  12.         self.clock = pygame.time.Clock()
  13.         self.FPS = 60      
  14.         self.bls = []      
  15.         if options == "n":
  16.             self.x = int(input("X Resolution:"))
  17.             self.y = int(input("Y Resolution:"))
  18.             self.g = float(input("Acceleration due to gravity:"))
  19.             self.r = int(input("Particle radius:"))
  20.             self.c = input("Colour according to rms or component velocity? (rms/com):")
  21.             self.f = abs(int(input("Drag (Higher = Less Drag):")))          
  22.         else:
  23.             self.x,self.y,self.g,self.r,self.f,self.c = 500,500,9.81,4,500,"rms"            
  24.         self.num = int(input("How Many Balls? (0 for random):"))        
  25.         if (self.num == 0):
  26.             self.num = randint(1,1000)            
  27.         if self.f <= 1 and self.f >=0:
  28.             self.f = 1            
  29.         self.screen = pygame.display.set_mode((self.x,self.y))
  30.  
  31.                
  32. class Balls():
  33.     def __init__(self):
  34.         self.radius = world.r
  35.         self.ydisplacement = (world.y/2)
  36.         self.xdisplacement = (world.x/2)
  37.         self.yvelocity = randint(int(-world.y/3),int(world.y/3))
  38.         self.xvelocity = randint(int(-world.x/3),int(world.x/3))
  39.         self.xlist = []
  40.         self.ylist = []
  41.         self.time = 0
  42.         self.falltime = 0
  43.         self.bouncetime = 0
  44.        
  45.     def get_motion(self):
  46.         self.time = self.time + 0.001
  47.         self.falltime = self.falltime + 0.001
  48.         self.bouncetime = self.bouncetime + 0.001
  49.         self.yvelocity = self.yvelocity + 2 * world.g * self.falltime
  50.         self.ydisplacement = self.ydisplacement + self.yvelocity * self.falltime + 0.5 * world.g * self.falltime
  51.         self.xdisplacement = self.xdisplacement + self.xvelocity * self.bouncetime
  52.  
  53.     def friction(self):
  54.         if not self.xvelocity == 0 and self.xvelocity >= 0:
  55.             self.xvelocity = self.xvelocity - self.xvelocity/world.f        
  56.         elif not self.xvelocity == 0 and self.xvelocity <= 0:
  57.             self.xvelocity = self.xvelocity - self.xvelocity/world.f
  58.         if self.xvelocity >= -0.2 and self.xvelocity <= 0.2:
  59.             self.xvelocity = 0
  60.        
  61.     def calc_ybounce(self):
  62.         if self.ydisplacement >= world.y - self.radius:
  63.             self.yvelocity = (-1) * self.yvelocity
  64.             self.ydisplacement = world.y - self.radius
  65.             self.bouncetime = 0
  66.  
  67.         if self.ydisplacement <= 0+self.radius:
  68.             self.yvelocity = (-1) * self.yvelocity
  69.             self.ydisplacement = 0+self.radius
  70.             self.bouncetime =0
  71.            
  72.     def calc_xbounce(self):
  73.         if self.xdisplacement >= world.x - self.radius:
  74.             self.xvelocity = (-1) * self.xvelocity
  75.             self.xdisplacement = world.x - self.radius
  76.            
  77.         if self.xdisplacement <= 0 + self.radius:
  78.             self.xvelocity = (-1) * self.xvelocity
  79.             self.xdisplacement = 0 + self.radius
  80.      
  81.     def draw(self):
  82.         self.col = int((((self.xvelocity**2)+(self.yvelocity**2))/2)**0.5)
  83.         if world.c == "com":
  84.             pygame.gfxdraw.filled_circle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(self.col/2,abs(int(self.yvelocity)),abs(int(self.xvelocity))))
  85.             pygame.gfxdraw.aacircle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(self.col/2,abs(int(self.yvelocity)),abs(int(self.xvelocity))))
  86.         else:
  87.             pygame.gfxdraw.filled_circle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(50,self.col,self.col/2))
  88.             pygame.gfxdraw.aacircle(world.screen,int(self.xdisplacement),int(self.ydisplacement),self.radius,(50,self.col,self.col/2))
  89.        
  90.     def tick(self):
  91.         self.get_motion()
  92.         self.friction()
  93.         self.calc_ybounce()
  94.         self.calc_xbounce()
  95.         self.draw()
  96.  
  97. world = wld(options)
  98. for i in range(0,world.num):
  99.     world.bls.append(Balls())
  100.  
  101. while True:
  102.     world.screen.fill((200,200,200))
  103.     tick = world.clock.tick(world.FPS)
  104.     for i in range(0,world.num):
  105.         world.bls[i].tick()
  106.     pygame.display.update()
  107.        
  108.     for event in pygame.event.get():
  109.         if event.type == QUIT:
  110.             pygame.quit()
  111.             sys.exit()
  112.         if event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE and len(world.bls) <= 500:
  113.             if world.graphing:
  114.                 world.graphing = False
  115.             else:
  116.                 world.graphing = True
Advertisement
Add Comment
Please, Sign In to add comment