forked from HeaTTheatR/Articles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget_animation.py
45 lines (36 loc) · 1.04 KB
/
widget_animation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from random import random
from kivy.animation import Animation
from kivy.base import runTouchApp
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
Builder.load_string('''
<Root>:
AnimRect:
pos: 500, 300
<AnimRect>:
canvas:
Color:
rgba: 0, 1, 0, 1
Rectangle:
pos: self.pos
size: self.size
''')
class Root(Widget):
pass
class AnimRect(Widget):
def anim_to_random_pos(self):
Animation.cancel_all(self)
random_x = random() * (Window.width - self.width)
random_y = random() * (Window.height - self.height)
anim = Animation(x=random_x, y=random_y,
duration=2,
t='out_elastic')
anim.start(self)
def on_touch_down(self, touch):
if self.collide_point(*touch.pos):
self.anim_to_random_pos()
runTouchApp(Root())