Skip to content
This repository was archived by the owner on Mar 31, 2020. It is now read-only.

Commit f6b55a5

Browse files
committed
Splash page button bouncing
1 parent 1f61f90 commit f6b55a5

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/animate.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import operator
55
import time
66
import math
7+
import random
78
from typing import NamedTuple, Callable, TypeVar, Generator
89
from enum import Enum
910
from functools import partialmethod
@@ -76,11 +77,14 @@ def start(self) -> Generator[Callable]:
7677
"""
7778
The entry point for generating move commands.
7879
"""
80+
self.reset()
81+
while self.current != self.end:
82+
yield self.move
83+
84+
def reset(self):
7985
self.time = time.time()
8086
self.beg = self.current
8187
self.distance = self.beg.distance(self.end)
82-
while self.current != self.end:
83-
yield self.move
8488

8589
def move(self):
8690
self.canvas.move(self.id, *self.increment)
@@ -118,20 +122,28 @@ def journey(self):
118122

119123
class BounceBall(Motion):
120124

121-
def kick(self, direction: Point, speed=8):
122-
self.direction = direction
123-
self.speed = speed
124-
self.end = self.direction + self.current
125+
chaos = 3
126+
127+
def kick(self, direction: Point):
128+
self.canvas.update()
129+
130+
c1, c2 = -self.chaos, self.chaos
131+
chaoticx, chaoticy = random.randint(c1, c2), random.randint(c1, c2)
132+
self.direction = direction + Coord(chaoticx, chaoticy)
133+
self.end = self.direction * self.canvas.winfo_height()
134+
self.reset()
125135

126136
@property
127137
def increment(self):
128138
self.canvas.update()
129-
self.end += self.get_bounce()
139+
bounce = self.get_bounce()
140+
if bounce != Coord(0, 0):
141+
self.kick(bounce)
130142
return self.future - self.current
131143

132144
def get_bounce(self):
145+
self.canvas.update()
133146
x1, y1, x2, y2 = self.canvas.bbox(self.id)
134-
135147
bounce = Coord(0, 0)
136148
if x1 <= self.bound_x1:
137149
print('x1', x1, self.bound_x1)

src/splash.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22

3-
from .view import Window, View, BounceBall
4-
from .animate import Direction
3+
from .view import Window, View
4+
from .animate import Direction, BounceBall
55
from . import widget, DOCS
66

77

@@ -49,16 +49,15 @@ def init(self):
4949

5050
self.btn_confirm = widget.PrimaryButton(self.window, command=self.begin, text='Okay')
5151

52-
self.title.pack(fill='x', pady=10)
53-
self.window.pack(fill='both', expand=True)
52+
self.title.pack(fill='both')
53+
self.window.pack(fill='both')
5454
self.bounce()
5555

5656
def bounce(self):
5757
bouncer = View(self.window, window=self.btn_confirm)
5858
wid = self.window.set_view(bouncer, self.window.center)
59-
motion = BounceBall(self.window, wid, self.window.origin)
60-
motion.kick(Direction.RIGHT)
61-
59+
motion = BounceBall(self.window, wid, self.window.origin, speed=6)
60+
motion.kick(Direction.DOWN)
6261
self.after(0, self.window.run, motion)
6362

6463
def begin(self):

0 commit comments

Comments
 (0)