Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- # -*- coding: utf-8 -*-
- # Displays lots of rectangles :)
- import sf
- import random
- class Rectangle():
- def __init__(self, w, h):
- self.angular_velocity = random.randint(1, 359)
- # position
- x = random.randint(20, w - 20)
- y = random.randint(20, h - 20)
- # color
- r = random.randint(0, 255)
- g = random.randint(0, 255)
- b = random.randint(0, 255)
- # size
- w = random.randint(2, 22)
- h = random.randint(2, 22)
- # create shape
- s = sf.RectangleShape((w, h))
- s.fill_color = sf.Color(r, g, b)
- s.position = (x, y)
- s.origin = (w/2, h/2)
- self.shape = s
- def step(self, dt):
- self.shape.rotation += self.angular_velocity * dt
- def draw(self, target, states):
- target.draw(self.shape)
- def main():
- window = sf.RenderWindow(sf.VideoMode(800, 600), b'Rectangles', sf.Style.CLOSE)
- window.framerate_limit = 60
- shapes = []
- for i in range(1, 1000):
- shapes.append(Rectangle(800, 600))
- clock = sf.Clock()
- running = True
- while running:
- # frame time
- dt = clock.restart().as_seconds()
- for event in window.iter_events():
- if (event.type == sf.Event.CLOSED or
- (event.type == sf.Event.KEY_PRESSED
- and event.code == sf.Keyboard.ESCAPE)):
- running = False
- # rotation
- for s in shapes:
- s.step(dt)
- window.clear(sf.Color.WHITE)
- # drawable : fuite
- for s in shapes:
- window.draw(s)
- window.display()
- window.close()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement