Skip to content

Instantly share code, notes, and snippets.

@quitegreensky
Last active January 22, 2021 20:51
Show Gist options
  • Select an option

  • Save quitegreensky/9050acdb0cee666efb161ceaec564a85 to your computer and use it in GitHub Desktop.

Select an option

Save quitegreensky/9050acdb0cee666efb161ceaec564a85 to your computer and use it in GitHub Desktop.
snowfall made with kivy
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.properties import StringProperty, NumericProperty, ListProperty
from kivy.clock import Clock
from kivy.animation import Animation
from random import randint
from kivy.metrics import dp
Builder.load_string(
"""
<SnowItem>
opacity : 0
size_hint: None, None
canvas.before:
Color:
rgba: 1,1,1,1
Ellipse:
pos: self.pos
size: self.size
"""
)
class SnowItem(FloatLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self._update)
def _update(self, *args):
(Animation(opacity = 1, d=randint(*self.parent.time_before_fade))).start(self)
class SnowFall(FloatLayout):
size_range = ListProperty([dp(5), dp(15)])
time_range = ListProperty([3,8])
speed = ListProperty([4,15])
time_before_fade = ListProperty([0,2])
number = NumericProperty(50)
def __init__(self, **kwargs):
super().__init__(**kwargs)
Clock.schedule_once(self._update)
def add_item(self, count):
for x in range(count):
item = SnowItem(
size = [
randint(*self.size_range),
randint(*self.size_range)
],
pos = [
randint(0, self.width),
randint( int(self.height*0.7), self.height)
]
)
self.add_widget(item)
self.start_anim(item)
return item
def _update(self, *args):
self.add_item(self.number)
def start_anim(self, item):
target_pos = [randint(0,self.width),0]
final_time = randint(*self.time_range)
speed = randint(*self.time_range)
anim = Animation(pos = target_pos, d=speed)
anim.start(item)
#remove
Clock.schedule_once(lambda x:self.remove_widget(item), final_time)
#add new
Clock.schedule_once(lambda x:self.add_item(1), final_time)
fade_time = final_time-randint(*self.time_before_fade)
Clock.schedule_once(lambda x: self._fade_out(item, fade_time), final_time-fade_time)
def _fade_out(self, item, time):
anim = Animation(opacity = 0, d=time)
anim.start(item)
if __name__ == "__main__":
from kivy.app import App
from kivy.factory import Factory
from kivy.animation import Animation
kv = """
Screen:
canvas.before:
Color:
rgba: 0.3,0.3,1,1
Rectangle:
pos: self.pos
size: self.size
SnowFall:
size_hint: None, None
size: root.size
"""
class Test(App):
def build(self):
return Builder.load_string(kv)
if __name__ == '__main__':
Test().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment