Ball(x, y, radius) -> Ball
| 84 | ################################################################################ |
| 85 | |
| 86 | class Ball: |
| 87 | |
| 88 | 'Ball(x, y, radius) -> Ball' |
| 89 | |
| 90 | def __init__(self, x, y, radius): |
| 91 | 'Initialize the Ball object.' |
| 92 | self.pos = Vector(x, y) |
| 93 | self.vel = Vector(0, 0) |
| 94 | self.err = Vector(0, 0) |
| 95 | self.rad = radius |
| 96 | |
| 97 | def crash(self, ball): |
| 98 | 'Try to crash two balls together.' |
| 99 | p = ball.pos - self.pos |
| 100 | a = abs(p) |
| 101 | if a <= self.rad + ball.rad: |
| 102 | v = self.vel - ball.vel |
| 103 | s = _sub(_ang(p), _ang(v)) |
| 104 | if s < _PI_D_2: |
| 105 | e = p * (_math.cos(s) * abs(v) / a) |
| 106 | ball.err += e |
| 107 | self.err -= e |
| 108 | |
| 109 | def move(self, frames_per_second): |
| 110 | 'Update the ball\'s position.' |
| 111 | self.vel += self.err |
| 112 | self.err = Vector(0, 0) |
| 113 | self.pos += self.vel / frames_per_second |
| 114 | |
| 115 | ################################################################################ |
| 116 |