|
4 | 4 | import operator |
5 | 5 | import time |
6 | 6 | import math |
| 7 | +import random |
7 | 8 | from typing import NamedTuple, Callable, TypeVar, Generator |
8 | 9 | from enum import Enum |
9 | 10 | from functools import partialmethod |
@@ -76,11 +77,14 @@ def start(self) -> Generator[Callable]: |
76 | 77 | """ |
77 | 78 | The entry point for generating move commands. |
78 | 79 | """ |
| 80 | + self.reset() |
| 81 | + while self.current != self.end: |
| 82 | + yield self.move |
| 83 | + |
| 84 | + def reset(self): |
79 | 85 | self.time = time.time() |
80 | 86 | self.beg = self.current |
81 | 87 | self.distance = self.beg.distance(self.end) |
82 | | - while self.current != self.end: |
83 | | - yield self.move |
84 | 88 |
|
85 | 89 | def move(self): |
86 | 90 | self.canvas.move(self.id, *self.increment) |
@@ -118,20 +122,28 @@ def journey(self): |
118 | 122 |
|
119 | 123 | class BounceBall(Motion): |
120 | 124 |
|
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() |
125 | 135 |
|
126 | 136 | @property |
127 | 137 | def increment(self): |
128 | 138 | self.canvas.update() |
129 | | - self.end += self.get_bounce() |
| 139 | + bounce = self.get_bounce() |
| 140 | + if bounce != Coord(0, 0): |
| 141 | + self.kick(bounce) |
130 | 142 | return self.future - self.current |
131 | 143 |
|
132 | 144 | def get_bounce(self): |
| 145 | + self.canvas.update() |
133 | 146 | x1, y1, x2, y2 = self.canvas.bbox(self.id) |
134 | | - |
135 | 147 | bounce = Coord(0, 0) |
136 | 148 | if x1 <= self.bound_x1: |
137 | 149 | print('x1', x1, self.bound_x1) |
|
0 commit comments