SHARE
TWEET

xelapond

a guest Aug 6th, 2008 393 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pygame
  2. import rabbyt
  3. import os
  4. import sys
  5. import random
  6.  
  7. rabbyt.data_director = os.path.dirname(__file__)
  8.  
  9. #Act upon keyboard and mouse events
  10. def dispatch_events():
  11.     for event in pygame.event.get():
  12.         if event.type == pygame.QUIT:
  13.             sys.exit()
  14.         elif event.type == pygame.KEYDOWN:
  15.             if event.key == 115: stack.scatter()
  16.             elif event.key == 32: stack.gather()
  17.             elif event.key == pygame.K_q: sys.exit()
  18.  
  19. class Stack():
  20.     def __init__(self, pos, screen_size=(1280, 1024), items=[]):
  21.         self.pos = pos
  22.         self.screen_size = screen_size
  23.         self.items = items
  24.  
  25.         #Generate random angle
  26.         self.rr = lambda: random.randrange(0, 360)
  27.         #Generate random y coordinate
  28.         self.rx = lambda: random.randrange(int(self.screen_size[0]/-2), int(self.screen_size[0]/2))
  29.         #Generate random x coordinate
  30.         self.ry = lambda: random.randrange(int(self.screen_size[1]/-2), int(self.screen_size[1]/2))
  31.  
  32.     def add(self, item):
  33.         self.items.append(item)
  34.         item.x = self.pos[0]
  35.         item.y = self.pos[1]
  36.        
  37.     def gather(self, pos='h', dt=.5):
  38.         if pos == 'h': pos = self.pos
  39.         for item in self.items:
  40.             item.x = rabbyt.ease(item.x, pos[0], rabbyt.get_time(), rabbyt.get_time()+dt)
  41.             item.y = rabbyt.ease(item.y, pos[1], rabbyt.get_time(), rabbyt.get_time()+dt)
  42.             rot = self.rr()
  43.             item.rot = rabbyt.ease(item.rot, rot, rabbyt.get_time(), rabbyt.get_time()+.5)
  44.         return 1
  45.  
  46.     def scatter(self, dt=.5):
  47.         for item in self.items:
  48.             x = self.rx()
  49.             y = self.ry()
  50.             item.x = rabbyt.ease(item.x, x, rabbyt.get_time(), rabbyt.get_time()+dt)
  51.             item.y = rabbyt.ease(item.y, y, rabbyt.get_time(), rabbyt.get_time()+dt)
  52.             item.rot = rabbyt.ease(item.rot, self.rr(), rabbyt.get_time(), rabbyt.get_time()+.5)
  53.         return 1
  54.  
  55. if __name__ == '__main__':
  56.     pygame.init()
  57.     pygame.display.set_mode((1280, 1024), pygame.OPENGL | pygame.DOUBLEBUF | pygame.FULLSCREEN | pygame.NOFRAME)
  58.     rabbyt.set_viewport((1280, 1024))
  59.     rabbyt.set_default_attribs()
  60.  
  61.     stack = Stack(pos=[-420, -350])
  62.  
  63.     #Change the number of objects here
  64.     for x in range(0, 10):
  65.         item1 = rabbyt.Sprite('item.png')
  66.         stack.add(item1)
  67.  
  68.     c = pygame.time.Clock()
  69.     while not pygame.event.get(pygame.QUIT):
  70.         c.tick()
  71.         rabbyt.clear()
  72.         rabbyt.set_time(pygame.time.get_ticks()/1000.0)
  73.         rabbyt.render_unsorted(stack.items)
  74.         dispatch_events()
  75.         pygame.display.flip()
RAW Paste Data
Want to get better at Python?
Learn to code Python in 2017
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top