Advertisement
ElliotDG

Untitled

May 7th, 2024
430
0
1 day
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.uix.widget import Widget
  4. from kivy.graphics import Color, Rectangle
  5. from kivy.clock import Clock
  6. from kivy.utils import get_random_color
  7.  
  8. from random import randint
  9.  
  10. kv = """
  11. FloatLayout:
  12.    Label:
  13.        text: 'Falling Boxes'
  14.    FallingBoxes:
  15.        id: fb
  16. """
  17.  
  18.  
  19. class FallingBoxes(Widget):
  20.     def __init__(self, **kwargs):
  21.         super().__init__(**kwargs)
  22.         self.boxes = []
  23.         Clock.schedule_interval(self.update_boxes, 1 / 60)
  24.  
  25.     def create_box(self):
  26.         with self.canvas:
  27.             Color(rgba=get_random_color())
  28.             box = Rectangle(size=(20, 20), pos=(randint(0, 600), 800))
  29.         self.boxes.append(box)
  30.  
  31.     def update_boxes(self, dt):
  32.         for box in self.boxes:
  33.             box.pos = box.pos[0] + randint(1, 10), box.pos[1] - randint(1,20)
  34.             if box.pos[1] < -10:
  35.                 box.pos = (randint(0, 600), 800)
  36.  
  37.  
  38. class FallingBoxesApp(App):
  39.     def build(self):
  40.         return Builder.load_string(kv)
  41.  
  42.     def on_start(self):
  43.         for i in range(1000):
  44.             self.root.ids.fb.create_box()
  45.  
  46.  
  47. FallingBoxesApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement