Advertisement
metalx1000

PyGame - Particle Objects and Classes

Mar 25th, 2013
1,732
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.52 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import pygame, sys, random
  4. from pygame.locals import *
  5.  
  6. w = 800
  7. h = 480
  8.  
  9. z = 0
  10.  
  11. screen = pygame.display.set_mode((w,h))
  12.  
  13. pygame.display.update()
  14.  
  15. class Ball:
  16.     def __init__(self, radius, y, x, color, size, maxforce, force, life):
  17.         self.y=y
  18.         self.x=x
  19.         self.size=size
  20.         self.maxforce=maxforce
  21.         self.force=force
  22.         self.radius=radius
  23.         self.color=color
  24.         self.life=life
  25.         pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)
  26.  
  27.     def fall(self):
  28.         if self.y < h-self.radius:
  29.             self.y+=self.force
  30.             if self.force < self.maxforce:
  31.                 self.force+=1
  32.         elif self.y > h-self.radius or self.y == h-self.radius:
  33.             self.y = h-self.radius-1
  34.             self.force = self.force*-1
  35.             self.maxforce = self.maxforce/2
  36.         pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius)
  37.         self.life-=1
  38.         if self.life < 0:
  39.             ball.remove(self)  
  40.    
  41.  
  42. clock = pygame.time.Clock()
  43. ball = []
  44. ball.append(Ball(25, 250, 250, (random.randint(1,255),random.randint(1,255),random.randint(1,255)),"L", 25, 1 ,100))
  45.  
  46. while 1:
  47.     clock.tick(60)
  48.     x,y = pygame.mouse.get_pos()
  49.     for event in pygame.event.get():
  50.         if event.type == pygame.QUIT:
  51.             sys.exit()
  52.         elif event.type == MOUSEBUTTONDOWN:
  53.             z = 1
  54.         elif event.type == MOUSEBUTTONUP:
  55.             z = 0
  56.  
  57.     if z == 1:
  58.         ball.append(Ball(25, y, x, (random.randint(1,255),random.randint(1,255),random.randint(1,255)),"L", 25, 1 ,100))
  59.         z = 3
  60.     elif z > 1:
  61.         z-=1
  62.  
  63.     screen.fill((0,0,0))   
  64.  
  65.     for i in ball:
  66.         i.fall()
  67.  
  68.     pygame.display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement