Want more features on Pastebin? Sign Up, it's FREE!
Guest

rectangles_leak.py

By: a guest on Feb 22nd, 2012  |  syntax: Python  |  size: 1.73 KB  |  views: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Displays lots of rectangles :)
  5.  
  6. import sf
  7. import random
  8.  
  9.  
  10. class Rectangle():
  11.     def __init__(self, w, h):
  12.         self.angular_velocity = random.randint(1, 359)
  13.         # position
  14.         x = random.randint(20, w - 20)
  15.         y = random.randint(20, h - 20)
  16.         # color
  17.         r = random.randint(0, 255)
  18.         g = random.randint(0, 255)
  19.         b = random.randint(0, 255)
  20.         # size
  21.         w = random.randint(2, 22)
  22.         h = random.randint(2, 22)
  23.         # create shape
  24.         s = sf.RectangleShape((w, h))
  25.         s.fill_color = sf.Color(r, g, b)
  26.         s.position = (x, y)
  27.         s.origin = (w/2, h/2)
  28.         self.shape = s
  29.  
  30.     def step(self, dt):
  31.         self.shape.rotation += self.angular_velocity * dt
  32.  
  33.     def draw(self, target, states):
  34.         target.draw(self.shape)
  35.  
  36.  
  37. def main():
  38.     window = sf.RenderWindow(sf.VideoMode(800, 600), b'Rectangles', sf.Style.CLOSE)
  39.     window.framerate_limit = 60
  40.  
  41.     shapes = []
  42.     for i in range(1, 1000):
  43.         shapes.append(Rectangle(800, 600))
  44.  
  45.     clock = sf.Clock()
  46.     running = True
  47.  
  48.     while running:
  49.         # frame time
  50.         dt = clock.restart().as_seconds()
  51.  
  52.         for event in window.iter_events():
  53.             if (event.type == sf.Event.CLOSED or
  54.                 (event.type == sf.Event.KEY_PRESSED
  55.                  and event.code == sf.Keyboard.ESCAPE)):
  56.                 running = False
  57.  
  58.         # rotation
  59.         for s in shapes:
  60.             s.step(dt)
  61.  
  62.         window.clear(sf.Color.WHITE)
  63.  
  64.         # drawable : fuite
  65.         for s in shapes:
  66.             window.draw(s)
  67.  
  68.         window.display()
  69.  
  70.     window.close()
  71.  
  72.  
  73. if __name__ == '__main__':
  74.     main()
clone this paste RAW Paste Data