atulh4c

fixedCode

Jan 14th, 2016
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.uix.boxlayout import BoxLayout
  3. from kivy.properties import (ObjectProperty, ListProperty, StringProperty,
  4. NumericProperty)
  5. from kivy.factory import Factory
  6. import random
  7. from kivy.graphics import Color, Ellipse
  8. from kivy.clock import Clock
  9. from kivy.lang import Builder
  10.  
  11. kv = '''
  12. Snowing:
  13. canvas.before:
  14. Color:
  15. rgb: [0.2, 0.2, 0.2]
  16. Ellipse:
  17. pos: self.pos
  18. size: self.size
  19. '''
  20.  
  21. class Snowing(BoxLayout):
  22. FLAKE_SIZE = 5
  23. NUM_FLAKES = 60
  24. FLAKE_AREA = FLAKE_SIZE * NUM_FLAKES
  25. FLAKE_INTERVAL = 1.0 / 30.0
  26.  
  27. def __init__(self, **kwargs):
  28. super(Snowing, self).__init__(**kwargs)
  29. self.flakes = [[x * self.FLAKE_SIZE, 0]
  30. for x in range(self.NUM_FLAKES)]
  31. Clock.schedule_interval(self.update_flakes, self.FLAKE_INTERVAL)
  32.  
  33. def update_flakes(self, time):
  34. for f in self.flakes:
  35. f[0] += random.choice([-1, 1])
  36. f[1] -= random.randint(0, self.FLAKE_SIZE)
  37. if f[1] <= 0:
  38. f[1] = random.randint(0, int(self.height))
  39.  
  40. self.canvas.before.clear()
  41. with self.canvas.before:
  42. widget_x = self.center_x - self. FLAKE_AREA / 2
  43. widget_y = self.pos[1]
  44. for x_flake, y_flake in self.flakes:
  45. x = widget_x + x_flake
  46. y = widget_y + y_flake
  47. Color(0.9, 0.9, 1.0)
  48. Ellipse(pos=(x, y), size=(self.FLAKE_SIZE, self.FLAKE_SIZE))
  49.  
  50.  
  51. class SnowfallApp(App):
  52. def build(self):
  53. return Builder.load_string(kv)
  54.  
  55. if __name__ == '__main__':
  56. SnowfallApp().run()
Add Comment
Please, Sign In to add comment